-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday25_cucumbers.py
71 lines (49 loc) · 1.45 KB
/
day25_cucumbers.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
#!/usr/bin/env python
# coding: utf-8
import numpy as np
with open('day25_cucumbers.txt') as infile:
start_positions = [[s for s in row.strip()] for row in infile]
start_positions = np.array(start_positions)
# # Part 1
def sample(board, n=10):
print(board[:n, :n])
def move_east(board):
x, y = np.where(board=='>')
y_new = y+1
y_new[y_new==board.shape[1]] = 0
free_neigbour = (board[(x, y_new)] == '.')
east_free = (x[free_neigbour], y[free_neigbour])
east_next = (x[free_neigbour], y_new[free_neigbour])
n_changes = len(east_free[0])
board[east_free] = '.'
board[east_next] = '>'
return board, n_changes
def move_south(board):
x, y = np.where(board=='v')
x_new = x+1
x_new[x_new==board.shape[0]] = 0
free_neigbour = (board[(x_new, y)] == '.')
south_free = (x[free_neigbour], y[free_neigbour])
south_next = (x_new[free_neigbour], y[free_neigbour])
n_changes = len(south_free[0])
board[south_free] = '.'
board[south_next] = 'v'
return board, n_changes
def step(board):
board, n_changes_e = move_east(board)
board, n_changes_s = move_south(board)
if n_changes_e + n_changes_s > 0:
return board, False
else:
return board, True
board = start_positions.copy()
sample(board, 15)
finished = False
n_steps = 0
while not finished:
n_steps += 1
board, finished = step(board)
print()
sample(board, 15)
print()
print(n_steps, 'steps')