From b9d6a45e00bfc4cd0851a374ede2c8f069ec81d8 Mon Sep 17 00:00:00 2001 From: Lachlan Meyer Date: Thu, 26 Dec 2024 00:26:18 +1100 Subject: [PATCH] 24-1 --- 2024/24/24-1.py | 58 +++++++++++++++++++++++++++++++++++++++++++ 2024/24/testinput.txt | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 2024/24/24-1.py create mode 100644 2024/24/testinput.txt diff --git a/2024/24/24-1.py b/2024/24/24-1.py new file mode 100644 index 0000000..2ce9250 --- /dev/null +++ b/2024/24/24-1.py @@ -0,0 +1,58 @@ +import functools + +codeValues = {} +sequences = {} + +def runSequence(code): + sequence = sequences[code] + + match sequence[1]: + case 'AND': + codeValues[code] = getValue(sequence[0]) and getValue(sequence[2]) + case 'OR': + codeValues[code] = getValue(sequence[0]) or getValue(sequence[2]) + case 'XOR': + codeValues[code] = getValue(sequence[0]) ^ getValue(sequence[2]) + + return codeValues[code] + + + +@functools.cache +def getValue(code): + if code in codeValues: + return codeValues[code] + + return runSequence(code) + + +with open('2024/24/input.txt') as f: + for line in f: + line = line.strip() + + if '->' in line: + split = line.split(' -> ') + result = split[1] + sequence = tuple(split[0].split(' ')) + sequences[result] = sequence + + elif ':' in line: + split = line.split(': ') + code = split[0] + value = int(split[1]) + codeValues[code] = value + +zCodes = [] +for code in sequences.keys(): + if code[0] == 'z': + zCodes.append(code) + +zCodes.sort() +print(zCodes) + +zValues = '' +for code in reversed(zCodes): + zValues += str(getValue(code)) + +print(zValues) +print(int(zValues, 2)) \ No newline at end of file diff --git a/2024/24/testinput.txt b/2024/24/testinput.txt new file mode 100644 index 0000000..09fb230 --- /dev/null +++ b/2024/24/testinput.txt @@ -0,0 +1,47 @@ +x00: 1 +x01: 0 +x02: 1 +x03: 1 +x04: 0 +y00: 1 +y01: 1 +y02: 1 +y03: 1 +y04: 1 + +ntg XOR fgs -> mjb +y02 OR x01 -> tnw +kwq OR kpj -> z05 +x00 OR x03 -> fst +tgd XOR rvg -> z01 +vdt OR tnw -> bfw +bfw AND frj -> z10 +ffh OR nrd -> bqk +y00 AND y03 -> djm +y03 OR y00 -> psh +bqk OR frj -> z08 +tnw OR fst -> frj +gnj AND tgd -> z11 +bfw XOR mjb -> z00 +x03 OR x00 -> vdt +gnj AND wpb -> z02 +x04 AND y00 -> kjc +djm OR pbm -> qhw +nrd AND vdt -> hwm +kjc AND fst -> rvg +y04 OR y02 -> fgs +y01 AND x02 -> pbm +ntg OR kjc -> kwq +psh XOR fgs -> tgd +qhw XOR tgd -> z09 +pbm OR djm -> kpj +x03 XOR y03 -> ffh +x00 XOR y04 -> ntg +bfw OR bqk -> z06 +nrd XOR fgs -> wpb +frj XOR qhw -> z04 +bqk OR frj -> z07 +y03 OR x01 -> nrd +hwm AND bqk -> z03 +tgd XOR rvg -> z12 +tnw OR pbm -> gnj \ No newline at end of file