You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
887 B
JavaScript

4 years ago
const { readInput } = require("./util");
const { lines } = readInput("input");
class Buffer {
constructor(size = 25) {
this.buffer = [];
this.size = size;
}
push(val) {
this.buffer.push(val);
if (this.buffer.length > this.size) {
this.buffer.shift();
}
}
isValid(val) {
if (this.buffer.length < this.size) {
return true;
}
for (let a = 0; a < this.buffer.length; a++) {
const wanted = val - this.buffer[a];
for (let b = a + 1; b < this.buffer.length; b++) {
if (this.buffer[b] === wanted) {
return true;
}
}
}
return false;
}
}
const buffer = new Buffer(25);
for (const line of lines) {
const val = parseInt(line);
console.log(buffer.buffer, val, buffer.isValid(val));
if (!buffer.isValid(val)) {
console.log(val);
break;
}
buffer.push(parseInt(line));
}