aoc/2015/day03.py

65 lines
1.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
from typing import TextIO
grid: dict[tuple[int, int], bool] = {(0, 0): True}
instr: dict[str, tuple[int, int]] = {
">": (0, 1),
"<": (0, -1),
"^": (1, 0),
"v": (-1, 0),
}
f = open("day03.txt", "r")
directions = f.read().strip()
def part1() -> int:
houses = 1
current = (0, 0)
for char in directions:
current = (current[0] + instr[char][0], current[1] + instr[char][1])
if current not in grid:
grid[current] = True
houses += 1
return houses
def part2() -> int:
# reset grid
grid = {(0, 0): True}
houses = 1
current_santa = (0, 0)
current_robo = (0, 0)
for i in range(0, len(directions), 2):
current_santa = (
current_santa[0] + instr[directions[i]][0],
current_santa[1] + instr[directions[i]][1],
)
if current_santa not in grid:
grid[current_santa] = True
houses += 1
if i + 1 < len(directions):
current_robo = (
current_robo[0] + instr[directions[i + 1]][0],
current_robo[1] + instr[directions[i + 1]][1],
)
if current_robo not in grid:
grid[current_robo] = True
houses += 1
return houses
if __name__ == "__main__":
print(part1())
print(part2())