-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_13.py
56 lines (43 loc) · 1.6 KB
/
day_13.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
"""
--- Day 13: Point of Incidence ---
"""
import os
from typing import List
with open("13-test.in", "r", encoding="utf-8") as f:
test_patterns = [
[list(line) for line in block.split(os.linesep)]
for block in f.read().strip().split(f"{os.linesep}{os.linesep}")
]
def find_equal(grid: List[List[str]]) -> List[int]:
result = []
for (i1, r1), (i2, r2) in zip(enumerate(grid), enumerate(grid[1:], start=1)):
if r1 == r2:
result.append((i1, i2))
return result
assert find_equal([list(col) for col in zip(*test_patterns[0])]) == [(4, 5)]
assert find_equal(test_patterns[1]) == [(3, 4)]
def count_reflections(grid: List[List[str]], left: int, right: int):
current_left, current_right = left, right
while (
0 <= current_left
and current_right < len(grid)
and grid[current_left] == grid[current_right]
):
current_left, current_right = current_left - 1, current_right + 1
return left - current_left - 1
test_summarization = 0
for block in test_patterns:
rows_reflections = find_equal(block)
for top, bottom in rows_reflections:
print(count_reflections(block, top, bottom))
test_summarization += sum(
count_reflections(block, top, bottom) * 100 for top, bottom in rows_reflections
)
transposed_block = [list(col) for col in zip(*block)]
columns_reflections = find_equal(transposed_block)
test_summarization += sum(
count_reflections(transposed_block, left, right)
for left, right in columns_reflections
)
print(test_summarization)
assert test_summarization == 405