Skip to content

Commit 2dadda0

Browse files
committed
Day 11
1 parent 162ef5a commit 2dadda0

File tree

4 files changed

+293
-0
lines changed

4 files changed

+293
-0
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@ day10:
4747
npx ts-node day10.ts
4848
day10-2:
4949
npx ts-node day10-2.ts
50+
51+
day11:
52+
npx ts-node day11.ts
53+
day11-2:
54+
npx ts-node day11-2.ts

day11-2.ts

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
input: () => number = () => { throw new Error("no input") }
27+
output = (n: number) => console.log(n)
28+
29+
constructor(program: number[]) {
30+
this.mem = program.slice()
31+
}
32+
33+
compute() {
34+
let iptr = 0
35+
36+
while (true) {
37+
const code = this.mem[iptr]
38+
const op = code % 100
39+
if (op == 99) {
40+
return
41+
}
42+
43+
const instr = this.instructions[op - 1]
44+
if (!instr) {
45+
throw new Error(`unknown instruction ${op}`)
46+
}
47+
48+
const vs = Array.from(Array(instr.length)).map((_, i) =>
49+
this.ptr(iptr + i + 1, Math.floor(code % 10 ** (i + 3) / (10 ** (i + 2)))))
50+
iptr = instr.apply(this, vs) ?? (iptr + instr.length + 1)
51+
}
52+
}
53+
54+
private ptr(ptr: number, mode: number) {
55+
switch (mode) {
56+
case 0: return this.mem[ptr]
57+
case 1: return ptr
58+
case 2: return this.mem[ptr] + this.base
59+
default: throw new Error(`unknown mode ${mode}`)
60+
}
61+
}
62+
63+
private m(ptr: number) {
64+
return this.mem[ptr] ?? 0
65+
}
66+
67+
private add(ptr1: number, ptr2: number, ptr3: number) {
68+
this.mem[ptr3] = this.m(ptr1) + this.m(ptr2)
69+
}
70+
private mul(ptr1: number, ptr2: number, ptr3: number) {
71+
this.mem[ptr3] = this.m(ptr1) * this.m(ptr2)
72+
}
73+
private put(ptr1: number) {
74+
this.output(this.m(ptr1))
75+
}
76+
private get(ptr1: number) {
77+
this.mem[ptr1] = this.input()
78+
}
79+
private jnz(ptr1: number, ptr2: number) {
80+
if (this.m(ptr1) !== 0) {
81+
return this.m(ptr2)
82+
}
83+
}
84+
private jz(ptr1: number, ptr2: number) {
85+
if (this.m(ptr1) === 0) {
86+
return this.m(ptr2)
87+
}
88+
}
89+
private lt(ptr1: number, ptr2: number, ptr3: number) {
90+
const val = (this.m(ptr1) < this.m(ptr2)) ? 1 : 0
91+
this.mem[ptr3] = val
92+
}
93+
private eq(ptr1: number, ptr2: number, ptr3: number) {
94+
const val = (this.m(ptr1) === this.m(ptr2)) ? 1 : 0
95+
this.mem[ptr3] = val
96+
}
97+
private sbase(ptr1: number) {
98+
this.base += this.m(ptr1)
99+
}
100+
}
101+
102+
const computer = new Computer(load("input-day11.txt"))
103+
104+
const map = new Map<string, number>()
105+
let p = [0, 0]
106+
let vs = [[0, -1], [1, 0], [0, 1], [-1, 0]]
107+
let v = 0
108+
let state = 0
109+
110+
map.set(JSON.stringify([0, 0]), 1)
111+
112+
computer.input = () => {
113+
return map.get(JSON.stringify(p)) ?? 0
114+
}
115+
computer.output = (n: number) => {
116+
if (n !== 0 && n !== 1) {
117+
throw Error(`unexpected output ${n}`)
118+
}
119+
if (state === 0) {
120+
map.set(JSON.stringify(p), n)
121+
state = 1
122+
} else {
123+
if (n == 0) {
124+
v = (4 + v - 1) % 4
125+
} else {
126+
v = (v + 1) % 4
127+
}
128+
p = [p[0] + vs[v][0], p[1] + vs[v][1]]
129+
state = 0
130+
}
131+
}
132+
133+
computer.compute()
134+
135+
const [l, t, r, b] = Array.from(map.keys())
136+
.map(n => JSON.parse(n))
137+
.reduce((a, v) => [Math.min(a[0], v[0]), Math.min(a[1], v[1]), Math.max(a[2], v[0]), Math.max(a[3], v[1])], [0, 0, 0, 0])
138+
const image = Array(r + 1).fill(0).map(_ => Array(b + 1).fill(" "))
139+
140+
map.forEach((v, k) => {
141+
const [x, y] = JSON.parse(k)
142+
image[x][y] = v
143+
})
144+
145+
146+
print(image)
147+
148+
function print<T>(a: T[][]) {
149+
for (let y = 0; y < a[0].length; y++) {
150+
console.log(Array(a.length).fill(0).map((_, x) => a[x][y] ? "#" : " ").join(""))
151+
}
152+
}

day11.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
input: () => number = () => { throw new Error("no input") }
27+
output = (n: number) => console.log(n)
28+
29+
constructor(program: number[]) {
30+
this.mem = program.slice()
31+
}
32+
33+
compute() {
34+
let iptr = 0
35+
36+
while (true) {
37+
const code = this.mem[iptr]
38+
const op = code % 100
39+
if (op == 99) {
40+
return
41+
}
42+
43+
const instr = this.instructions[op - 1]
44+
if (!instr) {
45+
throw new Error(`unknown instruction ${op}`)
46+
}
47+
48+
const vs = Array.from(Array(instr.length)).map((_, i) =>
49+
this.ptr(iptr + i + 1, Math.floor(code % 10 ** (i + 3) / (10 ** (i + 2)))))
50+
iptr = instr.apply(this, vs) ?? (iptr + instr.length + 1)
51+
}
52+
}
53+
54+
private ptr(ptr: number, mode: number) {
55+
switch (mode) {
56+
case 0: return this.mem[ptr]
57+
case 1: return ptr
58+
case 2: return this.mem[ptr] + this.base
59+
default: throw new Error(`unknown mode ${mode}`)
60+
}
61+
}
62+
63+
private m(ptr: number) {
64+
return this.mem[ptr] ?? 0
65+
}
66+
67+
private add(ptr1: number, ptr2: number, ptr3: number) {
68+
this.mem[ptr3] = this.m(ptr1) + this.m(ptr2)
69+
}
70+
private mul(ptr1: number, ptr2: number, ptr3: number) {
71+
this.mem[ptr3] = this.m(ptr1) * this.m(ptr2)
72+
}
73+
private put(ptr1: number) {
74+
this.output(this.m(ptr1))
75+
}
76+
private get(ptr1: number) {
77+
this.mem[ptr1] = this.input()
78+
}
79+
private jnz(ptr1: number, ptr2: number) {
80+
if (this.m(ptr1) !== 0) {
81+
return this.m(ptr2)
82+
}
83+
}
84+
private jz(ptr1: number, ptr2: number) {
85+
if (this.m(ptr1) === 0) {
86+
return this.m(ptr2)
87+
}
88+
}
89+
private lt(ptr1: number, ptr2: number, ptr3: number) {
90+
const val = (this.m(ptr1) < this.m(ptr2)) ? 1 : 0
91+
this.mem[ptr3] = val
92+
}
93+
private eq(ptr1: number, ptr2: number, ptr3: number) {
94+
const val = (this.m(ptr1) === this.m(ptr2)) ? 1 : 0
95+
this.mem[ptr3] = val
96+
}
97+
private sbase(ptr1: number) {
98+
this.base += this.m(ptr1)
99+
}
100+
}
101+
102+
const computer = new Computer(load("input-day11.txt"))
103+
104+
const map = new Map<string, number>()
105+
let p = [0, 0]
106+
let vs = [[0, -1], [1, 0], [0, 1], [-1, 0]]
107+
let v = 0
108+
let state = 0
109+
110+
map.set(JSON.stringify([0, 0]), 1)
111+
112+
computer.input = () => {
113+
return map.get(JSON.stringify(p)) ?? 0
114+
}
115+
computer.output = (n: number) => {
116+
if (n !== 0 && n !== 1) {
117+
throw Error(`unexpected output ${n}`)
118+
}
119+
if (state === 0) {
120+
map.set(JSON.stringify(p), n)
121+
state = 1
122+
} else {
123+
if (n == 0) {
124+
v = (4 + v - 1) % 4
125+
} else {
126+
v = (v + 1) % 4
127+
}
128+
p = [p[0] + vs[v][0], p[1] + vs[v][1]]
129+
state = 0
130+
}
131+
}
132+
133+
computer.compute()
134+
135+
console.log(map.size)

input-day11.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3,8,1005,8,328,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,101,0,8,28,1006,0,13,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,1002,8,1,54,1,1103,9,10,1006,0,97,2,1003,0,10,1,105,6,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1001,8,0,91,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,0,10,4,10,102,1,8,113,2,109,5,10,1006,0,96,1,2,5,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,102,1,8,146,2,103,2,10,1006,0,69,2,9,8,10,1006,0,25,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,101,0,8,182,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,1001,8,0,203,2,5,9,10,1006,0,0,2,6,2,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,1002,8,1,236,2,4,0,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1002,8,1,263,2,105,9,10,1,103,15,10,1,4,4,10,2,109,7,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,1001,8,0,301,1006,0,63,2,105,6,10,101,1,9,9,1007,9,1018,10,1005,10,15,99,109,650,104,0,104,1,21102,387508441116,1,1,21102,1,345,0,1106,0,449,21102,1,387353256852,1,21102,1,356,0,1105,1,449,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,179410308315,0,1,21102,1,403,0,1106,0,449,21101,206199495827,0,1,21102,414,1,0,1105,1,449,3,10,104,0,104,0,3,10,104,0,104,0,21102,718086758760,1,1,21102,1,437,0,1105,1,449,21101,838429573908,0,1,21102,448,1,0,1106,0,449,99,109,2,21202,-1,1,1,21102,1,40,2,21102,480,1,3,21101,470,0,0,1105,1,513,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,475,476,491,4,0,1001,475,1,475,108,4,475,10,1006,10,507,1102,0,1,475,109,-2,2106,0,0,0,109,4,2101,0,-1,512,1207,-3,0,10,1006,10,530,21101,0,0,-3,21202,-3,1,1,21201,-2,0,2,21102,1,1,3,21102,549,1,0,1105,1,554,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,577,2207,-4,-2,10,1006,10,577,22102,1,-4,-4,1106,0,645,22102,1,-4,1,21201,-3,-1,2,21202,-2,2,3,21101,596,0,0,1106,0,554,22101,0,1,-4,21102,1,1,-1,2207,-4,-2,10,1006,10,615,21101,0,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,637,21201,-1,0,1,21101,637,0,0,106,0,512,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2106,0,0

0 commit comments

Comments
 (0)