Skip to content

Commit b9deb2c

Browse files
feat(2020-day-08): execute commands from the language
1 parent 7f9cb68 commit b9deb2c

File tree

2 files changed

+73
-24
lines changed

2 files changed

+73
-24
lines changed

2020/day-08/runProgram.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,51 @@ const program = [
1919
'acc +6'
2020
]
2121

22+
let accumulator = 0
23+
let position = 1
24+
25+
/**
26+
* API of commands this language supports
27+
*/
28+
const api = {
29+
nop: (a, b) => {
30+
console.debug(`doing a nop ${b}`)
31+
return a + 1
32+
},
33+
acc: (a, b) => {
34+
console.debug(`adding ${b} to accumulator`)
35+
accumulator += api[b.substr(0, 1)](
36+
0,
37+
Number(b.substr((1)))
38+
)
39+
return a + 1
40+
},
41+
jmp: (a, b) => {
42+
console.debug(`jumping from ${a} ${b} `)
43+
return a + api[b.substr(0, 1)](
44+
0,
45+
Number(b.substr((1)))
46+
)
47+
},
48+
'+': (x, y) => x + y,
49+
'-': (x, y) => x - y
50+
}
51+
2252
const parseCommand = (inst) => {
2353
console.debug('Parsing ', inst)
2454
const [cmd, arg] = inst.split(' ')
2555
return { cmd, arg }
2656
}
2757

58+
const execInstruction = (inst, instKey = 0, evKey = 0) => {
59+
const { cmd, arg } = parseCommand(inst)
60+
// Run the command
61+
// Support jumping by passing back next
62+
position = api[cmd](instKey, arg)
63+
logEvent({ instKey, evKey })
64+
return position
65+
}
66+
2867
const formatLogRow = (command, idx, program) => {
2968
let countStr
3069
if (!log[idx]) {
@@ -73,7 +112,9 @@ const logEvent = ({ instKey, evKey }) => {
73112

74113
module.exports = {
75114
run: console.log('run'),
76-
executeStep: console.log('executeStep'),
115+
getAccumulator: () => accumulator,
116+
getPosition: () => position,
117+
execInstruction,
77118
parseCommand,
78119
logEvent,
79120
displayLog

2020/day-08/runProgram.test.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-env mocha */
22
const { expect } = require('chai')
3-
const { run, parseCommand, logEvent, displayLog } = require('./runProgram')
3+
const { run, getPosition, getAccumulator, execInstruction, parseCommand, logEvent, displayLog } = require('./runProgram')
44

55
const exampleLog = `
66
nop +0 | 1
@@ -21,28 +21,36 @@ describe('--- Day 8: Handheld Halting ---', () => {
2121
expect(false).to.equal(true)
2222
})
2323
})
24-
// xdescribe('execInstruction()', () => {
25-
// it('executes a specified command', () => {
26-
// execInstruction()
27-
// expect(false).to.equal(true)
28-
// })
29-
// it('steps to the next sequential command', () => {
30-
// logEvent()
31-
// expect(false).to.equal(true)
32-
// })
33-
// it('can execute a `nop` command which does nothing', () => {
34-
// execInstruction('nop')
35-
// expect(false).to.equal(true)
36-
// })
37-
// it('can execute a `acc` command which increments the accumulator', () => {
38-
// execInstruction('acc')
39-
// expect(false).to.equal(true)
40-
// })
41-
// it('can execute a `jmp` command which jumps to a different command in the instruction set', () => {
42-
// execInstruction('jmp')
43-
// expect(false).to.equal(true)
44-
// })
45-
// })
24+
describe('execInstruction()', () => {
25+
it('executes a specified command', () => {
26+
expect(getPosition()).to.equal(1)
27+
expect(execInstruction('acc +3', 300, 600)).to.equal(301)
28+
expect(getAccumulator()).to.equal(3)
29+
expect(getPosition()).to.equal(301)
30+
})
31+
xit('steps to the next sequential command', () => {
32+
// logEvent()
33+
// expect(false).to.equal(true)
34+
})
35+
it('can execute a `nop` command which does nothing', () => {
36+
const acc = getAccumulator()
37+
expect(execInstruction('nop +3', 999, 600)).to.equal(1000)
38+
expect(getPosition()).to.equal(1000)
39+
expect(getAccumulator()).to.equal(acc)
40+
})
41+
it('can execute a `acc` command which increments the accumulator', () => {
42+
const acc = getAccumulator()
43+
expect(execInstruction('acc +100', 1234, 600)).to.equal(1235)
44+
expect(getPosition()).to.equal(1235)
45+
expect(getAccumulator()).to.equal(acc + 100)
46+
})
47+
it('can execute a `jmp` command which jumps to a different command in the instruction set', () => {
48+
const acc = getAccumulator()
49+
expect(execInstruction('jmp -23', 400, 600)).to.equal(377)
50+
expect(getPosition()).to.equal(377)
51+
expect(getAccumulator()).to.equal(acc)
52+
})
53+
})
4654
describe('parseCommand()', () => {
4755
it('parses an instruction string into a structured command object', () => {
4856
const instructions = [

0 commit comments

Comments
 (0)