Skip to content

Commit 2a93d11

Browse files
committed
Day 11: Part 2 done
1 parent aa8619b commit 2a93d11

File tree

1 file changed

+65
-42
lines changed

1 file changed

+65
-42
lines changed

day11/day11.py

Lines changed: 65 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,104 @@
11
#!/usr/bin/env python3
22
import sys
33
from collections import Counter, defaultdict
4+
from operator import add
45
from typing import Tuple, List, Counter as TypeCounter
56

67

78

8-
def check_neighbors(map, row_num, col_num, symbol) -> int:
9+
def check_neighbors(seatmap, row_num: int, col_num: int, symbol: str, direct: bool = True) -> int:
910
"""return the number of symbols around the seat specified
1011
by row and col"""
1112
results = 0
12-
13-
copy = map.keys()
14-
# print("neighbors:", row_num, col_num)
15-
for row in range(row_num-1, row_num+2):
16-
for col in range(col_num-1,col_num+2):
17-
if (row, col) in copy:
18-
#print(row, col)
19-
# dont check the seat itself :)
20-
if map[(row, col)] == symbol:
21-
if not (row, col) == (row_num, col_num):
22-
results += 1
23-
# print("res:", results)
13+
14+
15+
copy = seatmap.keys()
16+
if not direct:
17+
for row in range(row_num-1, row_num+2):
18+
for col in range(col_num-1,col_num+2):
19+
if (row, col) in copy:
20+
# dont check the seat itself :)
21+
if seatmap[(row, col)] == symbol:
22+
if not (row, col) == (row_num, col_num):
23+
results += 1
24+
# print("res:", results)
25+
else:
26+
#build direction vectors:
27+
for dir in [(0, -1), (0, 1), (-1, 0), (1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]:
28+
check_key = tuple(map(add, (row_num, col_num), dir))
29+
while check_key in copy:
30+
if seatmap[check_key] == symbol:
31+
results += 1
32+
break
33+
# stop looking if theres an empty seat
34+
elif seatmap[check_key] == "L":
35+
break
36+
37+
check_key = tuple(map(add, check_key, dir))
38+
39+
# check if im being stupid :)
40+
assert(results <= 8)
2441
return results
2542

2643

2744

2845

29-
def calculate_step(map: List[List[str]]) -> Tuple[bool, List[List[str]]]:
46+
def calculate_step(seatmap: List[List[str]], lim: int = 4, direct: bool = True) -> Tuple[bool, List[List[str]]]:
3047
changes = []
31-
next_map = defaultdict(str)
32-
for entry in map.items():
48+
next_seatmap = defaultdict(str)
49+
for entry in seatmap.items():
3350
row_num, col_num = entry[0]
3451
seat = entry[1]
3552
next_seat = seat
36-
num_neighbors = check_neighbors(map, row_num, col_num, "#")
53+
num_neighbors = check_neighbors(seatmap, row_num, col_num, "#", direct)
3754
if seat == "L":
3855
if num_neighbors == 0:
3956
next_seat = "#"
4057
else:
4158
pass
42-
#print("Empty seat will stay empty:", row_num, col_num, num_neighbors)
4359

44-
if seat == "#" and num_neighbors >= 4:
60+
if seat == "#" and num_neighbors >= lim:
4561
next_seat = "L"
4662
if next_seat != seat:
4763
changes.append(True)
48-
#print("Next: ", row_num, col_num, next_seat, num_neighbors)
49-
next_map[(row_num, col_num)] = next_seat
64+
next_seatmap[(row_num, col_num)] = next_seat
5065

51-
return (any(changes), next_map)
66+
return (any(changes), next_seatmap)
67+
68+
def calculate_rounds(seatmap, lim: int = 4, direct = False) -> int:
69+
round = 0
70+
run = True
71+
while run:
72+
round += 1
73+
change, next_seatmap = calculate_step(seatmap, lim, direct)
74+
if change:
75+
seatmap = next_seatmap
76+
else:
77+
run = False
78+
return list(seatmap.values()).count("#")
79+
80+
def print_nice_map(seatmap):
81+
result = ""
82+
for row in range(0,100):
83+
for col in range(0,100):
84+
if (row, col) in seatmap:
85+
result += str(seatmap[(row, col)])
86+
if (row, 0) in seatmap:
87+
result += "\n"
88+
return result
5289

5390
if __name__ == "__main__":
54-
map = defaultdict(str)
91+
seatmap = defaultdict(str)
5592
with open(sys.argv[1], "r") as infile:
56-
# add initial 0 as input jolt
5793

58-
# TODO change all to defaultdict, use (row, col) as key!
5994
line = 0
6095
for nextline in infile:
6196
for num, char in enumerate(nextline.strip()):
62-
map[(line, num)] = char
97+
seatmap[(line, num)] = char
6398
line += 1
6499

65-
# print(map)
66100

67-
round = 0
68-
run = True
69-
while run:
70-
#print(check_neighbors(map, 9, 10, "#"))
71-
#print("Round: ", round, "\n Map: ", map)
72-
round += 1
73-
change, next_map = calculate_step(map)
74-
if change:
75-
map = next_map
76-
else:
77-
run = False
78-
#if round > 10:
79-
# run = False
80-
print("Part1: ", list(map.values()).count("#"))
81-
#print("Part2: ", get_variants(jolts))
101+
part1 = calculate_rounds(seatmap.copy())
102+
print("Part1: ", part1)
103+
part2 = calculate_rounds(seatmap.copy(), 5, True)
104+
print("Part2: ", part2)

0 commit comments

Comments
 (0)