Solution to problem 11 in Python [mypy]
This commit is contained in:
parent
3c18e6d270
commit
887a97fae1
@ -167,12 +167,10 @@
|
||||
|
||||
import re
|
||||
from collections import defaultdict, deque, namedtuple
|
||||
|
||||
# part2 = [1, 1, 2, 3, 2, 3, 2, 3, 2, 3, 1, 1, 1, 1]
|
||||
from typing import Deque
|
||||
|
||||
|
||||
def parse_entry():
|
||||
# floor_re = re.compile(r"The (\w+) floor contains")
|
||||
gen_re = re.compile(r"a (\w+) generator")
|
||||
chip_re = re.compile(r"a (\w+-compatible) microchip")
|
||||
with open("files/P11.txt", "r") as f:
|
||||
@ -184,18 +182,24 @@ def parse_entry():
|
||||
microchips = chip_re.findall(my_input)
|
||||
if microchips:
|
||||
[entry[idx].append((idx, m)) for m in microchips]
|
||||
|
||||
return entry
|
||||
|
||||
|
||||
def find_level_of_microchip(item, dic):
|
||||
for key in dic.keys():
|
||||
for val in dic[key]:
|
||||
if f"{item}-compatible" in val:
|
||||
return val[0]
|
||||
def find_level_of_microchip(
|
||||
item: str, dic: defaultdict[int, list[tuple[int, str]]]
|
||||
):
|
||||
try:
|
||||
for key in dic.keys():
|
||||
for val in dic[key]:
|
||||
if f"{item}-compatible" in val:
|
||||
return val[0]
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def get_floor_config(dic):
|
||||
def get_floor_config(
|
||||
dic: defaultdict[int, list[tuple[int, str]]]
|
||||
) -> list[int]:
|
||||
floor_config = []
|
||||
for lvl in dic.keys():
|
||||
for items in dic[lvl]:
|
||||
@ -211,7 +215,7 @@ def get_floor_config(dic):
|
||||
State = namedtuple("State", ["f", "e", "s"])
|
||||
|
||||
|
||||
def valid(state):
|
||||
def valid(state) -> bool:
|
||||
if not 1 <= state.e <= 4:
|
||||
return False
|
||||
|
||||
@ -222,11 +226,11 @@ def valid(state):
|
||||
return True
|
||||
|
||||
|
||||
def solved(state):
|
||||
def solved(state) -> bool:
|
||||
return all(i == 4 for i in state.f)
|
||||
|
||||
|
||||
def generalize(state):
|
||||
def generalize(state) -> str:
|
||||
generators = [
|
||||
sum(1 for v in state.f[::2] if v == floor) for floor in range(1, 5)
|
||||
]
|
||||
@ -237,8 +241,8 @@ def generalize(state):
|
||||
return "".join(map(str, generators + microchips)) + str(state.e)
|
||||
|
||||
|
||||
def bfs(floor_config):
|
||||
bfs_q = deque()
|
||||
def bfs(floor_config: list[int]):
|
||||
bfs_q: Deque = deque()
|
||||
bfs_q.append(State(floor_config, 1, 0))
|
||||
seen = set()
|
||||
while bfs_q:
|
||||
@ -273,7 +277,7 @@ def bfs(floor_config):
|
||||
state.f[idx] -= 1
|
||||
|
||||
|
||||
def part_1():
|
||||
def part_1() -> None:
|
||||
p1 = get_floor_config(parse_entry())
|
||||
print(f"We need {bfs(p1)} steps for part 1")
|
||||
|
||||
@ -298,7 +302,7 @@ def part_1():
|
||||
# # including these four new ones, to the fourth floor?
|
||||
|
||||
|
||||
def part_2():
|
||||
def part_2() -> None:
|
||||
p2 = get_floor_config(parse_entry())
|
||||
# extra parts are on floor 1
|
||||
p2.extend([1, 1, 1, 1])
|
||||
|
Loading…
Reference in New Issue
Block a user