From 81c4073f32053644800834932162580f194d92c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Tue, 6 May 2025 23:53:56 +0200 Subject: [PATCH] Solve 2017:25 "The Halting Problem" --- 2017-python/solutions/day_25.py | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 2017-python/solutions/day_25.py diff --git a/2017-python/solutions/day_25.py b/2017-python/solutions/day_25.py new file mode 100644 index 0000000..201bda3 --- /dev/null +++ b/2017-python/solutions/day_25.py @@ -0,0 +1,46 @@ +from solutions import BaseSolution +from collections import defaultdict +import re + + +class Solution(BaseSolution): + input_file = "25.txt" + + def __str__(self): + return "Day 25: The Halting Problem" + + def solve(self, puzzle_input): + T = defaultdict(int) + p = 0 + + hd, *states = puzzle_input.split("\n\n") + S = int(re.findall(r"(\d+) steps", hd)[0]) + s = re.findall(r"state (\w)\.", hd)[0] + P = {} + for state in states: + k = re.findall(r"state (\w)\:", state)[0] + v = [int(n) for n in re.findall(r"value (\d)\.", state)] + d = re.findall(r"(left|right)\.", state) + t = re.findall(r"state (\w)\.", state) + P[k] = (v[0], d[0], t[0], v[1], d[1], t[1]) + + for _ in range(S): + v0, d0, s0, v1, d1, s1 = P[s] + match T[p]: + case 0: + T[p] = v0 + s = s0 + p += 1 if d0 == "right" else -1 + case 1: + T[p] = v1 + s = s1 + p += 1 if d1 == "right" else -1 + return sum(T.values()) + + def solve_again(self, puzzle_input): + return "*" + + +if __name__ == "__main__": + solution = Solution() + solution.show_results()