2020 day 10
This commit is contained in:
parent
1590f4be6e
commit
bb6d22cd13
7 changed files with 259 additions and 0 deletions
18
2020/10/part1.js
Normal file
18
2020/10/part1.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
const fs = require('fs');
|
||||
const data = fs.readFileSync('input.txt', 'utf-8');
|
||||
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)
|
Loading…
Add table
Add a link
Reference in a new issue