Advent of Code — Day 15 — Javascript

Vincent Yang
3 min readMar 16, 2021

--

For day 15, the problem is quite tricky as the order of operations is difficult to nail down.

You are given an array of numbers — and in this case an array of length 3. The function goes as such — the array of numbers is iterated through and every subsequent number checks the count of the number that went previously. If the previous number has only appeared once, then the next number is zero. If it has appeared more than once, then the next number is the current iteration subtracted by the previous occurrence of the number.

In the example provided, they give us the starting array as [0, 3, 6] and the objective is to find the value of the 2020th iteration.

Turn 1: Add 0 
Turn 2: Add 3
Turn 3: Add 6
Turn 4: As the previous number, 6, only appeared once, add 0
Turn 5: As the previous number, 0, appeared more than once, subtract the turns of the previous two occurrences: 4 - 1 = Add 3
Turn 6: As the previous number, 3, appeared more than once, subtract the turns of the previous two occurrences: 5 - 2 = Add 3
Turn 7: As the previous number, 3, appeared more than once, subtract the turns of the previous two occurrences: 6 - 5 = Add 1
Turn 8: As the previous number, 1, only appeared once, add 0
Turn 9: As the previous number, 0, appeared more than once, subtract the turns of the previous two occurrences: 8 - 4 = Add 4
Turn 10: As the previous number, 4, only appeared once, add 0
Turn 11: As the previous number, 0 appeared more than once, subtract the turns of the previous two occurrences: 10 - 8 = Add 2

The things that we must keep in mind for this problem are: maintaining index of prior occurrence, maintaining count of the occurrence, and maintaining order.

function day15(){
const nums = [0, 3, 6]
let spoke = new Map()
}

As the time for computation will be in the thousands, a Map() will offer higher speeds based on its stricter specification.

function day15(){
const nums = [0, 3, 6]
let spoke = new Map()
for(let i = 0; i < 2020; i++){
if(nums[i] || nums[i] == 0){
spoke.set(starting[i], i + 1)
}
}
}

What this will do is given the starting set of numbers, we place them into the map object and assign them the turn value. The turn value is i + 1 because we need to start on turn 1, not the initial 0 index in Javascript.

function day15(){
const nums = [0, 3, 6]
let spoke = new Map()
let said
for(let i = 0; i < 2020; i++){
if(nums[i] || nums[i] == 0){
said = starting[i]
spoke.set(said, i + 1)
} else if(!(spoken.has(said)){
spoken.set(said, i)
said = 0
} else {
let temp = spoken.get(just)
spoken.set(just, i)
said = i - temp
}
}
}

It’s all coming together now. After the initial if statement, the counter will increment out of bounds of the initial array. This will cause the else statement to trigger and since the variable ‘said’ still holds the previous element on the 4th iteration, it will set the hold the previous iteration in a temp value, reset the iteration and then on the next iteration, set the value of ‘said’.

--

--

No responses yet