Skip to content

Commit d7bbe38

Browse files
committed
Add code to solve part 2 of day 11
1 parent 19819e6 commit d7bbe38

File tree

2 files changed

+76
-18
lines changed

2 files changed

+76
-18
lines changed

2020/day_11/Python/seats.py

+75-17
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def get_adjacent_seats(seat_layout, seat_location):
3939
return adjacent_seats
4040

4141

42-
# Add apply rules for part2
4342
# new function for get_visible_seats
4443
def get_visible_seats(seat_layout, seat_location):
4544
"""
@@ -48,31 +47,52 @@ def get_visible_seats(seat_layout, seat_location):
4847
and raster order.
4948
5049
"""
51-
seat_location_x, seat_location_y = seat_location
50+
seat_location_y, seat_location_x = seat_location
5251

5352
# find visible seats by loop
5453
visible_seats = ['.', '.', '.', '.', '.', '.', '.', '.']
5554

55+
grid_size_x = len(seat_layout[0])
56+
grid_size_y = len(seat_layout)
57+
5658
for n in range(1, max(len(seat_layout), len(seat_layout[0])) + 1):
5759
# stop condition?
58-
seat_x = max(seat_location_x - n, 0)
59-
seat_y = max(seat_location_y - n, 0)
60-
61-
for x in range(seat_x, seat_location_x + n + 1, n):
62-
for y in range(seat_y, seat_location_y + n + 1, n):
63-
if (x != seat_location_x or y != seat_location_y): # don't count the seat we're on!!
64-
try:
60+
# seat_x = max(seat_location_x - n, 0)
61+
# seat_y = max(seat_location_y - n, 0)
62+
63+
# Direction index is COLUMN first. So NW is 0, W is 1, SW is 2 etc.
64+
# 0 3 5
65+
#
66+
# 1 seat 6
67+
#
68+
# 2 4 7
69+
70+
direction_index = -1
71+
72+
for x in range(seat_location_x - n, seat_location_x + n + 1, n):
73+
for y in range(seat_location_y -n, seat_location_y + n + 1, n):
74+
# don't count the seat we're on!!
75+
if (x != seat_location_x or y != seat_location_y):
76+
direction_index += 1
77+
if seat_is_in_grid(grid_size_x, grid_size_y, x, y):
78+
#try:
6579
# only update visible seats if it's a floor
66-
direction_index = (n-1) % 8
6780
if visible_seats[direction_index] == '.':
68-
visible_seats[direction_index] = seat_layout[x][y]
69-
print(f"where we are: {x, y} seat layout: {seat_layout[x][y]}")
70-
except IndexError:
71-
pass
81+
visible_seats[direction_index] = seat_layout[y][x]
82+
#except IndexError:
83+
# pass
7284
if '.' not in visible_seats:
7385
return visible_seats
7486
return visible_seats
7587

88+
def seat_is_in_grid(grid_size_x, grid_size_y, seat_location_x, seat_location_y):
89+
'''returns true if seat is within the grid and False if it lies outside'''
90+
result = False
91+
if (0 <= seat_location_x < grid_size_x):
92+
if (0 <= seat_location_y < grid_size_y):
93+
result = True
94+
95+
return result
7696

7797
def apply_part1_rules(seat_layout):
7898
"""
@@ -91,13 +111,33 @@ def apply_part1_rules(seat_layout):
91111
new_seat = 'L'
92112
else:
93113
new_seat = seat
94-
#print(new_seat_layout[row_index])
95114
new_seat_layout[row_index][col_index] = new_seat
96115

97116
return new_seat_layout
98117

118+
# Add apply rules for part2
119+
def apply_part2_rules(seat_layout):
120+
"""
121+
Apply rules once to the seat configuration.
122+
"""
123+
new_seat_layout = copy.deepcopy(seat_layout)
124+
125+
for row_index, row in enumerate(seat_layout):
126+
127+
for col_index, seat in enumerate(row):
128+
visible_seats = get_visible_seats(seat_layout, (row_index, col_index))
129+
number_of_occupied_seats = visible_seats.count('#')
130+
if seat == 'L' and number_of_occupied_seats == 0:
131+
new_seat = '#'
132+
elif seat == '#' and number_of_occupied_seats >= 5:
133+
new_seat = 'L'
134+
else:
135+
new_seat = seat
136+
new_seat_layout[row_index][col_index] = new_seat
137+
138+
return new_seat_layout
99139

100-
def run(initial_seat_layout):
140+
def run_part1(initial_seat_layout):
101141
"""
102142
Apply rules until the configuration reaches 'equilbrium' - applying the rules again changes nothing
103143
"""
@@ -109,11 +149,29 @@ def run(initial_seat_layout):
109149
new_layout = apply_part1_rules(previous_layout)
110150
return new_layout
111151

152+
def run_part2(initial_seat_layout):
153+
"""
154+
Apply rules until the configuration reaches 'equilbrium' - applying the rules again changes nothing
155+
"""
156+
# iterate apply rules until seat layout doesn't change
157+
previous_layout = initial_seat_layout
158+
new_layout = apply_part2_rules(initial_seat_layout)
159+
count = 1
160+
while new_layout != previous_layout:
161+
previous_layout = new_layout
162+
new_layout = apply_part2_rules(previous_layout)
163+
print(f"looked at {count} layouts")
164+
count += 1
165+
return new_layout
166+
112167

113168
if __name__ == "__main__":
114169
# load initial seat layout and run
115170
initial_layout = loaders.load_string()
116171
initial_layout = process_puzzle_input(initial_layout)
117-
final_layout = run(initial_layout)
172+
final_layout = run_part1(initial_layout)
118173
number_of_seats = count_occupied_seats(final_layout)
119174
print(f'Part 1: {number_of_seats}')
175+
final_layout = run_part2(initial_layout)
176+
number_of_seats = count_occupied_seats(final_layout)
177+
print(f'Part 2: {number_of_seats}')

2020/day_11/Python/test_seats.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_get_adjacent_seats(seat_location, expected):
4343
"example_input,seat_location,expected",
4444
[
4545
[layouts.part2_example1, (4, 3), ["#", "#", "#", "#", "#", "#", "#", "#"]],
46-
[layouts.part2_example2, (1, 1), [".", ".", "L", ".", ".", ".", ".", "."]],
46+
[layouts.part2_example2, (1, 1), [".", ".", ".", ".", ".", ".", "L", "."]],
4747
[layouts.part2_example3, (3, 3), [".", ".", ".", ".", ".", ".", ".", "."]],
4848
],
4949
)

0 commit comments

Comments
 (0)