65 lines
1.7 KiB
Python
65 lines
1.7 KiB
Python
from tools.aoc import AOCDay
|
|
from tools.coordinate import Coordinate, DistanceAlgorithm
|
|
from typing import Any
|
|
|
|
DIRS = [Coordinate(0, -1), Coordinate(1, 0), Coordinate(0, 1), Coordinate(-1, 0)]
|
|
|
|
|
|
class Day(AOCDay):
|
|
inputs = [
|
|
[
|
|
(12, "input1_test"),
|
|
(146, "input1"),
|
|
],
|
|
[
|
|
(131, "input1"),
|
|
],
|
|
]
|
|
|
|
def parse_input(self) -> list[tuple[int, int]]:
|
|
instructions = []
|
|
for inst in self.getInput().split(", "):
|
|
turn = 1 if inst[0] == "R" else -1
|
|
dist = int(inst[1:])
|
|
instructions.append((turn, dist))
|
|
|
|
return instructions
|
|
|
|
def part1(self) -> Any:
|
|
instructions = self.parse_input()
|
|
facing = 0
|
|
cur_pos = Coordinate(0, 0)
|
|
|
|
for turn, dist in instructions:
|
|
facing = (facing + turn) % 4
|
|
cur_pos += DIRS[facing] * dist
|
|
|
|
return cur_pos.getDistanceTo(
|
|
Coordinate(0, 0),
|
|
algorithm=DistanceAlgorithm.MANHATTAN,
|
|
includeDiagonals=False,
|
|
)
|
|
|
|
def part2(self) -> Any:
|
|
instructions = self.parse_input()
|
|
facing = 0
|
|
cur_pos = Coordinate(0, 0)
|
|
seen = set()
|
|
|
|
for turn, dist in instructions:
|
|
facing = (facing + turn) % 4
|
|
for _ in range(dist):
|
|
cur_pos += DIRS[facing]
|
|
if cur_pos in seen:
|
|
return cur_pos.getDistanceTo(
|
|
Coordinate(0, 0),
|
|
algorithm=DistanceAlgorithm.MANHATTAN,
|
|
includeDiagonals=False,
|
|
)
|
|
seen.add(cur_pos)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
day = Day(2016, 1)
|
|
day.run(verbose=True)
|