From b6e46076bb1a065a024b159c6ff56dca4e8140a2 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Mon, 30 Dec 2024 12:45:55 +0100 Subject: [PATCH] day17 --- day17.py | 60 ++++++++++++++++++++++++++++++++++++++++++++ inputs/input17 | 1 + inputs/input17_test | 1 + inputs/input17_test2 | 1 + inputs/input17_test3 | 1 + 5 files changed, 64 insertions(+) create mode 100644 day17.py create mode 100644 inputs/input17 create mode 100644 inputs/input17_test create mode 100644 inputs/input17_test2 create mode 100644 inputs/input17_test3 diff --git a/day17.py b/day17.py new file mode 100644 index 0000000..c94a747 --- /dev/null +++ b/day17.py @@ -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) diff --git a/inputs/input17 b/inputs/input17 new file mode 100644 index 0000000..76432d7 --- /dev/null +++ b/inputs/input17 @@ -0,0 +1 @@ +pvhmgsws diff --git a/inputs/input17_test b/inputs/input17_test new file mode 100644 index 0000000..891e79b --- /dev/null +++ b/inputs/input17_test @@ -0,0 +1 @@ +ihgpwlah \ No newline at end of file diff --git a/inputs/input17_test2 b/inputs/input17_test2 new file mode 100644 index 0000000..c8cc26e --- /dev/null +++ b/inputs/input17_test2 @@ -0,0 +1 @@ +kglvqrro \ No newline at end of file diff --git a/inputs/input17_test3 b/inputs/input17_test3 new file mode 100644 index 0000000..a4f29b6 --- /dev/null +++ b/inputs/input17_test3 @@ -0,0 +1 @@ +ulqzkmiv \ No newline at end of file