day17
This commit is contained in:
parent
070acc8433
commit
b6e46076bb
60
day17.py
Normal file
60
day17.py
Normal file
@ -0,0 +1,60 @@
|
||||
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)
|
||||
1
inputs/input17
Normal file
1
inputs/input17
Normal file
@ -0,0 +1 @@
|
||||
pvhmgsws
|
||||
1
inputs/input17_test
Normal file
1
inputs/input17_test
Normal file
@ -0,0 +1 @@
|
||||
ihgpwlah
|
||||
1
inputs/input17_test2
Normal file
1
inputs/input17_test2
Normal file
@ -0,0 +1 @@
|
||||
kglvqrro
|
||||
1
inputs/input17_test3
Normal file
1
inputs/input17_test3
Normal file
@ -0,0 +1 @@
|
||||
ulqzkmiv
|
||||
Loading…
Reference in New Issue
Block a user