41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
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()
|