Skip to content

Instantly share code, notes, and snippets.

@plvhx
Created January 22, 2021 07:42
Show Gist options
  • Save plvhx/268c58457d01c1d8cd3af0f37a18a1e7 to your computer and use it in GitHub Desktop.
Save plvhx/268c58457d01c1d8cd3af0f37a18a1e7 to your computer and use it in GitHub Desktop.
advent of code 2020 day 3 part 2
const fs = require('fs')
const splitted = fs.readFileSync('./input.txt', 'utf-8').split(String.fromCharCode(0x0a))
function normalizePattern(splt) {
let nSplt = splt
let normLen = 90
for (let i = 0; i < (splt.length - 1); i++) {
nSplt[i] = splt[i].repeat(normLen)
}
return nSplt
}
function countTree(splt, right, down) {
let numTree = 0, position = right
for (let i = down; i < splt.length; i += down) {
if (splt[i].charAt(position) == '#') {
numTree++
}
position += right
}
return numTree
}
normalized = normalizePattern(splitted)
list = [
countTree(normalized, 1, 1),
countTree(normalized, 3, 1),
countTree(normalized, 5, 1),
countTree(normalized, 7, 1),
countTree(normalized, 1, 2)
]
console.log(list.reduce((x, y) => x * y))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment