diff --git a/2024/24/example.txt b/2024/24/example.txt new file mode 100644 index 0000000..09fb230 --- /dev/null +++ b/2024/24/example.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 diff --git a/2024/24/index.js b/2024/24/index.js new file mode 100644 index 0000000..20bc898 --- /dev/null +++ b/2024/24/index.js @@ -0,0 +1,72 @@ +import fs from 'node:fs' + +function init(filename) { + const data = fs.readFileSync(filename, { encoding: 'utf-8' }, data => data) + const device = new Device() + const Wires = data.split('\n\n')[0].split('\n') + const Gates = data.split('\n\n')[1].split('\n') + Wires.forEach((wire, index) => { + let [name, value] = wire.split(':') + device.Wires.push({ + name: name, + value: parseInt(value.trim()) + }) + }) + + Gates.forEach((gate, index) => { + let [w1, operator, w2, _null, output] = gate.split(' ') + device.Gates.push({ + wire1: w1, + operator: operator, + wire2: w2, + output: output + }) + }) + device.part1() +} + +class Device { + constructor() { + this.Wires = [] + this.Gates = [] + this.Outputs = [] + } + + getWire(wire) { + return this.Wires.find(Wire => wire === Wire.name).value + } + + generateOutput(w1, w2, operator) { + let result = 0 + switch (operator) { + case 'AND': + result = this.getWire(w1) && this.getWire(w2) + break; + case 'OR': + result = this.getWire(w1) || this.getWire(w2) + break; + case 'XOR': + result = this.getWire(w1) ^ this.getWire(w2) + break; + default: break + } + return result + } + part1() { + this.Gates.forEach(gate => { + let result = this.generateOutput(gate.wire1, gate.wire2, gate.operator) + this.Outputs.push({ name: gate.output, value: result }) + }) + + let result = 0 + this.Outputs.sort((a, b) => a.name - b.name).forEach((output, index) => { + result += output.value * (index ** 2) + }) + + console.log(this.Outputs) + console.log(result) + } + +} + +init('example.txt') \ No newline at end of file diff --git a/2024/24/input.txt b/2024/24/input.txt new file mode 100644 index 0000000..e69de29