@@ -39,7 +39,6 @@ def get_adjacent_seats(seat_layout, seat_location):
39
39
return adjacent_seats
40
40
41
41
42
- # Add apply rules for part2
43
42
# new function for get_visible_seats
44
43
def get_visible_seats (seat_layout , seat_location ):
45
44
"""
@@ -48,31 +47,52 @@ def get_visible_seats(seat_layout, seat_location):
48
47
and raster order.
49
48
50
49
"""
51
- seat_location_x , seat_location_y = seat_location
50
+ seat_location_y , seat_location_x = seat_location
52
51
53
52
# find visible seats by loop
54
53
visible_seats = ['.' , '.' , '.' , '.' , '.' , '.' , '.' , '.' ]
55
54
55
+ grid_size_x = len (seat_layout [0 ])
56
+ grid_size_y = len (seat_layout )
57
+
56
58
for n in range (1 , max (len (seat_layout ), len (seat_layout [0 ])) + 1 ):
57
59
# 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:
65
79
# only update visible seats if it's a floor
66
- direction_index = (n - 1 ) % 8
67
80
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
72
84
if '.' not in visible_seats :
73
85
return visible_seats
74
86
return visible_seats
75
87
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
76
96
77
97
def apply_part1_rules (seat_layout ):
78
98
"""
@@ -91,13 +111,33 @@ def apply_part1_rules(seat_layout):
91
111
new_seat = 'L'
92
112
else :
93
113
new_seat = seat
94
- #print(new_seat_layout[row_index])
95
114
new_seat_layout [row_index ][col_index ] = new_seat
96
115
97
116
return new_seat_layout
98
117
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
99
139
100
- def run (initial_seat_layout ):
140
+ def run_part1 (initial_seat_layout ):
101
141
"""
102
142
Apply rules until the configuration reaches 'equilbrium' - applying the rules again changes nothing
103
143
"""
@@ -109,11 +149,29 @@ def run(initial_seat_layout):
109
149
new_layout = apply_part1_rules (previous_layout )
110
150
return new_layout
111
151
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
+
112
167
113
168
if __name__ == "__main__" :
114
169
# load initial seat layout and run
115
170
initial_layout = loaders .load_string ()
116
171
initial_layout = process_puzzle_input (initial_layout )
117
- final_layout = run (initial_layout )
172
+ final_layout = run_part1 (initial_layout )
118
173
number_of_seats = count_occupied_seats (final_layout )
119
174
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 } ' )
0 commit comments