-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart_a.py
executable file
·182 lines (168 loc) · 5.22 KB
/
part_a.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python3
import functools
import utils
class Challenge(utils.BaseChallenge):
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
187
"""
return TreeMap.from_text(_input)\
.get_tree_count_on_slope((3, 1))
class TreeMap:
PARSE_MAP = {
'.': False,
'#': True,
}
@classmethod
def from_text(cls, tree_map_text):
"""
>>> TreeMap.from_text(
... "..#\\n"
... "#..\\n"
... ".#.\\n"
... ).lines
[[False, False, True], [True, False, False], [False, True, False]]
"""
lines = tree_map_text.splitlines()
non_empty_lines = filter(None, lines)
return cls([
list(map(cls.PARSE_MAP.__getitem__, line))
for line in non_empty_lines
])
def __init__(self, lines):
self.lines = lines
if self.lines:
self.line_length = len(self.lines[0])
else:
self.line_length = None
def get_product_of_tree_counts_on_slopes(self, slopes):
"""
>>> tree_map_b = TreeMap.from_text(
... "..##.......\\n"
... "#...#...#..\\n"
... ".#....#..#.\\n"
... "..#.#...#.#\\n"
... ".#...##..#.\\n"
... "..#.##.....\\n"
... ".#.#.#....#\\n"
... ".#........#\\n"
... "#.##...#...\\n"
... "#...##....#\\n"
... ".#..#...#.#\\n"
... )
>>> tree_map_b.get_product_of_tree_counts_on_slopes(
... [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)])
336
"""
return functools.reduce(int.__mul__, (
self.get_tree_count_on_slope(slope)
for slope in slopes
), 1)
def get_tree_counts_on_slopes(self, slopes):
"""
>>> tree_map_b = TreeMap.from_text(
... "..##.......\\n"
... "#...#...#..\\n"
... ".#....#..#.\\n"
... "..#.#...#.#\\n"
... ".#...##..#.\\n"
... "..#.##.....\\n"
... ".#.#.#....#\\n"
... ".#........#\\n"
... "#.##...#...\\n"
... "#...##....#\\n"
... ".#..#...#.#\\n"
... )
>>> tree_map_b.get_tree_counts_on_slopes(
... [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)])
[2, 7, 3, 4, 2]
"""
return [
self.get_tree_count_on_slope(slope)
for slope in slopes
]
def get_tree_count_on_slope(self, slope):
"""
>>> TreeMap([[False] * 3] * 3).get_tree_count_on_slope((1, 1))
0
>>> TreeMap([[True] * 3] * 3).get_tree_count_on_slope((1, 1))
3
>>> tree_map_a = TreeMap.from_text(
... "..#\\n"
... "#..\\n"
... ".#.\\n"
... )
>>> tree_map_a.get_tree_count_on_slope((1, 1))
0
>>> tree_map_a.get_tree_count_on_slope((2, 1))
1
>>> tree_map_a.get_tree_count_on_slope((1, 2))
1
>>> tree_map_b = TreeMap.from_text(
... "..##.......\\n"
... "#...#...#..\\n"
... ".#....#..#.\\n"
... "..#.#...#.#\\n"
... ".#...##..#.\\n"
... "..#.##.....\\n"
... ".#.#.#....#\\n"
... ".#........#\\n"
... "#.##...#...\\n"
... "#...##....#\\n"
... ".#..#...#.#\\n"
... )
>>> tree_map_b.get_tree_count_on_slope((3, 1))
7
>>> tree_map_b.get_tree_count_on_slope((1, 1))
2
>>> tree_map_b.get_tree_count_on_slope((5, 1))
3
>>> tree_map_b.get_tree_count_on_slope((7, 1))
4
>>> tree_map_b.get_tree_count_on_slope((1, 2))
2
"""
return self.get_tree_count_on_points(self.get_points_on_slope(slope))
def get_tree_count_on_points(self, points):
return sum(
1
for x, y in points
if self.lines[y][x]
)
def get_points_on_slope(self, slope):
"""
>>> tree_map_a = TreeMap.from_text(
... "..#\\n"
... "#..\\n"
... ".#.\\n"
... )
>>> tree_map_a.get_points_on_slope((1, 1))
[(0, 0), (1, 1), (2, 2)]
>>> tree_map_a.get_points_on_slope((2, 1))
[(0, 0), (2, 1), (1, 2)]
>>> tree_map_a.get_points_on_slope((1, 2))
[(0, 0), (1, 2)]
>>> tree_map_b = TreeMap.from_text(
... "..##.......\\n"
... "#...#...#..\\n"
... ".#....#..#.\\n"
... "..#.#...#.#\\n"
... ".#...##..#.\\n"
... "..#.##.....\\n"
... ".#.#.#....#\\n"
... ".#........#\\n"
... "#.##...#...\\n"
... "#...##....#\\n"
... ".#..#...#.#\\n"
... )
>>> tree_map_b.get_points_on_slope((3, 1))
[(0, 0), (3, 1), (6, 2), (9, 3), (1, 4), (4, 5), (7, 6), (10, 7), (2, 8), (5, 9), (8, 10)]
"""
delta_x, delta_y = slope
return [
((y // delta_y * delta_x) % self.line_length, y)
for y in range(0, len(self.lines), delta_y)
]
Challenge.main()
challenge = Challenge()