-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.py
executable file
·150 lines (122 loc) · 5.58 KB
/
day11.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from abc import abstractmethod
from copy import deepcopy
from typing import Tuple
FLOOR = '.'
OCCUPIED = '#'
EMPTY = 'L'
EDGE = 'X'
class Ferry:
seats_map = None
def __init__(self):
self._provisioning = deepcopy(Ferry.seats_map)
self._width = len(Ferry.seats_map[0])
self._height = len(Ferry.seats_map)
self._stablized = False
self._seat_count = 0
@staticmethod
def print():
for row in Ferry.seats_map:
print(row)
print()
def update(self, tolerance=4):
dirty = False
for row in range(0, self._height):
for seat in range(0, self._width):
if Ferry.seats_map[row][seat] == FLOOR:
continue
surroundings = self.get_surroundings((row, seat))
if Ferry.seats_map[row][seat] == EMPTY:
if surroundings.count(OCCUPIED) == 0:
self._provisioning[row][seat] = OCCUPIED
self._seat_count += 1
dirty = True
elif Ferry.seats_map[row][seat] == OCCUPIED:
if surroundings.count(OCCUPIED) >= tolerance:
self._provisioning[row][seat] = EMPTY
self._seat_count -= 1
dirty = True
if dirty:
Ferry.seats_map = deepcopy(self._provisioning)
else:
self._stablized = True
@abstractmethod
def get_surroundings(self, index: Tuple[int, int]) -> list:
pass
@property
def stablized(self) -> bool:
return self._stablized
@property
def seat_count(self) -> int:
return self._seat_count
class Part1(Ferry):
def get_surroundings(self, index: Tuple[int, int]) -> list:
row, seat = index
surroundings = [None] * 8
surroundings[0] = Ferry.seats_map[row-1][seat-1] if row - 1 >= 0 and seat - 1 >= 0 else None
surroundings[1] = Ferry.seats_map[row-1][seat] if row - 1 >= 0 else None
surroundings[2] = Ferry.seats_map[row-1][seat+1] if row - 1 >= 0 and seat + 1 < self._width else None
surroundings[3] = Ferry.seats_map[row][seat+1] if seat + 1 < self._width else None
surroundings[4] = Ferry.seats_map[row+1][seat+1] if row + 1 < self._height and seat + 1 < self._width else None
surroundings[5] = Ferry.seats_map[row+1][seat] if row + 1 < self._height else None
surroundings[6] = Ferry.seats_map[row+1][seat-1] if row + 1 < self._height and seat - 1 >= 0 else None
surroundings[7] = Ferry.seats_map[row][seat-1] if seat - 1 >= 0 else None
return surroundings
class Part2(Ferry):
def get_surroundings(self, index: Tuple[int, int]) -> list:
row, seat = index
surroundings = [None] * 8
reach = 1
while surroundings.count(None) > 0:
if row-reach >= 0 and seat-reach >= 0 and surroundings[0] is None:
if Ferry.seats_map[row-reach][seat-reach] != FLOOR:
surroundings[0] = Ferry.seats_map[row-reach][seat-reach]
elif surroundings[0] is None:
surroundings[0] = EDGE
if row - reach >= 0 and not surroundings[1]:
if Ferry.seats_map[row-reach][seat] != FLOOR:
surroundings[1] = Ferry.seats_map[row-reach][seat]
elif surroundings[1] is None:
surroundings[1] = EDGE
if row - reach >= 0 and seat + reach < self._width and surroundings[2] is None:
if Ferry.seats_map[row-reach][seat+reach] != FLOOR:
surroundings[2] = Ferry.seats_map[row-reach][seat+reach]
elif surroundings[2] is None:
surroundings[2] = EDGE
if seat + reach < self._width and not surroundings[3]:
if Ferry.seats_map[row][seat+reach] != FLOOR:
surroundings[3] = Ferry.seats_map[row][seat+reach]
elif surroundings[3] is None:
surroundings[3] = EDGE
if row + reach < self._height and seat + reach < self._width and surroundings[4] is None:
if Ferry.seats_map[row+reach][seat+reach] != FLOOR:
surroundings[4] = Ferry.seats_map[row+reach][seat+reach]
elif surroundings[4] is None:
surroundings[4] = EDGE
if row + reach < self._height and surroundings[5] is None:
if Ferry.seats_map[row+reach][seat] != FLOOR:
surroundings[5] = Ferry.seats_map[row+reach][seat]
elif surroundings[5] is None:
surroundings[5] = EDGE
if row + reach < self._height and seat - reach >= 0 and surroundings[6] is None:
if Ferry.seats_map[row+reach][seat-reach] != FLOOR:
surroundings[6] = Ferry.seats_map[row+reach][seat-reach]
elif surroundings[6] is None:
surroundings[6] = EDGE
if seat - reach >= 0 and surroundings[7] is None:
if Ferry.seats_map[row][seat-reach] != FLOOR:
surroundings[7] = Ferry.seats_map[row][seat-reach]
elif surroundings[7] is None:
surroundings[7] = EDGE
reach += 1
return surroundings
if __name__ == '__main__':
with open('test.txt', 'r') as f:
Ferry.seats_map = [list(row.strip()) for row in f.readlines()]
# ferry = Part1()
# while not ferry.stablized:
# ferry.update(tolerance=4)
# print(ferry.seat_count)
ferry = Part2()
while not ferry.stablized:
ferry.update(tolerance=5)
print(ferry.seat_count)