advent-of-code/2017-python/solutions/day_19.py

64 lines
1.6 KiB
Python
Raw Normal View History

2021-11-01 16:40:46 +01:00
from solutions import BaseSolution
class Solution(BaseSolution):
2025-05-06 20:14:58 +02:00
input_file = "19.txt"
2021-11-01 16:40:46 +01:00
trim_input = False
def __str__(self):
2025-05-06 20:14:58 +02:00
return "Day 19: A Series of Tubes"
2021-11-01 16:40:46 +01:00
def _walk_maze(self, puzzle_input):
DIRECTIONS = {
2025-05-06 20:14:58 +02:00
"D": (1, 0),
"U": (-1, 0),
"R": (0, 1),
"L": (0, -1),
2021-11-01 16:40:46 +01:00
}
maze = puzzle_input.splitlines()
2025-05-06 20:14:58 +02:00
pos = (0, list(maze[0]).index("|"))
d = DIRECTIONS["D"]
paths = "-|"
2021-11-01 16:40:46 +01:00
steps = 0
2025-05-06 20:14:58 +02:00
seen = ""
2021-11-01 16:40:46 +01:00
def _nc(nu):
np = (pos[0] + nu[0], pos[1] + nu[1])
if np[0] < len(maze) and np[1] < len(maze[np[0]]):
return maze[np[0]][np[1]]
else:
2025-05-06 20:14:58 +02:00
return " "
2021-11-01 16:40:46 +01:00
while True:
pos = (pos[0] + d[0], pos[1] + d[1])
c = _nc((0, 0))
steps += 1
2025-05-06 20:14:58 +02:00
if c == "+":
2021-11-01 16:40:46 +01:00
nc = _nc(d)
2025-05-06 20:14:58 +02:00
if nc == " ":
2021-11-01 16:40:46 +01:00
for v in DIRECTIONS.values():
if -v[0] == d[0] and -v[1] == d[1]:
continue
nc = _nc(v)
2025-05-06 20:14:58 +02:00
if nc != " ":
2021-11-01 16:40:46 +01:00
d = v
break
2025-05-06 20:14:58 +02:00
elif c == " ":
2021-11-01 16:40:46 +01:00
break
elif c not in paths:
seen += c
return seen, steps
def solve(self, puzzle_input):
seen, _ = self._walk_maze(puzzle_input)
return seen
def solve_again(self, puzzle_input):
_, steps = self._walk_maze(puzzle_input)
return steps
2025-05-06 20:14:58 +02:00
if __name__ == "__main__":
2021-11-01 16:40:46 +01:00
solution = Solution()
solution.show_results()