This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
11_seating_system.py
77 lines (59 loc) · 1.98 KB
/
11_seating_system.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
##################################
# --- Day 11: Seating System --- #
##################################
import AOCUtils
moves8 = [(0, 1), (1, 0), (0, -1), (-1, 0),
(1, 1), (1, -1), (-1, 1), (-1, -1)]
##################################
originalSeats = [list(l) for l in AOCUtils.loadInput(11)]
seats = [l[:] for l in originalSeats]
h, w = len(seats), len(seats[0])
hasChanged = True
while hasChanged:
newSeats = [l[:] for l in seats]
hasChanged = False
for a in range(h):
for b in range(w):
n = 0
for da, db in moves8:
if da == 0 and db == 0: continue
if 0 <= a+da < h and 0 <= b+db < w:
if seats[a+da][b+db] == "#":
n += 1
if seats[a][b] == "L" and n == 0:
newSeats[a][b] = "#"
hasChanged = True
elif seats[a][b] == "#" and n >= 4:
newSeats[a][b] = "L"
hasChanged = True
seats = newSeats
p1 = sum(l.count("#") for l in seats)
print("Part 1: {}".format(p1))
seats = [l[:] for l in originalSeats]
h, w = len(seats), len(seats[0])
hasChanged = True
while hasChanged:
hasChanged = False
newSeats = [l[:] for l in seats]
for a in range(h):
for b in range(w):
n = 0
for da, db in moves8:
ca, cb = a, b
while 0 <= ca+da < h and 0 <= cb+db < w:
ca += da
cb += db
if seats[ca][cb] == "#":
n += 1
if seats[ca][cb] != ".":
break
if seats[a][b] == "L" and n == 0:
newSeats[a][b] = "#"
hasChanged = True
elif seats[a][b] == "#" and n >= 5:
newSeats[a][b] = "L"
hasChanged = True
seats = newSeats
p2 = sum(l.count("#") for l in seats)
print("Part 2: {}".format(p2))
AOCUtils.printTimeTaken()