Advent of Code — Day 8 — Javascript

For day 8, the input data is a stack of instructions. The stack of instructions, when followed properly, leads to an infinite loop. You are given in the instructions that every line can only be run once and to return the value prior to the start of the second iteration.

The three instructions in the stack are ‘nop’, ‘acc’, and ‘jmp’. The first has no function and should only continue to the next instruction below it. The second will either add or subtract to the current value. And the third will tell where in the next line the function should traverse to.

My initial thoughts are to split by a newline character and run a while loop. We can store each line number into a set or an object and to check if the line already exists prior to executing the operation.

Let’s check the sample data that is provided.

nop +0
acc +1
jmp +4
acc +3
jmp -3
acc -99
acc +1
jmp -4
acc +6

Starting from the top, we can keep a value variable and move along the array.

nop +0 => (1) Does nothing, increment the array by 1
acc +1 => (2) Total = 1, increment the array by 1 (8) Repeats
jmp +4 => (3) Total = 1, increment the array by 4
acc +3 => (6) Total = 5, increment the array by 1
jmp -3 => (7) Total = 5, decrement the array by 3
acc -99
acc +1 => (4) Total = 2, increment the array by 1
jmp -4 => (5) Total = 2, decrement the array by 4
acc +6

Looking at the example, the program runs through 7 different lines of instructions prior to repeating the process and starting the infinite loop. The return value should by the total = 5.

As always, we start off the function by splitting the data by the newline character to allow us to start parsing and we can initialize the set we will be storing.

function day8(input){
const arr = input.split('\n')
let set = new Set()
let total = 0
}
//arr[0] = "nop +0"
//arr[1] = "acc +1"
//arr[2] = "jmp +4"

A for loop could be used, but it would be slightly odd seeing as we will be moving up and down the array based on the data. Instead, I will be using a while loop which will work just as well.

function day8(input){
const arr = input.split('\n')
let set = new Set()
let total = 0
let i = 0
while(i < arr.length){
if(set.has(i)){
return total
}
set.add(i)
}
}

As we move up and down the data, we will add each line into the set to keep a history of where we have traversed. Now, we need to divide up the keys and the operations and then depending on the key, we perform different operations.

function day8(input){
const arr = input.split('\n')
let set = new Set()
let total = 0
let i = 0
while(i < arr.length){
if(set.has(i)){
return total
}
set.add(i)
const key = arr[i].split(' ')
switch(key[0]){
case 'acc':
value += parseInt(key[1]
i++
break
case 'jmp':
i += parseInt(key[1])
break
default:
i++
break
}
}
}

To walk through the code, currently each line is formatted similarly. And by splitting by the white space, we break up the key and its corresponding operations. The parseInt also only takes the numbers in a string and converts it into a single integer. In this case, ignoring the symbol.

arr[0] = "nop +0"
arr[1] = "acc +1"
const key = arr[i].split(' ')arr[0][0] = "nop"
arr[0][1] = "+0"
arr[1][0] = "acc"
arr[1][1] = "+1"
parseInt(key[1]) = 0
parseInt(key[1]) = 1