Skip to content

Commit cffa302

Browse files
fix(2019-day-02): failures when exceeding Javascript int sizes
1 parent 40a76d9 commit cffa302

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

2019/day-02/intcodeParser.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
21
const add = ({ posIn1, posIn2, posOut, data }) => {
3-
data[posOut] = data[posIn1] + data[posIn2]
2+
console.debug(`Adding ${data[Number(posIn1)]} to ${data[Number(posIn2)]}`)
3+
data[posOut] = data[Number(posIn1)] + data[Number(posIn2)]
44
return true
55
}
66

77
const multiply = ({ posIn1, posIn2, posOut, data }) => {
8-
data[posOut] = data[posIn1] * data[posIn2]
8+
console.debug(`Multiplying ${data[Number(posIn1)]} with ${data[Number(posIn2)]}`)
9+
data[posOut] = data[Number(posIn1)] * data[Number(posIn2)]
910
return true
1011
}
1112

@@ -15,12 +16,14 @@ const terminate = ({ position }) => {
1516
}
1617

1718
const step = ({ position, data }) => {
19+
console.debug(`Step: ${position}`)
1820
const opCodesMap = {
1921
1: add,
2022
2: multiply,
2123
99: terminate
2224
}
2325
const segment = data.slice(position, position + 4)
26+
2427
// Run the correct opcode for the specified step
2528
return opCodesMap[data[position]]({
2629
posIn1: segment[1],
@@ -34,6 +37,14 @@ const step = ({ position, data }) => {
3437
const runProgram = ({ data }) => {
3538
let position = 0
3639
let running = true
40+
41+
// Convert to BigInts because operations will exceed 53bit integers
42+
// TODO: Standard chokes on this ES2020 feature. Remove eslint-disable
43+
// once fixed.
44+
// See https://github.com/standard/standard/issues/1436
45+
// eslint-disable-next-line no-undef
46+
data.forEach((key, idx) => { data[idx] = BigInt(key) })
47+
3748
while (running === true && position <= data.length) {
3849
running = step({ position, data })
3950
position += 4

2019/day-02/intcodeParser.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ describe('--- 2019 Day 2: 1202 Program Alarm ---', () => {
4040
[2, 4, 4, 5, 99, 9801],
4141
[30, 1, 1, 4, 2, 5, 6, 0, 99]
4242
]
43+
// Convert outputs to BigInts
44+
testOutputs.forEach((out, idx) => {
45+
out.forEach((value, idx2) => {
46+
// TODO: Standard chokes on this ES2020 feature. Remove eslint-disable
47+
// once fixed.
48+
// See https://github.com/standard/standard/issues/1436
49+
// eslint-disable-next-line no-undef
50+
testOutputs[idx][idx2] = BigInt(value)
51+
})
52+
})
53+
4354
testInputs.forEach((data, idx) => {
4455
runProgram({ data })
4556
expect(data).to.deep.equal(testOutputs[idx])

0 commit comments

Comments
 (0)