-
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.
feat(js): add solution for 2024 day 24 part 1
- Loading branch information
1 parent
fb82cba
commit 74cb97c
Showing
2 changed files
with
148 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,62 @@ | ||
import { Cache, solution } from '../../utils/index.js'; | ||
|
||
const CONNECTION_MATCHER = /(?<a>\w+) (?<op>AND|OR|XOR) (?<b>\w+) -> (?<out>\w+)/g; | ||
|
||
const parse = (input) => { | ||
const parts = input.trim().split('\n\n'); | ||
|
||
const gates = new Cache({ | ||
init: (label) => ({ label }), | ||
}); | ||
|
||
for (const match of parts[1].matchAll(CONNECTION_MATCHER)) { | ||
const { a, b, op } = match.groups; | ||
const out = gates.lookup(match.groups.out); | ||
out.formula = { a, b, op }; | ||
} | ||
|
||
for (const line of parts[0].split('\n')) { | ||
const [label, value] = line.split(': '); | ||
gates.lookup(label).value = BigInt(value); | ||
} | ||
|
||
return gates; | ||
}; | ||
|
||
const resolve = (label, gates) => { | ||
const gate = gates.lookup(label); | ||
|
||
if (gate.value !== undefined) return gate.value; | ||
|
||
const a = resolve(gate.formula.a, gates); | ||
const b = resolve(gate.formula.b, gates); | ||
|
||
const { op } = gate.formula; | ||
if (op === 'AND') { | ||
gate.value = a & b; | ||
} else if (op === 'OR') { | ||
gate.value = a | b; | ||
} else if (op === 'XOR') { | ||
gate.value = a ^ b; | ||
} else { | ||
throw new Error(`unsupported op: ${op}`); | ||
} | ||
return gate.value; | ||
}; | ||
|
||
export const partOne = solution((input) => { | ||
const gates = parse(input); | ||
|
||
let result = 0n; | ||
for (const gate of gates.values()) { | ||
if (!gate.label.startsWith('z')) continue; | ||
|
||
const value = resolve(gate.label, gates); | ||
const offset = BigInt(gate.label.slice(-2)); | ||
|
||
result |= value << offset; | ||
} | ||
return Number(result); | ||
}); | ||
|
||
// TODO: Ain't nobody got time for part two ;) |
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,86 @@ | ||
part-one: | ||
- input: | | ||
x00: 1 | ||
x01: 1 | ||
x02: 1 | ||
y00: 0 | ||
y01: 1 | ||
y02: 0 | ||
x00 AND y00 -> z00 | ||
x01 XOR y01 -> z01 | ||
x02 OR y02 -> z02 | ||
answer: 4 | ||
- input: | | ||
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 | ||
answer: 2024 | ||
part-two: | ||
- input: | | ||
x00: 0 | ||
x01: 1 | ||
x02: 0 | ||
x03: 1 | ||
x04: 0 | ||
x05: 1 | ||
y00: 0 | ||
y01: 0 | ||
y02: 1 | ||
y03: 1 | ||
y04: 0 | ||
y05: 1 | ||
x00 AND y00 -> z05 | ||
x01 AND y01 -> z02 | ||
x02 AND y02 -> z01 | ||
x03 AND y03 -> z03 | ||
x04 AND y04 -> z04 | ||
x05 AND y05 -> z00 | ||
answer: z00,z01,z02,z05 |