aoc/2020/10/part1.js

18 lines
559 B
JavaScript
Raw Permalink Normal View History

2020-12-10 22:07:29 +00:00
const fs = require('fs');
2020-12-12 18:34:40 +00:00
const data = fs.readFileSync('./input.txt', 'utf-8');
2020-12-10 22:07:29 +00:00
const lines = data.split('\n').map(num => parseInt(num)).sort((a,b)=>(a-b));
lines.unshift(0); // add the outlet with joltage 0
lines.push(lines[lines.length-1]+3); // your device has a joltage 3 above the highest
let threes = 0; // keep track of differences for puzzle solution
let ones = 0;
for (let i = 1; i < lines.length; i++) {
switch (lines[i]-lines[i-1]) {
case 3:
threes++;
break;
case 1:
ones++;
break;
}
}
console.log(ones * threes)