Solve 2017:24 "Electromagnetic Moat"

This commit is contained in:
Anders Englöf Ytterström 2025-05-06 23:06:16 +02:00
parent 4b1fa6bd65
commit 8b6d89fec5

View file

@ -0,0 +1,51 @@
from solutions import BaseSolution
import re
class Solution(BaseSolution):
input_file = "24.txt"
def __str__(self):
return "Day 24: Electromagnetic Moat"
def solve(self, puzzle_input):
p1, _ = self._solve(puzzle_input)
return p1
def solve_again(self, puzzle_input):
_, p2 = self._solve(puzzle_input)
return p2
def _solve(self, puzzle_input):
components = [
tuple(map(int, re.findall(r"\d+", line))) for line in puzzle_input.split()
]
Q = [(c, []) for c in components if 0 in c]
S = 0
L = 0
LS = 0
while Q:
c, s = Q.pop()
if c in s:
S = max(S, sum(x + y for x, y in s))
if len(s) >= L:
L = len(s)
LS = max(LS, L * 1_000_000 + sum(x + y for x, y in s))
continue
cc = set(c) if not s else set(c) - set(s[-1])
if not cc:
cc = set(c)
for p1, p2 in components:
if p1 > 0 and p1 in cc:
Q.append(((p1, p2), s + [c]))
if p2 > 0 and p2 in cc:
Q.append(((p1, p2), s + [c]))
return S, LS % 1_000_000
if __name__ == "__main__":
solution = Solution()
solution.show_results()