Day 9
This commit is contained in:
23
09/part1.js
Normal file
23
09/part1.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const fs = require('fs');
|
||||
const PREAMBLE_SIZE = 25;
|
||||
|
||||
fs.readFile('input', (err, data) => {
|
||||
if (err) throw err;
|
||||
let input = data.toString().trim().split("\n").map((line) => Number(line) );
|
||||
|
||||
let buf = input.slice(0, PREAMBLE_SIZE);
|
||||
for(pos=PREAMBLE_SIZE;pos<input.length;pos++) {
|
||||
let next = input[pos];
|
||||
|
||||
let sums = new Set( );
|
||||
buf.forEach((a) => buf.forEach((b) => { if (a != b) sums.add(a+b); }));
|
||||
|
||||
if (!sums.has(next)) {
|
||||
console.log(next);
|
||||
break;
|
||||
}
|
||||
|
||||
buf = buf.slice(1, 25);
|
||||
buf.push(next);
|
||||
}
|
||||
});
|
35
09/part2.js
Normal file
35
09/part2.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const fs = require('fs');
|
||||
const PREAMBLE_SIZE = 25;
|
||||
|
||||
function breakIt(input, target) {
|
||||
for (i=0;i<input.length;i++) {
|
||||
for(j=i+1;j<input.length;j++) {
|
||||
let subset = input.slice(i, j);
|
||||
if (target == subset.reduce((a,b) => a+b)) {
|
||||
console.log(Math.min(...subset) + Math.max(...subset))
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fs.readFile('input', (err, data) => {
|
||||
if (err) throw err;
|
||||
let input = data.toString().trim().split("\n").map((line) => Number(line) );
|
||||
|
||||
let buf = input.slice(0, PREAMBLE_SIZE);
|
||||
for(pos=PREAMBLE_SIZE;pos<input.length;pos++) {
|
||||
let next = input[pos];
|
||||
|
||||
let sums = new Set();
|
||||
buf.forEach((a) => buf.forEach((b) => { if (a != b) sums.add(a+b); }));
|
||||
|
||||
if (!sums.has(next)) {
|
||||
breakIt(input, next);
|
||||
return;
|
||||
}
|
||||
|
||||
buf = buf.slice(1, 25);
|
||||
buf.push(next);
|
||||
}
|
||||
});
|
Reference in New Issue
Block a user