-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25-1.py
executable file
·95 lines (71 loc) · 2.37 KB
/
25-1.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
#!/usr/bin/env python
import numpy
class Map:
def __init__(self, lines):
self.height = len(lines)
self.width = len(lines[0])
self.map = numpy.zeros((self.height, self.width), dtype=numpy.uint)
for row in range(self.height):
for col in range(self.width):
self.map[row, col] = self.decode(lines[row][col])
def decode(self, symbol):
if symbol == ".":
return 0
elif symbol == '>':
return 1
elif symbol == 'v':
return 2
def encode(self, number):
if number == 0:
return "."
elif number == 1:
return ">"
elif number == 2:
return "v"
def step(self):
changed = False
new_map = numpy.zeros((self.height, self.width), dtype=numpy.uint)
for y in range(self.height):
for x in range(self.width):
if self.map[y, x] == 2:
new_map[y, x] = 2
if self.map[y, x] != 1:
continue
if self.map[y, (x + 1) % self.width] != 0:
new_map[y, x] = 1
else:
new_map[y, x] = 0
new_map[y, (x + 1) % self.width] = 1
changed = True
self.map = new_map
new_map = numpy.zeros((self.height, self.width), dtype=numpy.uint)
for x in range(self.width):
for y in range(self.height):
if self.map[y, x] == 1:
new_map[y, x] = 1
if self.map[y, x] != 2:
continue
if self.map[(y + 1) % self.height, x] != 0:
new_map[y, x] = 2
else:
new_map[y, x] = 0
new_map[(y + 1) % self.height, x] = 2
changed = True
self.map = new_map
return changed
def __str__(self):
s = f"<{self.__class__.__name__}: (height: {self.height}, width: {self.width})\n"
for y in range(self.height):
for x in range(self.width):
s += self.encode(self.map[y, x])
s += "\n"
s += '>'
return s
lines = [list(line.strip()) for line in open('25.input').readlines()]
map = Map(lines)
steps = 0
while True:
steps += 1
if not map.step():
break
print(steps)