-
Notifications
You must be signed in to change notification settings - Fork 1
/
14-1.py
executable file
·62 lines (52 loc) · 1.67 KB
/
14-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
#!/usr/bin/env python
import numpy
class Dish:
def __init__(self, lines):
self.height = len(lines)
self.width = len(lines[0])
self.map = numpy.zeros((self.height, self.width), dtype=numpy.uint8)
for y in range(self.height):
for x in range(self.width):
if lines[y][x] == 'O':
self.map[y, x] = 1
elif lines[y][x] == '#':
self.map[y, x] = 2
def load(self):
load = 0
for y in range(self.height):
for x in range(self.width):
if self.map[y, x] == 1:
load += self.height - y
return load
def move_north(self, start_y, x):
end_y = start_y
for check_y in range((start_y - 1), -1, -1):
if self.map[check_y, x] != 0:
break
end_y = check_y
self.map[start_y, x] = 0
self.map[end_y, x] = 1
return end_y
def tilt_north(self):
for y in range(self.height):
for x in range(self.width):
if self.map[y, x] == 1:
self.move_north(y, x)
def __str__(self):
s = f"<{self.__class__.__name__}:\n"
for y in range(self.height):
for x in range(self.width):
match self.map[y, x]:
case 0:
s += "."
case 1:
s += "O"
case 2:
s += "#"
s += "\n"
s += ">"
return s
lines = [line.strip() for line in open('14.input').readlines()]
dish = Dish(lines)
dish.tilt_north()
print(dish.load())