Skip to content

Commit 29eab39

Browse files
committed
Not getting stars here really.
Solved with the help of claude.ai The base-26 arithmetics is a bit over my head.
1 parent 25c6a10 commit 29eab39

File tree

3 files changed

+434
-0
lines changed

3 files changed

+434
-0
lines changed

2021/24/ai.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Each block's parameters
2+
const blocks = [
3+
{ divZ: 1, addX: 14, addY: 0 }, // 0
4+
{ divZ: 1, addX: 13, addY: 12 }, // 1
5+
{ divZ: 1, addX: 15, addY: 14 }, // 2
6+
{ divZ: 1, addX: 13, addY: 0 }, // 3
7+
{ divZ: 26, addX: -2, addY: 3 }, // 4
8+
{ divZ: 1, addX: 10, addY: 15 }, // 5
9+
{ divZ: 1, addX: 13, addY: 11 }, // 6
10+
{ divZ: 26, addX: -15, addY: 12 }, // 7
11+
{ divZ: 1, addX: 11, addY: 1 }, // 8
12+
{ divZ: 26, addX: -9, addY: 12 }, // 9
13+
{ divZ: 26, addX: -9, addY: 3 }, // 10
14+
{ divZ: 26, addX: -7, addY: 10 }, // 11
15+
{ divZ: 26, addX: -4, addY: 14 }, // 12
16+
{ divZ: 26, addX: -6, addY: 12 } // 13
17+
];
18+
19+
function findRelationships() {
20+
let stack = [];
21+
let relationships = [];
22+
23+
for (let i = 0; i < blocks.length; i++) {
24+
if (blocks[i].divZ === 1) {
25+
stack.push({ pos: i, value: blocks[i].addY });
26+
} else {
27+
const prev = stack.pop();
28+
relationships.push({
29+
pos1: prev.pos,
30+
pos2: i,
31+
diff: prev.value + blocks[i].addX
32+
});
33+
}
34+
}
35+
return relationships;
36+
}
37+
38+
function findAllValidPairs(diff) {
39+
const pairs = [];
40+
for (let w1 = 1; w1 <= 9; w1++) {
41+
const w2 = w1 + diff;
42+
if (w2 >= 1 && w2 <= 9) {
43+
pairs.push([w1, w2]);
44+
}
45+
}
46+
return pairs;
47+
}
48+
49+
function* generateAllValidNumbers(relationships, currentDigits = new Array(14).fill(null), relationshipIndex = 0) {
50+
if (relationshipIndex === relationships.length) {
51+
// Fill in any remaining unassigned digits with 9 (for maximum) or 1 (for minimum)
52+
const result = [...currentDigits];
53+
for (let i = 0; i < result.length; i++) {
54+
if (result[i] === null) {
55+
result[i] = 9; // or 1 for minimum
56+
}
57+
}
58+
yield result;
59+
return;
60+
}
61+
62+
const rel = relationships[relationshipIndex];
63+
const validPairs = findAllValidPairs(rel.diff);
64+
65+
for (const [w1, w2] of validPairs) {
66+
if ((currentDigits[rel.pos1] === null || currentDigits[rel.pos1] === w1) &&
67+
(currentDigits[rel.pos2] === null || currentDigits[rel.pos2] === w2)) {
68+
const nextDigits = [...currentDigits];
69+
nextDigits[rel.pos1] = w1;
70+
nextDigits[rel.pos2] = w2;
71+
yield* generateAllValidNumbers(relationships, nextDigits, relationshipIndex + 1);
72+
}
73+
}
74+
}
75+
76+
function verifyNumber(digits) {
77+
let z = 0;
78+
for (let i = 0; i < 14; i++) {
79+
const w = digits[i];
80+
let x = z % 26;
81+
z = Math.floor(z / blocks[i].divZ);
82+
x += blocks[i].addX;
83+
x = (x === w) ? 0 : 1;
84+
const y = (w + blocks[i].addY) * x;
85+
z = z * (25 * x + 1) + y;
86+
}
87+
return z === 0;
88+
}
89+
90+
// Find all valid numbers
91+
function findAllValidNumbers() {
92+
const relationships = findRelationships();
93+
const validNumbers = [];
94+
95+
console.log('Digit relationships:');
96+
relationships.forEach(rel => {
97+
console.log(`digit[${rel.pos1}] + ${rel.diff} = digit[${rel.pos2}]`);
98+
});
99+
100+
console.log('\nSearching for valid numbers...');
101+
for (const digits of generateAllValidNumbers(relationships)) {
102+
if (verifyNumber(digits)) {
103+
validNumbers.push([...digits]);
104+
}
105+
}
106+
107+
// Sort numbers for better display
108+
validNumbers.sort((a, b) => {
109+
const numA = BigInt(a.join(''));
110+
const numB = BigInt(b.join(''));
111+
return numA < numB ? -1 : numA > numB ? 1 : 0;
112+
});
113+
114+
return validNumbers;
115+
}
116+
117+
// Find and display all solutions
118+
const allValidNumbers = findAllValidNumbers();
119+
console.log(`\nFound ${allValidNumbers.length} valid numbers:`);
120+
console.log('Smallest:', allValidNumbers[0].join(''));
121+
console.log('Largest:', allValidNumbers[allValidNumbers.length - 1].join(''));
122+
console.log('\nAll valid numbers:');
123+
allValidNumbers.forEach(digits => console.log(digits.join('')));

2021/24/day24.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const { loadLines } = require('../input')
2+
let input = loadLines('24/input.txt')
3+
4+
const instructions = {
5+
'inp': (cpu, a) => cpu.var[a] = cpu.input.shift(),
6+
'add': (cpu, a, b) => cpu.var[a] = cpu.var[a] + cpu.var[b],
7+
'addv': (cpu, a, b) => cpu.var[a] = cpu.var[a] + b,
8+
'mul': (cpu, a, b) => cpu.var[a] = cpu.var[a] * cpu.var[b],
9+
'mulv': (cpu, a, b) => cpu.var[a] = cpu.var[a] * b,
10+
'div': (cpu, a, b) => cpu.var[a] = Math.floor(cpu.var[a] / cpu.var[b]),
11+
'divv': (cpu, a, b) => cpu.var[a] = Math.floor(cpu.var[a] / b),
12+
'mod': (cpu, a, b) => cpu.var[a] = cpu.var[a] % cpu.var[b],
13+
'modv': (cpu, a, b) => cpu.var[a] = cpu.var[a] % b,
14+
'eql': (cpu, a, b) => cpu.var[a] = cpu.var[a] == cpu.var[b] ? 1 : 0,
15+
'eqlv': (cpu, a, b) => cpu.var[a] = cpu.var[a] == b ? 1 : 0,
16+
}
17+
18+
function parseCode(line) {
19+
let ops = line.split(' ')
20+
let instruction = ops.shift()
21+
if (ops.length == 1) {
22+
return (cpu) => instructions[instruction](cpu, ops[0])
23+
} else {
24+
let value = parseInt(ops[1])
25+
if (isNaN(value)) {
26+
return (cpu) => instructions[instruction](cpu, ops[0], ops[1])
27+
} else {
28+
return (cpu) => instructions[instruction+'v'](cpu, ops[0], value)
29+
}
30+
}
31+
}
32+
33+
function runCode(code, input) {
34+
let cpu = {
35+
var: {
36+
w: 0,
37+
x: 0,
38+
y: 0,
39+
z: 0
40+
},
41+
input: [...input]
42+
}
43+
44+
for (op of code) {
45+
op(cpu)
46+
}
47+
48+
return cpu
49+
}
50+
51+
let code = input.map(l => parseCode(l))
52+
53+
console.log(
54+
runCode(code, [7,1,1,3,1,1,5,1,9,1,7,8,9,1]).var.z
55+
)
56+
57+
console.log(
58+
runCode(code, [9,1,2,9,7,3,9,5,9,1,9,9,9,3]).var.z
59+
)

0 commit comments

Comments
 (0)