Skip to content

Commit b82d91c

Browse files
committed
2024-24: ⭐️⭐️
Function prinout which lead to the discovery of the fixes (still manual) And a DOT to visualise it
1 parent edec12d commit b82d91c

File tree

2 files changed

+71
-18
lines changed

2 files changed

+71
-18
lines changed

2024/24/day24.js

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,80 @@ const operations = {
3737
'XOR': (a, b) => a ^ b,
3838
}
3939

40-
function calculateValue(wire, wires, values, usedWires = []) {
41-
// console.log(wire, usedWires, usedWires.includes(wire))
42-
if (usedWires.includes(wire)) {
43-
throw "There is a loop with wire " + wire
44-
}
40+
function calculateValue(wire, wires, values, depth = 0) {
41+
if (depth > 60) throw "There is a loop in the graph"
4542
let prev = wires.find(v => v[3] == wire)
4643

4744
let setup = { l: prev[0], r: prev[2], g: prev[1] }
4845

4946
if (!setup.l.startsWith('x') && !setup.l.startsWith('y')) {
50-
setup.l = calculateValue(setup.l, wires, values, [wire, ...usedWires])
47+
setup.l = calculateValue(setup.l, wires, values, depth++)
5148
} else {
5249
setup.l = values.get(setup.l)
5350
}
5451

5552
if (!setup.r.startsWith('x') && !setup.r.startsWith('y')) {
56-
setup.r = calculateValue(setup.r, wires, values, [wire, ...usedWires])
53+
setup.r = calculateValue(setup.r, wires, values, depth++)
5754
} else {
5855
setup.r = values.get(setup.r)
5956
}
6057

6158
return operations[setup.g](setup.l, setup.r)
6259
}
6360

61+
function printFunction(wire, wires, depth = 0) {
62+
if (depth > 2) return '...'
63+
let connection = wires.find(v => v[3] == wire)
64+
65+
let setup = { l: connection[0], r: connection[2], g: connection[1] }
66+
67+
let ret = `${wire} = `
68+
69+
let left
70+
if (!setup.l.startsWith('x') && !setup.l.startsWith('y')) {
71+
left = '(' + printFunction(setup.l, wires, depth + 1) + ')'
72+
} else {
73+
left = setup.l
74+
}
75+
76+
let right
77+
if (!setup.r.startsWith('x') && !setup.r.startsWith('y')) {
78+
right = '(' + printFunction(setup.r, wires, depth + 1) + ')'
79+
} else {
80+
right = setup.r
81+
}
82+
83+
if (left.length > right.length) {
84+
ret += `${right} ${setup.g} ${left}`
85+
} else {
86+
ret += `${left} ${setup.g} ${right}`
87+
}
88+
89+
return ret
90+
}
91+
6492
function getFullOutput(code, outputBits, values) {
6593
return outputBits.map(o => calculateValue(o, code, values))
6694
}
6795

96+
function printDot(wires) {
97+
console.log('digraph {')
98+
for (const w of wires) {
99+
let color = w[1] == 'AND'?'green4':w[1]=='OR'?'purple':'black'
100+
console.log(` ${w[0]} -> ${w[3]} [color=\"${color}\"];`)
101+
console.log(` ${w[2]} -> ${w[3]} [color=\"${color}\"];`)
102+
console.log(`${w[3]} [color=\"${color}\",fontcolor=\"${color}\"]`)
103+
}
104+
for (const o of wires.filter(w => w[3].startsWith('z')).map(w => w[3])) {
105+
console.log(`${o} [style=filled,color=darkolivegreen1]`)
106+
}
107+
let inputs = [...new Set(wires.flatMap(w => [w[0], w[2]]))].filter(v => v.startsWith('x') || v.startsWith('y'))
108+
for (const i of inputs) {
109+
console.log(`${i} [style=filled,color=khaki1]`)
110+
}
111+
console.log('}')
112+
113+
}
68114

69115
function populateValues(x, y) {
70116
let values = new Map()
@@ -77,7 +123,6 @@ function populateValues(x, y) {
77123
return values
78124
}
79125

80-
81126
function testEveryBit(code, outputBits) {
82127
let correctBits = []
83128
for (let b = 0; b < 45; b++) {
@@ -87,28 +132,28 @@ function testEveryBit(code, outputBits) {
87132
let v = populateValues(x, y)
88133
let z = getFullOutput(code, outputBits, v).join('')
89134

90-
if (z.split('').toReversed()[b] =='1') {
135+
if (z.split('').toReversed()[b] == '1') {
91136
correctBits.push(b)
92-
} else {
93-
// console.log(b)
94137
}
95-
// console.log(' x:',x.split('').toReversed().join(''))
96-
// console.log(' y:',y.split('').toReversed().join(''))
97-
// console.log('z:', z.split('').toReversed().join(''))
98-
// console.log()
138+
99139
}
100-
// console.log(correctBits)
101140
return correctBits
102141
}
103142

104143

105144
let [data, code] = parseInput(input)
106145
const outputBits = code.map(c => c[3]).filter(v => v.startsWith('z')).sort()
107146

147+
// node day24.js | dot -Tpng >day24.png
148+
// printDot(code)
149+
// return
150+
108151
let valueCodes = getFullOutput(code, outputBits, data)
109-
console.log(parseInt(valueCodes.toReversed().join(''),2))
152+
console.log(parseInt(valueCodes.toReversed().join(''), 2))
110153

111-
console.log(testEveryBit(code, outputBits).length == 45)
154+
console.log(
155+
outputBits.map(b => printFunction(b, code)).join('\n')
156+
)
112157

113158
let swap = [
114159
["z14", "vss"],
@@ -124,6 +169,14 @@ for (const sw of swap) {
124169
s2[3] = sw[0]
125170
}
126171

172+
console.log("")
173+
console.log("AFTER THE FIX")
174+
175+
console.log(
176+
outputBits.map(b => printFunction(b, code)).join('\n')
177+
)
178+
179+
127180
console.log(testEveryBit(code, outputBits).length == 45)
128181

129182
console.log(swap.flat().sort().join(','))

2024/24/day24.png

1.37 MB
Loading

0 commit comments

Comments
 (0)