-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.py
70 lines (59 loc) · 2.12 KB
/
day13.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
import sys
if len(sys.argv) != 2:
print("Missing input file.")
sys.exit(1)
filename = sys.argv[1]
is_sample = filename == "sample.txt"
def parse():
with open(filename) as f:
return [[list(line) for line in block.strip().split("\n")]
for block in f.read().split("\n\n")]
def find_reflection_index(block, already_found_idx=None):
for i in range(1, len(block)):
if i == already_found_idx:
continue
distance_to_top = i
distance_to_bottom = len(block) - i
distance = min(distance_to_top, distance_to_bottom)
ok = True
for j in range(0, distance):
lower = i - j - 1
upper = i + j
assert lower >= 0
assert upper < len(block)
assert lower < upper, f"{lower} >= {upper}"
if block[lower] != block[upper]:
ok = False
continue
if ok:
return i
return 0
def calc(blocks):
part1 = 0
part2 = 0
for block in blocks:
row1 = find_reflection_index(block)
col1 = find_reflection_index(list(zip(*block)))
part1 += col1 + 100 * row1
W, H = len(block[0]), len(block)
for i in range(0, H):
for j in range(0, W):
block[i][j] = "." if block[i][j] == "#" else "#" # fix smudge
row2 = find_reflection_index(block, row1)
col2 = find_reflection_index(list(zip(*block)), col1)
block[i][j] = "." if block[i][j] == "#" else "#" # revert back
if row2 > 0 or col2 > 0:
part2 += col2 + 100 * row2
break
else:
continue
break
return part1, part2
def print_and_assert(part, expected, actual):
print(f"Part {part}: {actual}{' (sample)' if is_sample else ''}")
assert actual == expected, f"{part} was {actual}, expected {expected}"
if __name__ == '__main__':
blocks = parse()
part1, part2 = calc(blocks)
print_and_assert(1, 405 if is_sample else 33195, part1)
print_and_assert(2, 400 if is_sample else 31836, part2)