Skip to content

Commit

Permalink
Add day 24 of year 2024 🎅
Browse files Browse the repository at this point in the history
  • Loading branch information
letelete committed Dec 25, 2024
1 parent b8a03b8 commit e11cad8
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 2024/days/day-24/README.md
Original file line number Diff line number Diff line change
@@ -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 |
10 changes: 10 additions & 0 deletions 2024/days/day-24/in.sample.1.txt
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions 2024/days/day-24/in.sample.2.txt
Original file line number Diff line number Diff line change
@@ -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
61 changes: 61 additions & 0 deletions 2024/days/day-24/main.js
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit e11cad8

Please sign in to comment.