day 24: part 1 complete
This commit is contained in:
@ -29,7 +29,6 @@ class Device {
|
||||
constructor() {
|
||||
this.Wires = []
|
||||
this.Gates = []
|
||||
this.Outputs = []
|
||||
}
|
||||
|
||||
getWire(wire) {
|
||||
@ -53,20 +52,27 @@ class Device {
|
||||
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 i = 0
|
||||
while (this.Gates.length > 0) {
|
||||
if (this.Wires.some(wire => wire.name === this.Gates[i].wire1) &&
|
||||
this.Wires.some(wire => wire.name === this.Gates[i].wire2)
|
||||
) {
|
||||
let result = this.generateOutput(this.Gates[i].wire1, this.Gates[i].wire2, this.Gates[i].operator)
|
||||
this.Wires.push({ name: this.Gates[i].output, value: result })
|
||||
}
|
||||
else { // this configuration cannot yet be determined, pushing back to the end of array
|
||||
this.Gates.push(this.Gates[i])
|
||||
}
|
||||
this.Gates.splice(i, 1)
|
||||
}
|
||||
|
||||
let result = 0
|
||||
this.Outputs.sort((a, b) => a.name - b.name).forEach((output, index) => {
|
||||
result += output.value * (index ** 2)
|
||||
})
|
||||
let result = this.Wires.filter(wire => String(wire.name).startsWith('z')).sort((a, b) => a.name < b.name ? -1 : 1).reduce((prev, current, index) => {
|
||||
return (current.value === 1) ? prev + Math.pow(2, index) : prev;
|
||||
}, 0);
|
||||
|
||||
console.log(this.Outputs)
|
||||
console.log(result)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
init('example.txt')
|
||||
init('input.txt')
|
Reference in New Issue
Block a user