Skip to content

Commit 8e4c9f7

Browse files
committed
Add day 14
1 parent 754d4df commit 8e4c9f7

File tree

5 files changed

+183
-1
lines changed

5 files changed

+183
-1
lines changed

2022/14/14.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from sys import stdin
2+
lines = [[list(map(int, c.split(','))) for c in l.strip().split(" -> ")] for l in stdin.readlines()]
3+
width = max(x+200 for x, _ in sum(lines, []))
4+
height = max(y for _, y in sum(lines, []))
5+
6+
def solve(floor='.'):
7+
field = [['.'] * width for _ in range(height + 2)] + [[floor] * width]
8+
for walls in lines:
9+
for (x1, y1), (x2, y2) in zip(walls, walls[1:]):
10+
for x in range(min(x1, x2), max(x1, x2)+1):
11+
for y in range(min(y1, y2), max(y1, y2)+1):
12+
field[y][x] = '#'
13+
14+
while True:
15+
sx, sy = 500, 0
16+
while sy+1 < len(field):
17+
for xa in (0, -1, 1):
18+
if field[sy+1][sx+xa] == '.':
19+
sy, sx = sy+1, sx+xa
20+
break
21+
else:
22+
field[sy][sx] = 'o'
23+
break
24+
if sy+1 >= len(field) or field[0][500] == 'o':
25+
return sum(field, []).count("o")
26+
27+
print(solve(floor='.'))
28+
print(solve(floor='#'))

2022/14/input.ans

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
793
2+
24166

0 commit comments

Comments
 (0)