-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay3.js
31 lines (28 loc) · 870 Bytes
/
Day3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"use strict"
//https://adventofcode.com/2020/day/3
//load input
const fs = require('fs')
const encoded = fs.readFileSync("/Users/hakizu/Downloads/message.txt")
const input = encoded.toString('utf-8').split('\n')
//Part1
const rowLength = input.length
const columnLength = input[0].length
function numberOfTrees(columnIncrement, rowIncrement) {
let colIndex = 0
let trees = 0
for (let rowIndex = 0; rowIndex < rowLength; rowIndex += rowIncrement) {
if (input[rowIndex][colIndex % columnLength] === '#') {
trees++
}
colIndex += columnIncrement
}
return trees
}
const answer = numberOfTrees(3, 1)
console.log(answer)
//Part2
const slopes = [[1, 1] , [5, 1], [3, 1], [7, 1], [1, 2]]
const secondAnswer = slopes.map(it => {
return numberOfTrees(it[0], it[1])
})
console.log(secondAnswer.reduce((a, b) => a * b))