kupuguy

Untitled

Dec 11th, 2025
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 KB | Source Code | 0 0
  1. from pathlib import Path
  2.  
  3.  
  4. def parse(input: str) -> dict[str, set[str]]:
  5.     result: dict[str, set[str]] = {}
  6.     for line in input.splitlines():
  7.         src, _, targets = line.partition(": ")
  8.         result[src] = set(targets.strip().split())
  9.  
  10.     return result
  11.  
  12.  
  13. def part1(data: dict[str, set[str]]) -> int:
  14.     counts: dict[str, int] = {device: 1 for device in data if "out" in data[device]}
  15.  
  16.     while "you" not in counts:
  17.         ck = counts.keys()
  18.         for device in list(data.keys() - ck):
  19.             if data[device] < ck:
  20.                 counts[device] = sum(counts[k] for k in data[device])
  21.     return counts["you"]
  22.  
  23.  
  24. def count_paths(
  25.     devices: dict[str, set[str]], from_device: str, to_device: str, ignore: set[str]
  26. ) -> int:
  27.     counts: dict[str, int] = {
  28.         device: 1 for device in devices if to_device in devices[device]
  29.     }
  30.  
  31.     ok = True
  32.     while ok and from_device not in counts:
  33.         ok = False
  34.         ck = counts.keys()
  35.         for device in list(devices.keys() - ck - ignore):
  36.             if devices[device] - ignore < ck:
  37.                 counts[device] = sum(counts[k] for k in devices[device] - ignore)
  38.                 ok = True
  39.     res = counts.get(from_device, 0)
  40.     print(f"{from_device}->{to_device} skipping {ignore}: {res}")
  41.     return res
  42.  
  43.  
  44. def part2(data: list[str]) -> int:
  45.     svr_fft = count_paths(data, "svr", "fft", {"dac", "out"})
  46.     svr_dac = count_paths(data, "svr", "dac", {"fft", "out"})
  47.     fft_dac = count_paths(data, "fft", "dac", {"svr", "out"})
  48.     dac_fft = count_paths(data, "dac", "fft", {"svr", "out"})
  49.     fft_out = count_paths(data, "fft", "out", {"svr", "dac"})
  50.     dac_out = count_paths(data, "dac", "out", {"svr", "fft"})
  51.  
  52.     return svr_fft * fft_dac * dac_out + svr_dac * dac_fft * fft_out
  53.  
  54.  
  55. TEST = parse(Path("input/day11-test.txt").read_text())
  56. assert part1(TEST) == 5
  57. INPUT = parse(Path("input/day11.txt").read_text())
  58. part1_total = part1(INPUT)
  59. print(f"{part1_total=:,}")
  60.  
  61. TEST = parse(Path("input/day11-2-test.txt").read_text())
  62. assert part2(TEST) == 2
  63.  
  64. part2_total = part2(INPUT)
  65. print(f"{part2_total=:,}")
  66.  
Advertisement
Add Comment
Please, Sign In to add comment