-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
803424b
commit db797fe
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from advent.utils.utils import Advent | ||
|
||
advent = Advent(24, 2024) | ||
|
||
GATE = tuple[str, str, str, str] | ||
|
||
|
||
def main(): | ||
lines = advent.get_input_lines() | ||
wires, values, gates = read_input(lines) | ||
|
||
advent.submit(1, run(wires, values, gates)) | ||
|
||
|
||
def run(wires: set[str], values: dict[str, int], gates: list[GATE]) -> int: | ||
while len(wires) > len(values.keys()): | ||
tick(values, gates) | ||
|
||
out = "" | ||
keys = sorted(values.keys()) | ||
for key in keys[keys.index("z00") :]: | ||
out += str(values[key]) | ||
return int(out[::-1], base=2) | ||
|
||
|
||
def tick(values: dict[str, int], gates: list[GATE]): | ||
for left, gate, right, out in gates: | ||
if left in values and right in values: | ||
if gate == "AND": | ||
values[out] = values[left] & values[right] | ||
elif gate == "OR": | ||
values[out] = values[left] | values[right] | ||
else: | ||
values[out] = values[left] ^ values[right] | ||
|
||
|
||
def read_input(lines: list[str]) -> tuple[set[str], dict[str, int], list[GATE]]: | ||
values = dict() | ||
gates = list() | ||
for line in lines: | ||
if ":" in line: | ||
gate, val = line.split(": ") | ||
values[gate] = int(val) | ||
elif "->" in line: | ||
left, out = line.split(" -> ") | ||
left, gate, right = left.split(" ") | ||
gates.append((left, gate, right, out)) | ||
|
||
wires = set(values.keys()) | ||
for left, gate, right, out in gates: | ||
wires.update((left, right, out)) | ||
return wires, values, gates | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |