-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday25.py
executable file
·50 lines (34 loc) · 951 Bytes
/
day25.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
#!/usr/bin/env python3
import sys
from itertools import count
def evolve(grid):
h, w = len(grid), len(grid[0])
steps = 0
for steps in count(1):
advance = []
for r in range(h):
for c in range(w):
newc = (c + 1) % w
if grid[r][c] == '>' and grid[r][newc] == '.':
advance.append((r, c, newc))
for r, c, newc in advance:
grid[r][c] = '.'
grid[r][newc] = '>'
horiz_still = not advance
advance = []
for r in range(h):
for c in range(w):
newr = (r + 1) % h
if grid[r][c] == 'v' and grid[newr][c] == '.':
advance.append((r, c, newr))
if horiz_still and not advance:
break
for r, c, newr in advance:
grid[r][c] = '.'
grid[newr][c] = 'v'
return steps
# Open the first argument as input or use stdin if no arguments were given
fin = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin
grid = list(map(list, fin.read().split()))
ans = evolve(grid)
print('Part 1:', ans)