summary refs log tree commit diff stats
path: root/day25.py
blob: 3d1bcb1187b4f7c7feb43cb139458766d39236bb (plain) (blame)
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
#!/usr/bin/env python

import numpy as np
with open('day25.txt') as data:
    to_num = {
        '.': 0,
        '>': 1,
        'v': 3
    }
    cucumbers = np.stack([list(map(lambda x: to_num[x], list(line.strip()))) for line in data])

def shift(line, char):
    changed = True
    shifted = np.roll(line, 1)
    diff = line - shifted
    if np.count_nonzero(diff == -char) == 0:
        changed = False
    line[diff == -char] = char
    idx = np.nonzero(diff == -char)[0]
    idx -= 1
    line[idx] = 0
    return changed

count = 0
while True:
    count += 1
    changed = False
    for row in cucumbers[:]:
        changed |= shift(row, 1)
    for col in cucumbers.T:
        changed |= shift(col, 3)
    if not changed:
        print(count)
        break