-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday11.py
More file actions
85 lines (78 loc) · 2.83 KB
/
day11.py
File metadata and controls
85 lines (78 loc) · 2.83 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def getAdjacentSeats(seats, seatRow, seatColumn):
adjacentSeats = []
for x in range(seatRow-1, seatRow+2):
for y in range(seatColumn-1, seatColumn+2):
if (x == seatRow and y == seatColumn) or (x < 0) or (y < 0): continue
try:
adjacentSeats.append(seats[x][y])
except IndexError:
continue
return adjacentSeats
def getVisibleSeat(seats, x, y, xDirection, yDirection):
visibleSeat = '.'
while visibleSeat == '.':
try:
x += xDirection
y += yDirection
if x < 0 or y < 0: break
visibleSeat = seats[x][y]
except IndexError:
break
return visibleSeat
def getAdjacentVisibleSeats(seats, seatRow, seatColumn):
adjacentVisibleSeats = []
for x in range(-1, 2):
for y in range(-1, 2):
if y == 0 and x == 0: continue
adjacentVisibleSeats.append(getVisibleSeat(seats, seatRow, seatColumn, x, y))
return adjacentVisibleSeats
def evaluateGeneration(seats, occupiedThreshold=4, visible=False):
newSeats = []
for seatRow in range(len(seats)):
newRow = ''
for seatColumn in range(len(seats[0])):
if visible: adjacentSeats = getAdjacentVisibleSeats(seats, seatRow, seatColumn)
else: adjacentSeats = getAdjacentSeats(seats, seatRow, seatColumn)
if seats[seatRow][seatColumn] == 'L':
countOccupied = adjacentSeats.count('#')
if countOccupied == 0:
newRow += '#'
else:
newRow += 'L'
elif seats[seatRow][seatColumn] == '#':
countOccupied = adjacentSeats.count('#')
if countOccupied >= occupiedThreshold:
newRow += 'L'
else:
newRow += '#'
elif seats[seatRow][seatColumn] == '.':
newRow += '.'
newSeats.append(newRow)
return newSeats
seats = [line.strip() for line in open('day11input.txt')]
consecutiveSameState = 0
oldSeats = seats[:]
while consecutiveSameState < 3:
newSeats = evaluateGeneration(oldSeats)
if newSeats == oldSeats:
consecutiveSameState +=1
del oldSeats[:]
oldSeats = newSeats[:]
del newSeats[:]
occupiedCount = 0
for seatRow in oldSeats:
occupiedCount += seatRow.count('#')
print(occupiedCount)
consecutiveSameState2 = 0
oldSeats2 = seats[:]
while consecutiveSameState2 < 3:
newSeats2 = evaluateGeneration(oldSeats2, 5, True)
if newSeats2 == oldSeats2:
consecutiveSameState2 +=1
del oldSeats2[:]
oldSeats2 = newSeats2[:]
del newSeats2[:]
occupiedCount2 = 0
for seatRow in oldSeats2:
occupiedCount2 += seatRow.count('#')
print(occupiedCount2)