-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathday11hv.py
37 lines (31 loc) · 961 Bytes
/
day11hv.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
from benchy import minibench
lines = open("day11.txt").read().split("\n")
def compute(cosmic_constant):
# initializers for blank space detection
vexp = [cosmic_constant] * len(lines)
hexp = [cosmic_constant] * len(lines[0])
vcnt = [0] * len(lines)
hcnt = [0] * len(lines[0])
# find empty lines and count stars at each x and y separately
for i in range(len(lines)):
for j in range(len(lines[i])):
if lines[i][j] == '#':
vexp[i]=hexp[j]=1
vcnt[i]+=1
hcnt[j]+=1
sum = 0
for (a,b) in [(hcnt,hexp),(vcnt,vexp)]:
pos = cnt = dst = 0
for i in range(len(a)):
cnt += a[i]
dst += a[i]*pos
sum += a[i]*(cnt*pos - dst)
pos += b[i]
return sum
def part1():
return compute(2)
def part2():
return compute(1000000)
print(part1())
print(part2())
minibench({"part1": part1, "part2": part2})