-
Notifications
You must be signed in to change notification settings - Fork 1
/
day03_trees.py
96 lines (74 loc) · 2.23 KB
/
day03_trees.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import math
from typing import Set, NamedTuple
class Square(NamedTuple):
row: int
column: int
class Grid(NamedTuple):
rows: int
columns: int
trees: Set[Square]
def create_grid(lines):
trees = set()
for row, line in enumerate(lines):
line = line.strip()
for col, square in enumerate(line):
if square == "#":
trees.add(Square(row, col))
return Grid(rows=len(lines), columns=len(line), trees=trees)
def number_of_trees_hit(grid: Grid, right: int, down: int) -> int:
current_square = Square(row=0, column=0)
num_trees_hit = 0
while current_square.row < grid.rows:
if (
Square(row=current_square.row, column=current_square.column % grid.columns)
in grid.trees
):
num_trees_hit += 1
current_square = Square(
row=current_square.row + down, column=current_square.column + right
)
return num_trees_hit
def test_number_of_trees_hit():
TEST_INPUT = """..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#"""
grid = create_grid(TEST_INPUT.split("\n"))
trees_hit = number_of_trees_hit(grid, right=3, down=1)
assert trees_hit == 7
def test_part_two():
TEST_INPUT = """..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#"""
grid = create_grid(TEST_INPUT.split("\n"))
trees_hit = []
for (right, down) in [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]:
hit = number_of_trees_hit(grid, right, down)
trees_hit.append(hit)
return math.prod(trees_hit) == 336
if __name__ == "__main__":
with open("2020/data/day03_input.txt") as f:
grid = create_grid(f.readlines())
trees_hit = number_of_trees_hit(grid, right=3, down=1)
print(f"Part 1 answer is {trees_hit}")
trees_hit = []
for (right, down) in [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]:
hit = number_of_trees_hit(grid, right, down)
trees_hit.append(hit)
result = math.prod(trees_hit)
print(f"Part 2 answer is {result}")