50 lines
1.2 KiB
Python
50 lines
1.2 KiB
Python
from tools.aoc import AOCDay
|
|
from tools.coordinate import Coordinate
|
|
from tools.grid import Grid
|
|
from typing import Any
|
|
|
|
DIRS = {
|
|
"U": Coordinate(0, -1),
|
|
"D": Coordinate(0, 1),
|
|
"L": Coordinate(-1, 0),
|
|
"R": Coordinate(1, 0),
|
|
}
|
|
|
|
|
|
class Day(AOCDay):
|
|
inputs = [
|
|
[
|
|
("1985", "input2_test"),
|
|
("38961", "input2"),
|
|
],
|
|
[
|
|
("5DB3", "input2_test"),
|
|
("46C92", "input2"),
|
|
],
|
|
]
|
|
|
|
def get_code(self, keypad: Grid, pos: Coordinate) -> str:
|
|
code = ""
|
|
for line in self.getInput():
|
|
for move in line:
|
|
if keypad.get(pos + DIRS[move]):
|
|
pos += DIRS[move]
|
|
code += keypad.get(pos)
|
|
|
|
return code
|
|
|
|
def part1(self) -> Any:
|
|
keypad = Grid.from_data(["123", "456", "789"], default=None)
|
|
start = Coordinate(1, 1)
|
|
return self.get_code(keypad, start)
|
|
|
|
def part2(self) -> Any:
|
|
keypad = Grid.from_data([" 1", " 234", "56789", " ABC", " D"], default=None, translate={" ": None})
|
|
start = Coordinate(0, 2)
|
|
return self.get_code(keypad, start)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
day = Day(2016, 2)
|
|
day.run(verbose=True)
|