Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pathlib import Path
- class Wire:
- def value(self, state: dict[str, "Wire"]) -> int: ...
- def show(self, state: dict[str, "Wire"]) -> str: ...
- class Input(Wire):
- def __init__(self, name: str, value: int) -> None:
- self.v = value
- self.name = name
- def value(self, state: dict[str, "Wire"]) -> int:
- return self.v
- def show(self, state: dict[str, "Wire"]) -> str:
- return self.name
- class Gate(Wire):
- op = "?"
- def __init__(self, name: str, left: str, right: str) -> None:
- self.left = left
- self.right = right
- self.v = None
- self.name = name
- def __repr__(self) -> str:
- return f"{self.left}{self.op}{self.right}"
- def show(self, state: dict[str, "Wire"]) -> str:
- return (
- f"({state[self.left].show(state)}{self.op}{state[self.right].show(state)})"
- )
- class And(Gate):
- op = "&"
- def value(self, state: dict[str, "Wire"]) -> int:
- if self.v is None:
- self.v = state[self.left].value(state) & state[self.right].value(state)
- return self.v
- class Or(Gate):
- op = "|"
- def value(self, state: dict[str, "Wire"]) -> int:
- if self.v is None:
- self.v = state[self.left].value(state) | state[self.right].value(state)
- return self.v
- class Xor(Gate):
- op = "^"
- def value(self, state: dict[str, "Wire"]) -> int:
- if self.v is None:
- self.v = state[self.left].value(state) ^ state[self.right].value(state)
- return self.v
- GATES = {"AND": And, "OR": Or, "XOR": Xor}
- def parse(input: str) -> dict[str, "Wire"]:
- wires: dict[str, Wire] = {}
- for line in input.splitlines():
- key, _, value = line.partition(": ")
- if value:
- wires[key] = Input(key, int(value))
- operation, _, out = line.partition(" -> ")
- if out:
- l, op, r = operation.split()
- wires[out] = GATES[op](out, l, r)
- return wires
- def part1(data: dict[str, "Wire"]) -> int:
- z_keys = sorted([k for k in data if k.startswith("z")], reverse=True)
- # print(z_keys)
- values = [data[k].value(data) for k in z_keys]
- return int("".join(str(v) for v in values), 2)
- TEST = parse(Path("input/day24-test.txt").read_text())
- assert part1(TEST) == 2024
- INPUT = parse(Path("input/day24.txt").read_text())
- part1_total = part1(INPUT)
- print(f"{part1_total=:,}") # 64,755,511,006,320
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement