diff --git a/2024/days/day-24/README.md b/2024/days/day-24/README.md new file mode 100644 index 0000000..a845dfd --- /dev/null +++ b/2024/days/day-24/README.md @@ -0,0 +1,25 @@ +# Benchmark + +``` +Platform: darwin arm64 +CPU: Apple M3 Pro 11 Cores +Memory: 18.00 GB +``` + +## Sample 1 + +| part | time (~) | μs | +| ---- | -------- | ------------------- | +| 1 | 0.08ms | 0.08462499999999551 | + +## Sample 2 + +| part | time (~) | μs | +| ---- | -------- | ----------------- | +| 1 | 0.76ms | 0.758499999999998 | + +## Answer + +| part | time (~) | μs | +| ---- | -------- | ------------------ | +| 1 | 29.45ms | 29.453915999999992 | diff --git a/2024/days/day-24/in.sample.1.txt b/2024/days/day-24/in.sample.1.txt new file mode 100644 index 0000000..8e277c1 --- /dev/null +++ b/2024/days/day-24/in.sample.1.txt @@ -0,0 +1,10 @@ +x00: 1 +x01: 1 +x02: 1 +y00: 0 +y01: 1 +y02: 0 + +x00 AND y00 -> z00 +x01 XOR y01 -> z01 +x02 OR y02 -> z02 diff --git a/2024/days/day-24/in.sample.2.txt b/2024/days/day-24/in.sample.2.txt new file mode 100644 index 0000000..94b6eed --- /dev/null +++ b/2024/days/day-24/in.sample.2.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 diff --git a/2024/days/day-24/main.js b/2024/days/day-24/main.js new file mode 100644 index 0000000..b01222b --- /dev/null +++ b/2024/days/day-24/main.js @@ -0,0 +1,61 @@ +function parse(source) { + const [a, b] = source.trim().split('\n\n'); + + const codes = [...a.matchAll(/^(\w+):\s(\d)$/gm)].map((groups) => ({ + key: groups[1], + value: parseInt(groups[2]), + })); + + const instances = [...b.matchAll(/^([\w\s]+) -> (\w+)$/gm)].map((groups) => ({ + expr: groups[1], + result: groups[2], + })); + + return { codes, instances }; +} + +function evaluate(a, b, op) { + if (op === 'AND') return a & b; + if (op === 'OR') return a | b; + if (op === 'XOR') return a ^ b; +} + +function part1(data) { + const q = []; + const m = new Map(); + + data.codes.forEach(({ key, value }) => m.set(key, value)); + + data.instances.forEach((ins) => { + const [a, op, b] = ins.expr.split(' '); + if (m.has(a) && m.has(b)) { + m.set(ins.result, evaluate(m.get(a), m.get(b), op)); + } else { + q.push(ins); + } + }); + + while (q.length) { + const ins = q.shift(); + const [a, op, b] = ins.expr.split(' '); + if (m.has(a) && m.has(b)) { + m.set(ins.result, evaluate(m.get(a), m.get(b), op)); + } else { + q.push(ins); + } + } + + const binary = data.instances + .map(({ result }) => [result, m.get(result)]) + .filter(([e]) => e.startsWith('z')) + .sort(([a], [b]) => b.localeCompare(a)) + .reduce((binary, [, bit]) => `${binary}${bit}`, ''); + + return parseInt(binary, 2); +} + +function part2(data) { + return null; +} + +module.exports = { parse, part1, part2 };