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
|
from sys import stdin
def birange(a, b):
if b < a: a, b = b, a
return range(a, b + 1)
solid = set()
for line in stdin:
parts = [[int(n) for n in s.split(',')] for s in line.split("->")]
for a, b in zip(parts, parts[1:]):
for x in birange(a[0], b[0]):
for y in birange(a[1], b[1]):
solid.add((x, y))
max_y = max([y for x,y in solid])
def partOne(s):
def drop(x, y):
while True:
while not (x, y+1) in s:
y += 1
if max_y < y:
return False
if not (x-1, y+1) in s:
x -= 1
y += 1
elif not (x+1, y+1) in s:
x += 1
y += 1
else:
s.add((x, y))
return True
n = 0
while drop(500, 0):
n += 1
return n
def partTwo(s):
total = 1
last = {500}
for y in range(1, max_y + 2):
cur = last.copy()
cur = cur.union(map(lambda x: x-1, last))
cur = cur.union(map(lambda x: x+1, last))
cur = set(filter(lambda x: not (x, y) in s, cur))
last = cur
total += len(cur)
return total
print(partOne(solid.copy()))
print(partTwo(solid.copy()))
|