Compare commits
2 Commits
29ab4f3496
...
f0a6214052
| Author | SHA1 | Date | |
|---|---|---|---|
| f0a6214052 | |||
| 824e235cec |
42
2016/d16.py
Normal file
42
2016/d16.py
Normal 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()
|
||||
40
2016/d17.py
Normal file
40
2016/d17.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import hashlib
|
||||
from collections import deque
|
||||
from lib import add2
|
||||
|
||||
|
||||
def solve():
|
||||
part_1 = False
|
||||
DIRS = [("U", (-1, 0)), ("D", (1, 0)), ("L", (0, -1)), ("R", (0, 1))]
|
||||
with open("i17.txt") as f:
|
||||
passcode = f.read().strip()
|
||||
|
||||
maxlen = 0
|
||||
poss = deque([((0, 0), f"{passcode}")])
|
||||
while poss:
|
||||
if part_1:
|
||||
pos, path = poss.popleft()
|
||||
else:
|
||||
pos, path = poss.pop()
|
||||
|
||||
if pos == (3, 3):
|
||||
if part_1:
|
||||
print(path[len(passcode):])
|
||||
return
|
||||
else:
|
||||
l = len(path) - len(passcode)
|
||||
if l > maxlen:
|
||||
maxlen = l
|
||||
else:
|
||||
status = hashlib.md5(f"{path}".encode()).hexdigest()
|
||||
for (c, d), s in zip(DIRS, status[:4]):
|
||||
if s in "bcdef":
|
||||
npos = add2(pos, d)
|
||||
npath = path + c
|
||||
if npos[0] >= 0 and npos[0] < 4 and npos[1] >= 0 and npos[1] < 4:
|
||||
poss.append((npos, npath))
|
||||
print(maxlen)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
solve()
|
||||
@@ -47,7 +47,10 @@ written in Python.
|
||||
- Day 12: 10:05 okay
|
||||
- Day 13: 9:43 okayish
|
||||
- Day 14: 120:00 struggled with this one (example incorrect?)
|
||||
- Day 15:
|
||||
- Day 15: Trial and error. Should use CRT instead.
|
||||
- Day 16: 14:11
|
||||
- Day 17: 45:00 didn't follow instructions... focus!
|
||||
- Day 18:
|
||||
|
||||
# 2017
|
||||
|
||||
|
||||
Reference in New Issue
Block a user