Skip to content

Commit 7f9cb68

Browse files
feat(2020-day-08): command parser to convert strings into usable commands
1 parent 86d0d44 commit 7f9cb68

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

2020/day-08/runProgram.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ const program = [
1919
'acc +6'
2020
]
2121

22+
const parseCommand = (inst) => {
23+
console.debug('Parsing ', inst)
24+
const [cmd, arg] = inst.split(' ')
25+
return { cmd, arg }
26+
}
27+
2228
const formatLogRow = (command, idx, program) => {
2329
let countStr
2430
if (!log[idx]) {
@@ -68,6 +74,7 @@ const logEvent = ({ instKey, evKey }) => {
6874
module.exports = {
6975
run: console.log('run'),
7076
executeStep: console.log('executeStep'),
77+
parseCommand,
7178
logEvent,
7279
displayLog
7380
}

2020/day-08/runProgram.test.js

Lines changed: 35 additions & 21 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, executeStep, logEvent, displayLog } = require('./runProgram')
3+
const { run, parseCommand, logEvent, displayLog } = require('./runProgram')
44

55
const exampleLog = `
66
nop +0 | 1
@@ -21,26 +21,40 @@ describe('--- Day 8: Handheld Halting ---', () => {
2121
expect(false).to.equal(true)
2222
})
2323
})
24-
xdescribe('executeStep()', () => {
25-
it('executes a specified command', () => {
26-
executeStep()
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-
executeStep('nop')
35-
expect(false).to.equal(true)
36-
})
37-
it('can execute a `acc` command which increments the accumulator', () => {
38-
executeStep('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-
executeStep('jmp')
43-
expect(false).to.equal(true)
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+
// })
46+
describe('parseCommand()', () => {
47+
it('parses an instruction string into a structured command object', () => {
48+
const instructions = [
49+
'jmp +4',
50+
'acc +3',
51+
'jmp -3',
52+
'acc -99'
53+
]
54+
instructions.forEach((inst) => {
55+
const { cmd, arg } = parseCommand(inst)
56+
expect(`${cmd} ${arg}`).to.equal(inst)
57+
})
4458
})
4559
})
4660
describe('logEvent()', () => {

0 commit comments

Comments
 (0)