Solve 2016 day 16.

This commit is contained in:
felixm 2024-05-05 15:51:43 -04:00
parent 29ab4f3496
commit 824e235cec

42
2016/d16.py Normal file
View File

@ -0,0 +1,42 @@
def step(a: str) -> str:
b = "".join(["1" if c == "0" else "0" for c in reversed(a)])
return a + "0" + b
def fill(a: str, n: int) -> str:
while len(a) < n:
a = step(a)
return a[:n]
def checksum(a: str) -> str:
while len(a) % 2 == 0:
xs = []
for i in range(0, len(a), 2):
if a[i] == a[i + 1]:
xs.append("1")
else:
xs.append("0")
a = "".join(xs)
return a
def solve():
assert step("1") == "100"
assert step("0") == "001"
assert step("11111") == "11111000000"
with open("i16.txt") as f:
d = f.read().strip()
a = fill(d, 272)
b = checksum(a)
print(b)
a = fill(d, 35651584)
b = checksum(a)
print(b)
if __name__ == "__main__":
solve()