-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.py
59 lines (38 loc) · 1.19 KB
/
day14.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
from aoc import *
from itertools import count
def parse_line(line):
points = mapl(integers, line.split(" -> "))
rock = set()
for (ax, ay), (bx, by) in zip(points, points[1:]):
for px in range(min(ax, bx), max(ax, bx)+1):
for py in range(min(ay, by), max(ay, by)+1):
rock.add(complex(px, py))
return rock
def parse_input(data):
rock = set()
for line in data:
rock |= parse_line(line)
return rock
START = 500
def solve(file=14):
def pour(pt, p1):
if pt in settled:
return pt.imag == floor
for dx in (0, -1, 1):
if pour(pt + dx+1j, p1) and p1:
return True
settled.add(pt)
count_sand = lambda settled: len(settled) - rock_count
settled = parse_input(read_input(file))
floor = int(max(pt.imag for pt in settled)) + 2
for x in range(START-floor, START+floor+1):
settled.add(complex(x, floor))
rock_count = len(settled)
pour(START, True)
p1 = count_sand(settled)
pour(START, False)
return p1, count_sand(settled)
print(solve())
def test():
assert solve("14_test") == (24, 93)
assert solve() == (1003, 25771)