Skip to content

Commit 420e1a3

Browse files
committed
Day 9
1 parent 8eefa08 commit 420e1a3

File tree

7 files changed

+246
-4
lines changed

7 files changed

+246
-4
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.tsdk": "node_modules/typescript/lib"
3+
}

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@ day8:
3737
npx ts-node day8.ts
3838
day8-2:
3939
npx ts-node day8-2.ts
40+
41+
day9:
42+
npx ts-node day9.ts
43+
day9-2:
44+
npx ts-node day9-2.ts

day9-2.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import * as fs from "fs"
2+
3+
function load(program: string) {
4+
return fs.readFileSync(program, "utf8")
5+
.split(",")
6+
.map(l => parseInt(l))
7+
}
8+
9+
class Computer {
10+
11+
private instructions = [
12+
this.add,
13+
this.mul,
14+
this.get,
15+
this.put,
16+
this.jnz,
17+
this.jz,
18+
this.lt,
19+
this.eq,
20+
this.sbase,
21+
] as const
22+
23+
private mem: number[]
24+
private base = 0
25+
26+
private inputs = []
27+
private outputs = []
28+
29+
constructor(program: number[]) {
30+
this.mem = program.slice()
31+
}
32+
33+
input(...input: number[]) {
34+
this.inputs = this.inputs.concat(input)
35+
return this
36+
}
37+
38+
compute(): number[] {
39+
let iptr = 0
40+
41+
while (true) {
42+
const code = this.mem[iptr]
43+
const op = code % 100
44+
if (op == 99) {
45+
return this.outputs
46+
}
47+
48+
const instr = this.instructions[op - 1]
49+
if (!instr) {
50+
throw new Error(`unknown instruction ${op}`)
51+
}
52+
53+
const vs = Array.from(Array(instr.length)).map((_, i) =>
54+
this.ptr(iptr + i + 1, Math.floor(code % 10 ** (i + 3) / (10 ** (i + 2)))))
55+
iptr = instr.apply(this, vs) ?? (iptr + instr.length + 1)
56+
}
57+
}
58+
59+
private ptr(ptr: number, mode: number) {
60+
switch (mode) {
61+
case 0: return this.mem[ptr]
62+
case 1: return ptr
63+
case 2: return this.mem[ptr] + this.base
64+
default: throw new Error(`unknown mode ${mode}`)
65+
}
66+
}
67+
68+
private m(ptr: number) {
69+
return this.mem[ptr] ?? 0
70+
}
71+
72+
private add(ptr1: number, ptr2: number, ptr3: number) {
73+
this.mem[ptr3] = this.m(ptr1) + this.m(ptr2)
74+
}
75+
private mul(ptr1: number, ptr2: number, ptr3: number) {
76+
this.mem[ptr3] = this.m(ptr1) * this.m(ptr2)
77+
}
78+
private put(ptr1: number) {
79+
this.outputs.push(this.m(ptr1))
80+
}
81+
private get(ptr1: number) {
82+
if (this.inputs.length === 0) {
83+
throw new Error("no input")
84+
}
85+
this.mem[ptr1] = this.inputs.shift()
86+
}
87+
private jnz(ptr1: number, ptr2: number) {
88+
if (this.m(ptr1) !== 0) {
89+
return this.m(ptr2)
90+
}
91+
}
92+
private jz(ptr1: number, ptr2: number) {
93+
if (this.m(ptr1) === 0) {
94+
return this.m(ptr2)
95+
}
96+
}
97+
private lt(ptr1: number, ptr2: number, ptr3: number) {
98+
const val = (this.m(ptr1) < this.m(ptr2)) ? 1 : 0
99+
this.mem[ptr3] = val
100+
}
101+
private eq(ptr1: number, ptr2: number, ptr3: number) {
102+
const val = (this.m(ptr1) === this.m(ptr2)) ? 1 : 0
103+
this.mem[ptr3] = val
104+
}
105+
private sbase(ptr1: number) {
106+
this.base += this.m(ptr1)
107+
}
108+
}
109+
110+
const program = load("input-day9.txt")
111+
console.log(new Computer(program).input(2).compute())

day9.ts

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import * as fs from "fs"
2+
3+
function load(program: string) {
4+
return fs.readFileSync(program, "utf8")
5+
.split(",")
6+
.map(l => parseInt(l))
7+
}
8+
9+
class Computer {
10+
11+
private instructions = [
12+
this.add,
13+
this.mul,
14+
this.get,
15+
this.put,
16+
this.jnz,
17+
this.jz,
18+
this.lt,
19+
this.eq,
20+
this.sbase,
21+
] as const
22+
23+
private mem: number[]
24+
private base = 0
25+
26+
private inputs = []
27+
private outputs = []
28+
29+
constructor(program: number[]) {
30+
this.mem = program.slice()
31+
}
32+
33+
input(...input: number[]) {
34+
this.inputs = this.inputs.concat(input)
35+
return this
36+
}
37+
38+
compute(): number[] {
39+
let iptr = 0
40+
41+
while (true) {
42+
const code = this.mem[iptr]
43+
const op = code % 100
44+
const modes = [code % 1000 / 100, code % 10000 / 1000, code % 100000 / 10000].map(Math.floor)
45+
if (op == 99) {
46+
return this.outputs
47+
}
48+
49+
const instr = this.instructions[op - 1]
50+
if (!instr) {
51+
throw new Error(`unknown instruction ${op}`)
52+
}
53+
54+
const vs = Array(instr.length - 1).fill(0).map((_, i) => this.ptr(iptr + i + 1, modes[i]))
55+
iptr = instr.apply(this, [iptr, ...vs])
56+
}
57+
}
58+
59+
private ptr(ptr: number, mode: number) {
60+
switch (mode) {
61+
case 0: return this.mem[ptr]
62+
case 1: return ptr
63+
case 2: return this.mem[ptr] + this.base
64+
default: throw new Error(`unknown mode ${mode}`)
65+
}
66+
}
67+
68+
private m(ptr: number) {
69+
return this.mem[ptr] ?? 0
70+
}
71+
72+
private add(iptr: number, ptr1: number, ptr2: number, ptr3: number) {
73+
this.mem[ptr3] = this.m(ptr1) + this.m(ptr2)
74+
return iptr + 4
75+
}
76+
private mul(iptr: number, ptr1: number, ptr2: number, ptr3: number) {
77+
this.mem[ptr3] = this.m(ptr1) * this.m(ptr2)
78+
return iptr + 4
79+
}
80+
private put(iptr: number, ptr1: number) {
81+
this.outputs.push(this.m(ptr1))
82+
return iptr + 2
83+
}
84+
private get(iptr: number, ptr1: number) {
85+
if (this.inputs.length === 0) {
86+
throw new Error("no input")
87+
}
88+
this.mem[ptr1] = this.inputs.shift()
89+
return iptr + 2
90+
}
91+
private jnz(iptr: number, ptr1: number, ptr2: number) {
92+
if (this.m(ptr1) !== 0) {
93+
return this.m(ptr2)
94+
}
95+
return iptr + 3
96+
}
97+
private jz(iptr: number, ptr1: number, ptr2: number) {
98+
if (this.m(ptr1) === 0) {
99+
return this.m(ptr2)
100+
}
101+
return iptr + 3
102+
}
103+
private lt(iptr: number, ptr1: number, ptr2: number, ptr3: number) {
104+
const val = (this.m(ptr1) < this.m(ptr2)) ? 1 : 0
105+
this.mem[ptr3] = val
106+
return iptr + 4
107+
}
108+
private eq(iptr: number, ptr1: number, ptr2: number, ptr3: number) {
109+
const val = (this.m(ptr1) === this.m(ptr2)) ? 1 : 0
110+
this.mem[ptr3] = val
111+
return iptr + 4
112+
}
113+
private sbase(iptr: number, ptr1: number) {
114+
this.base += this.m(ptr1)
115+
return iptr + 2
116+
}
117+
}
118+
119+
120+
const program = load("input-day9.txt")
121+
//const program = [109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99]
122+
console.log(new Computer(program).input(1).compute())

input-day9.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1102,34463338,34463338,63,1007,63,34463338,63,1005,63,53,1102,1,3,1000,109,988,209,12,9,1000,209,6,209,3,203,0,1008,1000,1,63,1005,63,65,1008,1000,2,63,1005,63,904,1008,1000,0,63,1005,63,58,4,25,104,0,99,4,0,104,0,99,4,17,104,0,99,0,0,1102,31,1,1009,1101,35,0,1006,1102,1,23,1002,1101,0,32,1013,1101,0,37,1003,1101,0,620,1029,1101,0,28,1011,1102,22,1,1016,1102,1,0,1020,1102,1,34,1007,1102,1,417,1026,1102,1,25,1000,1101,27,0,1010,1102,580,1,1025,1102,1,629,1028,1101,20,0,1004,1102,899,1,1022,1101,26,0,1001,1102,410,1,1027,1102,39,1,1018,1101,0,30,1008,1101,0,38,1014,1101,1,0,1021,1102,29,1,1017,1101,0,36,1012,1101,585,0,1024,1101,0,21,1005,1101,0,892,1023,1102,1,33,1019,1101,24,0,1015,109,17,1206,3,195,4,187,1105,1,199,1001,64,1,64,1002,64,2,64,109,-7,2108,30,-2,63,1005,63,217,4,205,1105,1,221,1001,64,1,64,1002,64,2,64,109,6,1206,5,233,1106,0,239,4,227,1001,64,1,64,1002,64,2,64,109,-16,1202,9,1,63,1008,63,34,63,1005,63,259,1105,1,265,4,245,1001,64,1,64,1002,64,2,64,109,8,1207,-2,34,63,1005,63,285,1001,64,1,64,1105,1,287,4,271,1002,64,2,64,109,-4,1207,-3,27,63,1005,63,305,4,293,1105,1,309,1001,64,1,64,1002,64,2,64,109,-1,21107,40,41,9,1005,1012,331,4,315,1001,64,1,64,1105,1,331,1002,64,2,64,109,5,2107,19,-4,63,1005,63,349,4,337,1106,0,353,1001,64,1,64,1002,64,2,64,109,1,1208,-5,20,63,1005,63,371,4,359,1105,1,375,1001,64,1,64,1002,64,2,64,109,-2,21101,41,0,9,1008,1016,41,63,1005,63,397,4,381,1106,0,401,1001,64,1,64,1002,64,2,64,109,25,2106,0,-5,1001,64,1,64,1105,1,419,4,407,1002,64,2,64,109,-30,2102,1,0,63,1008,63,26,63,1005,63,439,1106,0,445,4,425,1001,64,1,64,1002,64,2,64,109,2,2108,32,4,63,1005,63,465,1001,64,1,64,1105,1,467,4,451,1002,64,2,64,109,-11,1201,10,0,63,1008,63,34,63,1005,63,491,1001,64,1,64,1106,0,493,4,473,1002,64,2,64,109,27,21102,42,1,-1,1008,1019,42,63,1005,63,515,4,499,1105,1,519,1001,64,1,64,1002,64,2,64,109,-6,1201,-7,0,63,1008,63,34,63,1005,63,545,4,525,1001,64,1,64,1106,0,545,1002,64,2,64,109,-15,1202,3,1,63,1008,63,23,63,1005,63,567,4,551,1105,1,571,1001,64,1,64,1002,64,2,64,109,33,2105,1,-8,4,577,1106,0,589,1001,64,1,64,1002,64,2,64,109,-19,1208,-4,34,63,1005,63,605,1105,1,611,4,595,1001,64,1,64,1002,64,2,64,109,7,2106,0,8,4,617,1001,64,1,64,1106,0,629,1002,64,2,64,109,-8,1205,9,647,4,635,1001,64,1,64,1106,0,647,1002,64,2,64,109,-12,2107,38,3,63,1005,63,667,1001,64,1,64,1106,0,669,4,653,1002,64,2,64,109,-3,2102,1,10,63,1008,63,34,63,1005,63,695,4,675,1001,64,1,64,1105,1,695,1002,64,2,64,109,14,21108,43,45,4,1005,1015,711,1105,1,717,4,701,1001,64,1,64,1002,64,2,64,109,13,1205,-4,733,1001,64,1,64,1105,1,735,4,723,1002,64,2,64,109,-30,2101,0,9,63,1008,63,37,63,1005,63,761,4,741,1001,64,1,64,1106,0,761,1002,64,2,64,109,17,21102,44,1,1,1008,1012,45,63,1005,63,785,1001,64,1,64,1106,0,787,4,767,1002,64,2,64,109,5,2101,0,-9,63,1008,63,35,63,1005,63,811,1001,64,1,64,1106,0,813,4,793,1002,64,2,64,109,2,21107,45,44,-5,1005,1013,833,1001,64,1,64,1106,0,835,4,819,1002,64,2,64,109,-2,21101,46,0,-6,1008,1010,44,63,1005,63,855,1106,0,861,4,841,1001,64,1,64,1002,64,2,64,109,2,21108,47,47,-8,1005,1010,883,4,867,1001,64,1,64,1106,0,883,1002,64,2,64,109,2,2105,1,3,1001,64,1,64,1106,0,901,4,889,4,64,99,21102,27,1,1,21102,1,915,0,1105,1,922,21201,1,28815,1,204,1,99,109,3,1207,-2,3,63,1005,63,964,21201,-2,-1,1,21102,1,942,0,1105,1,922,21202,1,1,-1,21201,-2,-3,1,21101,0,957,0,1105,1,922,22201,1,-1,-2,1106,0,968,21202,-2,1,-2,109,-3,2106,0,0

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
"dependencies": {
1212
"@types/node": "^12.12.14",
1313
"ts-node": "^8.5.4",
14-
"typescript": "^3.7.2"
14+
"typescript": "^3.7.3"
1515
}
1616
}

0 commit comments

Comments
 (0)