DEV Community

Discussion on: Advent of Code 2020 Solution Megathread - Day 3: Toboggan Trajectory

Collapse
 
adamss profile image
Adam

Vanilla JS solution in 12 lines of code:

//create arr with input as template literal

let newArr= arr.split(/\r?\n/);//split by endlines
let xCord = 0;//X coordinates
let counter = 0;//trees hit counter
for(let i = 0 ; i < newArr.length ; i++){
let temp = newArr[i].split('');//split into array of characters
if(temp[xCord]=='#'){//checking for trees
counter++;//if there is a tree increment the counter
}
xCord = xCord +3;//add +3 to the right
if(xCord>=31){//check if there is more coords than elements in array
xCord = xCord-31;//reset coords
}}
console.log(counter);//result