-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommon.py
More file actions
71 lines (52 loc) · 1.77 KB
/
Copy pathcommon.py
File metadata and controls
71 lines (52 loc) · 1.77 KB
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
import copy
import time
from typing import Callable, List
open_seat = "L"
filled_seat = "#"
no_seat = "."
lookup_positions = [
(0, 1),
(1, 1),
(1, 0),
(1, -1),
(0, -1),
(-1, -1),
(-1, 0),
(-1, 1),
]
def parse(instr: str) -> List[List[str]]:
return [[char for char in x] for x in instr.strip().split("\n")]
def iterate(
current_hall: List[List[str]], neighbour_counter: Callable, get_new_state: Callable
) -> List[List[str]]:
# Copy hall
next_hall = copy.deepcopy(current_hall) # this is bloody slow
hall_size = (len(next_hall[0]), len(next_hall))
# Iterate each chair space
for col in range(hall_size[0]):
for row in range(hall_size[1]):
current_pos = current_hall[row][col]
# If it's the floor, there's nothing we can do with this spot
if current_pos == no_seat:
continue
# Count number of adjacent seats
num_neighbours = neighbour_counter(current_hall, (row, col), hall_size)
# Execute rules on copied list based on that count
next_hall[row][col] = get_new_state(num_neighbours, next_hall[row][col])
# Return copied list
return next_hall
def run(
current_state: List[List[str]], neighbour_counter: Callable, get_new_state: Callable
) -> int:
last_state = None
while current_state != last_state:
last_state = current_state
current_state = iterate(current_state, neighbour_counter, get_new_state)
# at this point, we've ended up with two identical seating arrangements
# let's count the number of occupied seats
total_occupied = 0
for a in current_state:
for b in a:
if b == filled_seat:
total_occupied += 1
return total_occupied