73 lines
1.9 KiB
Python
73 lines
1.9 KiB
Python
from tools.aoc import AOCDay
|
|
from tools.coordinate import Coordinate
|
|
from tools.grid import Grid
|
|
from typing import Any
|
|
|
|
|
|
class Day(AOCDay):
|
|
inputs = [
|
|
[
|
|
('ABCDEF', "input19_test"),
|
|
('FEZDNIVJWT', "input19")
|
|
],
|
|
[
|
|
(38, "input19_test"),
|
|
(17200, "input19")
|
|
]
|
|
]
|
|
|
|
def getLineGrid(self) -> Grid:
|
|
grid = Grid()
|
|
for y, line in enumerate(self.getInput()):
|
|
for x, char in enumerate(line):
|
|
if char != ' ':
|
|
grid.set(Coordinate(x, y), char)
|
|
|
|
return grid
|
|
|
|
def walkMaze(self) -> (str, int):
|
|
maze = self.getLineGrid()
|
|
x = 0
|
|
for i in maze.rangeX():
|
|
if maze.get(Coordinate(i, 0)):
|
|
x = i
|
|
break
|
|
|
|
letters = ''
|
|
step_count = 1
|
|
start = Coordinate(x, 0)
|
|
last_pos = Coordinate(0, 0)
|
|
last_dir = Coordinate(0, 1)
|
|
neighbours = maze.getNeighboursOf(start, includeDiagonal=False) + ['None']
|
|
while len(neighbours) > 1:
|
|
step_count += 1
|
|
if maze.get(start + last_dir):
|
|
last_pos = start
|
|
start += last_dir
|
|
else:
|
|
if last_pos in neighbours:
|
|
neighbours.remove(last_pos)
|
|
last_pos = start
|
|
last_dir = neighbours[0] - start
|
|
start = neighbours[0]
|
|
|
|
if maze.get(start) in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz':
|
|
letters += maze.get(start)
|
|
|
|
neighbours = maze.getNeighboursOf(start, includeDiagonal=False)
|
|
|
|
return letters, step_count
|
|
|
|
def part1(self) -> Any:
|
|
letters, _ = self.walkMaze()
|
|
return letters
|
|
|
|
def part2(self) -> Any:
|
|
_, steps = self.walkMaze()
|
|
return steps
|
|
|
|
|
|
if __name__ == '__main__':
|
|
day = Day(2017, 19)
|
|
day.run(verbose=True)
|