Skip to content

Commit eac56a6

Browse files
feat(2019-day-02): run add, multiply, and terminate functions
1 parent 2a6a421 commit eac56a6

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

2019/day-02/intcodeParser.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
const add = ({ posIn1, posIn2, posOut, data }) => {
3+
data[posOut] = data[posIn1] + data[posIn2]
4+
}
5+
6+
const multiply = ({ posIn1, posIn2, posOut, data }) => {
7+
data[posOut] = data[posIn1] * data[posIn2]
8+
}
9+
10+
const terminate = ({ data }) => {
11+
console.log('Reached terminator. Stopping.')
12+
}
13+
14+
const step = ({ position, data }) => {
15+
const opCodesMap = {
16+
1: add,
17+
2: multiply,
18+
99: terminate
19+
}
20+
const segment = data.slice(position, position + 4)
21+
// Run the correct opcode for the specified step
22+
opCodesMap[data[position]]({
23+
posIn1: segment[1],
24+
posIn2: segment[2],
25+
posOut: segment[3],
26+
data
27+
})
28+
}
29+
30+
module.exports = {
31+
step
32+
}

0 commit comments

Comments
 (0)