-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_17_part_2.ts
145 lines (131 loc) · 3.18 KB
/
day_17_part_2.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// const text = `\
// Register A: 729
// Register B: 0
// Register C: 0
// Program: 0,1,5,4,3,0`;
const text = await Deno.readTextFile("./day_17.txt");
// const text = `\
// Register A: 0
// Register B: 0
// Register C: 0
// Program: 0,3,5,4,3,0`;
/*
Register A: 27334280
Register B: 0
Register C: 0
Program: 2,4,1,2,7,5,0,3,1,7,4,1,5,5,3,0
0 c adv A /= 1 << c
1 l bxl B ^= l
2 c bst B = c & 7
3 l jnz if (A) Jmp l
4 - bxc B ^= C
5 c out print c
6 c bdv B = A / (1 << c)
7 c cdv C = A / (1 << c)
2 4(A) B = A (& 7)
1 2 B ^= 2
7 4(B) C = A / (1 << B)
0 3(3) A /= 8 (1 << 3)
1 7 B ^= 7
4 1(-) B ^= C
5 5(B) Print B
3 0 Jmp 0 if A !== 0
*/
let [a, b, c, ...program] = [...text.matchAll(/\d+/g)].map((e) => BigInt(e[0]));
// console.log(a, b, c, program);
const expected = program;
// const expected = [7n, 6n, 5n, 3n, 6n, 5n, 7n, 0n, 4n];
let val = -1n;
outer: for (;;) {
val++;
if (val % 100_000_000n === 0n) console.log({ val });
let expectedIndex = 0;
a = val;
b = 0n;
c = 0n;
while (a) {
b = (a & 7n) ^ 2n;
c = a >> b;
b = (b ^ 7n ^ c) & 7n;
// console.log(b);
if (b !== expected[expectedIndex++]) continue outer;
a >>= 3n;
}
if (expectedIndex === expected.length) break;
}
console.log({ correctVal: val });
// Deno.exit();
// let val = 0;
// outer: for (; val < 1e10; val++) {
// if (val % 1_000_000 === 0) console.log({ val });
// a = val;
// let outputIndex = 0;
// try {
// for (let i = 0; i < program.length;) {
// const op = program[i++];
// const getCombo = () => {
// const operand = program[i++];
// // console.log(`Operand: ${operand}`);
// switch (operand) {
// case 0:
// return 0;
// case 1:
// return 1;
// case 2:
// return 2;
// case 3:
// return 3;
// case 4:
// return a;
// case 5:
// return b;
// case 6:
// return c;
// default:
// throw new Error(`Invalid operand: ${operand}`);
// }
// };
// switch (op) {
// case 0: {
// a = (a / (2 ** getCombo())) | 0;
// break;
// }
// case 1: {
// b = b ^ program[i++];
// break;
// }
// case 2: {
// b = getCombo() % 8;
// break;
// }
// case 3: {
// if (a !== 0) i = program[i];
// else i += 1;
// break;
// }
// case 4: {
// i += 1;
// b = b ^ c;
// break;
// }
// case 5: {
// const out = getCombo() % 8;
// if (out !== program[outputIndex++]) continue outer;
// if (outputIndex === program.length) break outer;
// break;
// }
// case 6: {
// b = (a / (2 ** getCombo())) | 0;
// break;
// }
// case 7: {
// c = (a / (2 ** getCombo())) | 0;
// break;
// }
// }
// // console.log(`A: ${a}\nB: ${b}\nC: ${c}\nOp: ${op}\ni = ${i}`);
// }
// // deno-lint-ignore no-empty
// } catch (_) {}
// }
// console.log({ correctValue: val });