Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pathlib import Path
- def parse(input: str) -> dict[str, set[str]]:
- result: dict[str, set[str]] = {}
- for line in input.splitlines():
- src, _, targets = line.partition(": ")
- result[src] = set(targets.strip().split())
- return result
- def part1(data: dict[str, set[str]]) -> int:
- counts: dict[str, int] = {device: 1 for device in data if "out" in data[device]}
- while "you" not in counts:
- ck = counts.keys()
- for device in list(data.keys() - ck):
- if data[device] < ck:
- counts[device] = sum(counts[k] for k in data[device])
- return counts["you"]
- def count_paths(
- devices: dict[str, set[str]], from_device: str, to_device: str, ignore: set[str]
- ) -> int:
- counts: dict[str, int] = {
- device: 1 for device in devices if to_device in devices[device]
- }
- ok = True
- while ok and from_device not in counts:
- ok = False
- ck = counts.keys()
- for device in list(devices.keys() - ck - ignore):
- if devices[device] - ignore < ck:
- counts[device] = sum(counts[k] for k in devices[device] - ignore)
- ok = True
- res = counts.get(from_device, 0)
- print(f"{from_device}->{to_device} skipping {ignore}: {res}")
- return res
- def part2(data: list[str]) -> int:
- svr_fft = count_paths(data, "svr", "fft", {"dac", "out"})
- svr_dac = count_paths(data, "svr", "dac", {"fft", "out"})
- fft_dac = count_paths(data, "fft", "dac", {"svr", "out"})
- dac_fft = count_paths(data, "dac", "fft", {"svr", "out"})
- fft_out = count_paths(data, "fft", "out", {"svr", "dac"})
- dac_out = count_paths(data, "dac", "out", {"svr", "fft"})
- return svr_fft * fft_dac * dac_out + svr_dac * dac_fft * fft_out
- TEST = parse(Path("input/day11-test.txt").read_text())
- assert part1(TEST) == 5
- INPUT = parse(Path("input/day11.txt").read_text())
- part1_total = part1(INPUT)
- print(f"{part1_total=:,}")
- TEST = parse(Path("input/day11-2-test.txt").read_text())
- assert part2(TEST) == 2
- part2_total = part2(INPUT)
- print(f"{part2_total=:,}")
Advertisement
Add Comment
Please, Sign In to add comment