-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14.py
95 lines (83 loc) · 2.67 KB
/
14.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import numpy as np
import functools
from collections import Counter, deque, defaultdict
import re
def get_input():
with open('input', 'r') as file:
lines = file.readlines()
lowest = leftest = rightest = None
grid = defaultdict(lambda: '.')
for line in lines:
pts = [complex(*map(int, cd.split(','))) for cd in line.split(' -> ')]
for i in range(len(pts)):
if lowest is None or pts[i].imag > lowest:
lowest = int(pts[i].imag)
if leftest is None or pts[i].real < leftest:
leftest = int(pts[i].real)
if rightest is None or pts[i].real > rightest:
rightest = int(pts[i].real)
if i == len(pts)-1:
break
diff = pts[i+1] - pts[i]
dx, dy = map(int, [diff.real, diff.imag])
if np.sign(dx):
for step in range(0, dx + np.sign(dx), np.sign(dx)):
grid[pts[i]+step] = '#'
if np.sign(dy):
for step in range(0, dy + np.sign(dy), np.sign(dy)):
grid[pts[i]+step*1j] = '#'
return grid, lowest, rightest, leftest
def abyss(pos, lowest, rightest, leftest):
if pos.real > rightest or pos.real < leftest or pos.imag > lowest:
return True
return False
def solve(part):
grid, lowest, rightest, leftest = get_input()
sand = complex(500, 0)
quantity = 0
while not abyss(sand, lowest, rightest, leftest):
if grid[sand + 1j] == '.':
grid[sand] = '.'
sand += 1j
grid[sand] = 'o'
elif grid[sand -1+1j] == '.':
grid[sand] = '.'
sand += -1+1j
grid[sand] = 'o'
elif grid[sand + 1+1j] == '.':
grid[sand] = '.'
sand += 1+1j
grid[sand] = 'o'
else:
sand = complex(500, 0)
quantity += 1
print(quantity)
def p2():
grid, lowest, rightest, leftest = get_input()
for i in range(-10_000, 10_0001):
grid[i+(lowest+2)*1j] = '#'
print(grid[-1000+11j])
sand = complex(500, 0)
quantity = 0
while True:
if grid[sand + 1j] == '.':
grid[sand] = '.'
sand += 1j
grid[sand] = 'o'
elif grid[sand -1+1j] == '.':
grid[sand] = '.'
sand += -1+1j
grid[sand] = 'o'
elif grid[sand + 1+1j] == '.':
grid[sand] = '.'
sand += 1+1j
grid[sand] = 'o'
else:
quantity += 1
if sand == 500:
break
sand = complex(500, 0)
print(quantity)
if __name__ == '__main__':
solve(1)
p2()