Skip to content

Commit bdaeaf4

Browse files
committed
Day 21 - init
1 parent 46b6a71 commit bdaeaf4

File tree

4 files changed

+329
-29
lines changed

4 files changed

+329
-29
lines changed

day-20-a-regular-map/test.js

+25-29
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,46 @@ const fs = require('fs');
33

44
const map = require('./map');
55

6-
describe.only('Day 20: A Regular Map', () => {
6+
describe('Day 20: A Regular Map', () => {
77

88
describe('Part One', () => {
99
it('execute sample program and return reg 0', () => {
10-
const expected =
11-
`#####
12-
#.|.#
13-
#-###
14-
#.|X#
15-
#####`;
10+
// #####
11+
// #.|.#
12+
// #-###
13+
// #.|X#
14+
// #####
1615
expect(map.findFurthestRoom('^WNE$')).to.equal(3);
1716
});
1817

1918
it('execute sample program and return reg 0', () => {
20-
const expected =
21-
`#?#?#?#?#
22-
?.|.|.|.?
23-
#?#?#?#-#
24-
?X|.?
25-
#?#?#`;
19+
// #?#?#?#?#
20+
// ?.|.|.|.?
21+
// #?#?#?#-#
22+
// ?X|.?
23+
// #?#?#
2624
expect(map.findFurthestRoom('^ENWWW$')).to.equal(5);
2725
});
2826

2927
it('execute sample program and return reg 0', () => {
30-
const expected =
31-
`#?#?#?#?#
32-
?.|.|.|.?
33-
#-#?#?#?#
34-
?.|.|.|.?
35-
#?#?#?#-#
36-
?X|.?
37-
#?#?#`;
28+
// #?#?#?#?#
29+
// ?.|.|.|.?
30+
// #-#?#?#?#
31+
// ?.|.|.|.?
32+
// #?#?#?#-#
33+
// ?X|.?
34+
// #?#?#
3835
expect(map.findFurthestRoom('^ENWWW(NEEE)$')).to.equal(9);
3936
});
4037

4138
it('execute sample program and return reg 0', () => {
42-
const expected =
43-
`#?#
44-
?.?
45-
#-#?#?#?#
46-
?.|.|.|.?
47-
#?#?#?#-#
48-
#.|.?X|.?
49-
#?#?#?#?#`;
39+
// #?#
40+
// ?.?
41+
// #-#?#?#?#
42+
// ?.|.|.|.?
43+
// #?#?#?#-#
44+
// #.|.?X|.?
45+
// #?#?#?#?#
5046
expect(map.findFurthestRoom('^ENWWW(N|SE)$')).to.equal(7);
5147
});
5248

day-21-chronal-conversion/assembly.js

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
class Opcode {
2+
constructor(name, runFunction) {
3+
this.name = name;
4+
this.runFunction = runFunction;
5+
}
6+
7+
run(input1, input2, output, registers) {
8+
registers[output] = this.runFunction(input1, input2, registers);
9+
}
10+
}
11+
12+
const opcodes = {};
13+
addOpCode('addr', (a, b, registers) => {
14+
return registers[a] + registers[b];
15+
});
16+
addOpCode('addi', (a, b, registers) => {
17+
return registers[a] + b;
18+
});
19+
addOpCode('mulr', (a, b, registers) => {
20+
return registers[a] * registers[b];
21+
});
22+
addOpCode('muli', (a, b, registers) => {
23+
return registers[a] * b;
24+
});
25+
addOpCode('banr', (a, b, registers) => {
26+
return registers[a] & registers[b];
27+
});
28+
addOpCode('bani', (a, b, registers) => {
29+
return registers[a] & b;
30+
});
31+
addOpCode('borr', (a, b, registers) => {
32+
return registers[a] | registers[b];
33+
});
34+
addOpCode('bori', (a, b, registers) => {
35+
return registers[a] | b;
36+
});
37+
addOpCode('setr', (a, b, registers) => {
38+
return registers[a];
39+
});
40+
addOpCode('seti', (a) => {
41+
return a;
42+
});
43+
44+
addOpCode('gtir', (a, b, registers) => {
45+
return a > registers[b] ? 1 : 0;
46+
});
47+
addOpCode('gtri', (a, b, registers) => {
48+
return registers[a] > b ? 1 : 0;
49+
});
50+
addOpCode('gtrr', (a, b, registers) => {
51+
return registers[a] > registers[b] ? 1 : 0;
52+
});
53+
54+
addOpCode('eqir', (a, b, registers) => {
55+
return a === registers[b] ? 1 : 0;
56+
});
57+
addOpCode('eqri', (a, b, registers) => {
58+
return registers[a] === b ? 1 : 0;
59+
});
60+
addOpCode('eqrr', (a, b, registers) => {
61+
return registers[a] === registers[b] ? 1 : 0;
62+
});
63+
64+
function addOpCode(name, op) {
65+
opcodes[name] = new Opcode(name, op);
66+
}
67+
68+
function runInstruction(input) {
69+
let inputLines = input.split(/\r?\n/).map((line) => line.trim());
70+
let registers = setUpRegisters(inputLines.shift());
71+
let instruction = inputLines.shift().split(' ');
72+
opcodes[instruction[0]].run(+instruction[1],+instruction[2],+instruction[3], registers);
73+
return registers;
74+
}
75+
76+
function setUpRegisters(line) {
77+
const parse = line.replace(/ /g,'').match(/(.+):\[(.+)\]/);
78+
return parse[2].split(',').map(Number);
79+
}
80+
81+
function executeProgram(input, register0Value = 0) {
82+
let inputLines = input.split(/\r?\n/).map((line) => line.substring(0, line.indexOf('/')).trim());
83+
let result = inputLines.shift().match(/#ip (\d+)/);
84+
const ipBind = result[1];
85+
console.log('IP bound to ',ipBind);
86+
let registers = [register0Value,0,0,0,0,0];
87+
let ip = registers[ipBind];
88+
//console.log('ip='+ip,registers.slice(), inputLines[ip]);
89+
while(ip >= 0 && ip < inputLines.length) {
90+
let instruction = inputLines[ip].split(' ');
91+
// //OPTIMISER
92+
// if (ip == 2 && registers[1] != 0) {
93+
// //console.log('>>>>>in optimizer');
94+
// if ((registers[4] % registers[1])==0){
95+
// registers[0] += registers[1];
96+
// }
97+
// registers[5] = 0;
98+
// registers[3] = registers[4];
99+
// ip=12;
100+
// continue;
101+
// }
102+
103+
opcodes[instruction[0]].run(+instruction[1],+instruction[2],+instruction[3], registers);
104+
//console.log('res',registers);
105+
registers[ipBind]++;
106+
ip = registers[ipBind];
107+
}
108+
return registers[0];
109+
}
110+
111+
module.exports.runInstruction = runInstruction;
112+
module.exports.executeProgram = executeProgram;

day-21-chronal-conversion/input.txt

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ip 4
2+
seti 123 0 5
3+
bani 5 456 5
4+
eqri 5 72 5
5+
addr 5 4 4
6+
seti 0 0 4
7+
seti 0 7 5
8+
bori 5 65536 3
9+
seti 733884 6 5
10+
bani 3 255 1
11+
addr 5 1 5
12+
bani 5 16777215 5
13+
muli 5 65899 5
14+
bani 5 16777215 5
15+
gtir 256 3 1
16+
addr 1 4 4
17+
addi 4 1 4
18+
seti 27 8 4
19+
seti 0 6 1
20+
addi 1 1 2
21+
muli 2 256 2
22+
gtrr 2 3 2
23+
addr 2 4 4
24+
addi 4 1 4
25+
seti 25 4 4
26+
addi 1 1 1
27+
seti 17 8 4
28+
setr 1 7 3
29+
seti 7 0 4
30+
eqrr 5 0 1 //if (R0 == R5) halt else goto line:6
31+
addr 1 4 4
32+
seti 5 9 4

day-21-chronal-conversion/test.js

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
const expect = require('chai').expect;
2+
const fs = require('fs');
3+
4+
const assembly = require('./assembly');
5+
6+
describe.only('Day 21: Chronal Conversion', () => {
7+
8+
describe('Test instructions all work', () => {
9+
it('should run addr op correctly', () => {
10+
const input =
11+
`Before: [3, 2, 1, 1]
12+
addr 0 1 3`;
13+
expect(assembly.runInstruction(input)).to.eql([3, 2, 1, 5]);
14+
});
15+
16+
it('should run addi op correctly', () => {
17+
const input =
18+
`Before: [3, 2, 1, 1]
19+
addi 0 7 3`;
20+
expect(assembly.runInstruction(input)).to.eql([3, 2, 1, 10]);
21+
});
22+
23+
it('should run mulr op correctly', () => {
24+
const input =
25+
`Before: [3, 2, 1, 1]
26+
mulr 0 1 3`;
27+
expect(assembly.runInstruction(input)).to.eql([3, 2, 1, 6]);
28+
});
29+
30+
it('should run muli op correctly', () => {
31+
const input =
32+
`Before: [3, 2, 1, 1]
33+
muli 0 3 3`;
34+
expect(assembly.runInstruction(input)).to.eql([3, 2, 1, 9]);
35+
});
36+
37+
it('should run banr op correctly', () => {
38+
const input =
39+
`Before: [3, 2, 1, 1]
40+
banr 0 1 3`;
41+
expect(assembly.runInstruction(input)).to.eql([3, 2, 1, 2]);
42+
});
43+
44+
it('should run bani op correctly', () => {
45+
const input =
46+
`Before: [3, 2, 1, 1]
47+
bani 0 1 3`;
48+
expect(assembly.runInstruction(input)).to.eql([3, 2, 1, 1]);
49+
});
50+
51+
it('should run borr op correctly', () => {
52+
const input =
53+
`Before: [1, 2, 1, 1]
54+
borr 0 1 3`;
55+
expect(assembly.runInstruction(input)).to.eql([1, 2, 1, 3]);
56+
});
57+
58+
it('should run bori op correctly', () => {
59+
const input =
60+
`Before: [3, 2, 1, 1]
61+
bori 0 4 3`;
62+
expect(assembly.runInstruction(input)).to.eql([3, 2, 1, 7]);
63+
});
64+
65+
it('should run setr op correctly', () => {
66+
const input =
67+
`Before: [3, 2, 1, 1]
68+
setr 0 1 3`;
69+
expect(assembly.runInstruction(input)).to.eql([3, 2, 1, 3]);
70+
});
71+
72+
it('should run seti op correctly', () => {
73+
const input =
74+
`Before: [3, 2, 1, 1]
75+
seti 4 4 3`;
76+
expect(assembly.runInstruction(input)).to.eql([3, 2, 1, 4]);
77+
});
78+
79+
it('should run gtir op correctly', () => {
80+
let input =
81+
`Before: [3, 2, 1, 1]
82+
gtir 3 1 3`;
83+
expect(assembly.runInstruction(input)).to.eql([3, 2, 1, 1]);
84+
input =
85+
`Before: [3, 2, 1, 1]
86+
gtir 1 1 3`;
87+
expect(assembly.runInstruction(input)).to.eql([3, 2, 1, 0]);
88+
});
89+
90+
it('should run gtri op correctly', () => {
91+
let input =
92+
`Before: [3, 2, 1, 1]
93+
gtri 0 2 3`;
94+
expect(assembly.runInstruction(input)).to.eql([3, 2, 1, 1]);
95+
input =
96+
`Before: [3, 2, 1, 1]
97+
gtir 0 4 3`;
98+
expect(assembly.runInstruction(input)).to.eql([3, 2, 1, 0]);
99+
});
100+
101+
it('should run gtrr op correctly', () => {
102+
let input =
103+
`Before: [3, 2, 1, 1]
104+
gtrr 0 1 3`;
105+
expect(assembly.runInstruction(input)).to.eql([3, 2, 1, 1]);
106+
input =
107+
`Before: [3, 4, 1, 1]
108+
gtir 0 1 3`;
109+
expect(assembly.runInstruction(input)).to.eql([3, 4, 1, 0]);
110+
});
111+
112+
it('should run eqir op correctly', () => {
113+
let input =
114+
`Before: [3, 3, 1, 1]
115+
eqir 3 1 3`;
116+
expect(assembly.runInstruction(input)).to.eql([3, 3, 1, 1]);
117+
input =
118+
`Before: [3, 2, 1, 1]
119+
eqir 1 1 3`;
120+
expect(assembly.runInstruction(input)).to.eql([3, 2, 1, 0]);
121+
});
122+
123+
it('should run eqri op correctly', () => {
124+
let input =
125+
`Before: [3, 2, 1, 1]
126+
eqri 0 3 3`;
127+
expect(assembly.runInstruction(input)).to.eql([3, 2, 1, 1]);
128+
input =
129+
`Before: [3, 2, 1, 1]
130+
eqri 0 4 3`;
131+
expect(assembly.runInstruction(input)).to.eql([3, 2, 1, 0]);
132+
});
133+
134+
it('should run eqrr op correctly', () => {
135+
let input =
136+
`Before: [3, 3, 1, 1]
137+
eqrr 0 1 3`;
138+
expect(assembly.runInstruction(input)).to.eql([3, 3, 1, 1]);
139+
input =
140+
`Before: [3, 4, 1, 1]
141+
eqrr 0 1 3`;
142+
expect(assembly.runInstruction(input)).to.eql([3, 4, 1, 0]);
143+
});
144+
});
145+
146+
147+
describe('Part One', () => {
148+
it('execute input file program and return reg 0', () => {
149+
const input = fs.readFileSync('day-19-go-with-the-flow/input.txt').toString();
150+
expect(assembly.executeProgram(input)).to.equal(1248);
151+
}).timeout(10000);
152+
});
153+
154+
// describe('Part Two', () => {
155+
// it('execute input file program and return reg 0', () => {
156+
// const input = fs.readFileSync('day-19-go-with-the-flow/input.txt').toString();
157+
// expect(assembly.executeProgram(input,1)).to.equal(1248);
158+
// }).timeout(10000);
159+
// });
160+
});

0 commit comments

Comments
 (0)