diff --git a/2024-python/output/day_24.py b/2024-python/output/day_24.py new file mode 100644 index 0000000..3474478 --- /dev/null +++ b/2024-python/output/day_24.py @@ -0,0 +1,42 @@ +def solve(data): + values, wires = data.split("\n\n") + values = dict([tuple(c.strip().split(": ")) for c in values.splitlines()]) + Q = [w.split() for w in wires.splitlines()] + while Q: + a, o, b, _, c = Q.pop(0) + if a not in values or b not in values: + Q.append((a, o, b, "_", c)) + continue + a, b = int(values[a]), int(values[b]) + match o: + case "AND": + values[c] = int(a + b == 2) + case "XOR": + values[c] = int(a + b == 1) + case "OR": + values[c] = int(a + b > 0) + p1 = int( + "".join( + [ + str(v) + for k, v in sorted(values.items(), key=lambda x: x[0], reverse=True) + if k.startswith("z") + ] + ), + 2, + ) + p2 = None + return p1, p2 + + +if __name__ == "__main__": + with open("./input/24.txt", "r") as f: + inp = f.read().strip() + + p1, p2 = solve(inp) + + print(p1) + print(p2) + + assert p1 == 36035961805936 + # assert p2 == 0