Compare commits
2 commits
4b1fa6bd65
...
81c4073f32
| Author | SHA1 | Date | |
|---|---|---|---|
| 81c4073f32 | |||
| 8b6d89fec5 |
2 changed files with 97 additions and 0 deletions
51
2017-python/solutions/day_24.py
Normal file
51
2017-python/solutions/day_24.py
Normal 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()
|
||||
46
2017-python/solutions/day_25.py
Normal file
46
2017-python/solutions/day_25.py
Normal file
|
|
@ -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()
|
||||
Loading…
Add table
Reference in a new issue