61 lines
1.5 KiB
Python
61 lines
1.5 KiB
Python
import hashlib
|
|
from collections import deque
|
|
|
|
from tools.aoc import AOCDay
|
|
from typing import Any
|
|
|
|
|
|
class Day(AOCDay):
|
|
inputs = [
|
|
[
|
|
("DDRRRD", "input17_test"),
|
|
("DDUDRLRRUDRD", "input17_test2"),
|
|
("DRURDRUDDLLDLUURRDULRLDUUDDDRR", "input17_test3"),
|
|
("DRRDRLDURD", "input17"),
|
|
],
|
|
[
|
|
(370, "input17_test"),
|
|
(492, "input17_test2"),
|
|
(830, "input17_test3"),
|
|
(618, "input17"),
|
|
]
|
|
]
|
|
|
|
dirs = {
|
|
(0, -1): "U",
|
|
(0, 1): "D",
|
|
(-1, 0): "L",
|
|
(1, 0): "R",
|
|
}
|
|
doors = list(dirs.keys())
|
|
|
|
def traverse(self, p2: bool = False) -> int | str:
|
|
passcode = self.getInput()
|
|
q = deque([(0, 0, "")])
|
|
best = 0
|
|
while q:
|
|
x, y, path = q.popleft()
|
|
if x == 3 and y == 3:
|
|
if not p2:
|
|
return path
|
|
best = max(best, len(path))
|
|
continue
|
|
key = hashlib.md5((passcode + path).encode()).hexdigest()
|
|
for i, (dx, dy) in enumerate(self.doors):
|
|
if key[i] in "bcdef":
|
|
if 0 <= x + dx <= 3 and 0 <= y + dy <= 3:
|
|
q.append((x + dx, y + dy, path + self.dirs[(dx, dy)]))
|
|
|
|
return best
|
|
|
|
def part1(self) -> Any:
|
|
return self.traverse()
|
|
|
|
def part2(self) -> Any:
|
|
return self.traverse(True)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
day = Day(2016, 17)
|
|
day.run(verbose=True)
|