Skip to content

Commit 0266c0d

Browse files
committed
Added solutions for day 11
1 parent 2d29721 commit 0266c0d

File tree

3 files changed

+138
-1
lines changed

3 files changed

+138
-1
lines changed

advent2020/day11.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2020 Andrew Krepps
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
24+
from . import util
25+
26+
27+
def search_filled_seat(grid, row, col, row_dir, col_dir, search_lim):
28+
distance = 0
29+
while True:
30+
distance += 1
31+
if search_lim is not None and distance > search_lim:
32+
return False
33+
search_row = row + row_dir*distance
34+
search_col = col + col_dir*distance
35+
if search_row not in range(len(grid)) or search_col not in range(len(grid[0])):
36+
return False
37+
if grid[search_row][search_col] == '#':
38+
return True
39+
if grid[search_row][search_col] == 'L':
40+
return False
41+
42+
43+
def count_adjacent_filled_seats(grid, row, col, search_lim):
44+
count = 0
45+
for row_dir in range(-1, 2):
46+
for col_dir in range(-1, 2):
47+
if (row_dir, col_dir) != (0, 0) and search_filled_seat(grid, row, col, row_dir, col_dir, search_lim):
48+
count += 1
49+
return count
50+
51+
52+
def run_seat_simulation(seat_grid, search_lim, vacate_threshold):
53+
grid1 = [[cell for cell in row] for row in seat_grid]
54+
grid2 = [['X' for col in row] for row in grid1]
55+
while grid1 != grid2:
56+
for row in range(len(grid1)):
57+
for col in range(len(grid1[0])):
58+
adjacent_count = count_adjacent_filled_seats(grid1, row, col, search_lim)
59+
if grid1[row][col] == 'L' and adjacent_count == 0:
60+
grid2[row][col] = '#'
61+
elif grid1[row][col] == '#' and adjacent_count >= vacate_threshold:
62+
grid2[row][col] = 'L'
63+
else:
64+
grid2[row][col] = grid1[row][col]
65+
grid1, grid2 = grid2, grid1
66+
return grid1
67+
68+
69+
def count_filled_seats(seat_grid):
70+
return sum([sum([1 for col in row if col == '#']) for row in seat_grid])
71+
72+
73+
def get_part1_answer(seat_grid):
74+
stable_grid = run_seat_simulation(seat_grid, search_lim=1, vacate_threshold=4)
75+
return count_filled_seats(stable_grid)
76+
77+
78+
def get_part2_answer(seat_grid):
79+
stable_grid = run_seat_simulation(seat_grid, search_lim=None, vacate_threshold=5)
80+
return count_filled_seats(stable_grid)
81+
82+
83+
def run():
84+
with open(util.get_input_file_path("day11.txt")) as f:
85+
seat_grid = [line.strip() for line in f if len(line.strip()) > 0]
86+
print(f"The answer to part 1 is {get_part1_answer(seat_grid)}")
87+
print(f"The answer to part 2 is {get_part2_answer(seat_grid)}")

main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from advent2020 import day8
3434
from advent2020 import day9
3535
from advent2020 import day10
36+
from advent2020 import day11
3637

3738

3839
day_runners = [
@@ -45,7 +46,8 @@
4546
lambda: day7.run(),
4647
lambda: day8.run(),
4748
lambda: day9.run(),
48-
lambda: day10.run()
49+
lambda: day10.run(),
50+
lambda: day11.run()
4951
]
5052

5153

test/test_day11.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2020 Andrew Krepps
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
24+
import unittest
25+
26+
from advent2020.day11 import get_part1_answer
27+
from advent2020.day11 import get_part2_answer
28+
29+
30+
seat_data = """
31+
L.LL.LL.LL
32+
LLLLLLL.LL
33+
L.L.L..L..
34+
LLLL.LL.LL
35+
L.LL.LL.LL
36+
L.LLLLL.LL
37+
..L.L.....
38+
LLLLLLLLLL
39+
L.LLLLLL.L
40+
L.LLLLL.LL
41+
"""
42+
43+
44+
class Day11Test(unittest.TestCase):
45+
def test_day11(self):
46+
seat_grid = [line.strip() for line in seat_data.split("\n") if len(line.strip()) > 0]
47+
self.assertEqual(get_part1_answer(seat_grid), 37)
48+
self.assertEqual(get_part2_answer(seat_grid), 26)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy