-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday11.py
executable file
·40 lines (29 loc) · 950 Bytes
/
day11.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
#!/usr/bin/env python3
import sys
def sum_distances(counts, multiplier):
total = partial_sum = previous = space = 0
for n in counts:
if n:
total += n * (previous * space - partial_sum)
partial_sum += n * space
previous += n
space += 1
else:
space += multiplier
return total
def solve(row_counts, col_counts, multiplier):
return sum_distances(row_counts, multiplier) + sum_distances(col_counts, multiplier)
# Open the first argument as input or use stdin if no arguments were given
fin = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin
grid = list(map(str.rstrip, fin))
row_counts = [0] * len(grid)
col_counts = [0] * len(grid[0])
for r, row in enumerate(grid):
for c, cell in enumerate(row):
if cell == '#':
row_counts[r] += 1
col_counts[c] += 1
total1 = solve(row_counts, col_counts, 2)
print('Part 1:', total1)
total2 = solve(row_counts, col_counts, 1000000)
print('Part 2:', total2)