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