Skip to content

Commit 4cc6c1c

Browse files
feat(2020-day-08): run an entire brogram, breaking on infinite loop
1 parent b9deb2c commit 4cc6c1c

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

2020/day-08/runProgram.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const log = []
1+
let log = []
22
log[0] = [1]
33
log[1] = [2, 8]
44
log[2] = [3]
@@ -7,7 +7,7 @@ log[4] = [7]
77
log[6] = [4]
88
log[7] = [5]
99

10-
const program = [
10+
let program = [
1111
'nop +0',
1212
'acc +1',
1313
'jmp +4',
@@ -21,6 +21,7 @@ const program = [
2121

2222
let accumulator = 0
2323
let position = 1
24+
let breaker = false
2425

2526
/**
2627
* API of commands this language supports
@@ -61,9 +62,27 @@ const execInstruction = (inst, instKey = 0, evKey = 0) => {
6162
// Support jumping by passing back next
6263
position = api[cmd](instKey, arg)
6364
logEvent({ instKey, evKey })
65+
// break out when reaching an infinite loop
66+
if (log[instKey].length > 1) {
67+
breaker = true
68+
}
6469
return position
6570
}
6671

72+
const run = (insts) => {
73+
program = insts
74+
accumulator = 0
75+
position = 0
76+
log = []
77+
let evKey = 0
78+
79+
// eslint-disable-next-line no-unmodified-loop-condition
80+
while (breaker === false) {
81+
evKey++
82+
execInstruction(program[position], position, evKey)
83+
}
84+
}
85+
6786
const formatLogRow = (command, idx, program) => {
6887
let countStr
6988
if (!log[idx]) {
@@ -111,7 +130,7 @@ const logEvent = ({ instKey, evKey }) => {
111130
}
112131

113132
module.exports = {
114-
run: console.log('run'),
133+
run,
115134
getAccumulator: () => accumulator,
116135
getPosition: () => position,
117136
execInstruction,

2020/day-08/runProgram.test.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
const { expect } = require('chai')
33
const { run, getPosition, getAccumulator, execInstruction, parseCommand, logEvent, displayLog } = require('./runProgram')
44

5+
const exampleProgram = [
6+
'nop +0',
7+
'acc +1',
8+
'jmp +4',
9+
'acc +3',
10+
'jmp -3',
11+
'acc -99',
12+
'acc +1',
13+
'jmp -4',
14+
'acc +6'
15+
]
516
const exampleLog = `
617
nop +0 | 1
718
acc +1 | 2, 8(!)
@@ -15,12 +26,6 @@ acc +6 | `
1526

1627
describe('--- Day 8: Handheld Halting ---', () => {
1728
describe('Part 1', () => {
18-
xdescribe('run()', () => {
19-
it('executes the steps of a given program', () => {
20-
run()
21-
expect(false).to.equal(true)
22-
})
23-
})
2429
describe('execInstruction()', () => {
2530
it('executes a specified command', () => {
2631
expect(getPosition()).to.equal(1)
@@ -84,5 +89,14 @@ describe('--- Day 8: Handheld Halting ---', () => {
8489
)
8590
})
8691
})
92+
describe('run()', () => {
93+
it('executes the steps of a given program', () => {
94+
run(exampleProgram)
95+
// stops at infinite loop
96+
expect(getPosition()).to.equal(2)
97+
expect(getAccumulator()).to.equal(6)
98+
expect(displayLog()).to.equal(exampleLog)
99+
})
100+
})
87101
})
88102
})

0 commit comments

Comments
 (0)