Solve 2020/09

master
Alfred Melch 4 years ago
parent 53b89e5034
commit 514b90c84e

@ -0,0 +1,20 @@
35
20
15
25
47
40
62
55
65
95
102
117
150
182
127
219
299
277
309
576

File diff suppressed because it is too large Load Diff

@ -0,0 +1,44 @@
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));
}

@ -0,0 +1,70 @@
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;
}
}
let weakness = null;
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)) {
weakness = val;
break;
}
buffer.push(parseInt(line));
}
const stream = lines.map((line) => parseInt(line));
let lower = 0;
let upper = 0;
let sum = stream[0];
while (sum !== weakness && lower < stream.length) {
while (sum > weakness) {
sum -= stream[upper];
upper--;
}
while (sum < weakness) {
upper++;
sum += stream[upper];
}
sum -= stream[lower];
lower++;
}
const range = stream.slice(lower, upper + 1);
const [min, max] = [Math.min(...range), Math.max(...range)];
console.log({ lower, upper, range, min, max });
console.log("Solution:");
console.log(min + max);

@ -0,0 +1,41 @@
const fs = require("fs");
exports.readInput = function (filename = "input") {
const input = fs.readFileSync(filename, "utf-8").trim();
const lines = input.split("\n");
const paragraphs = input.split("\n\n");
let colLength = lines[0].length;
let colConsistentcy = true;
for (const line of lines) {
if (colLength != line.length) {
colConsistentcy = false;
}
}
const avgLength = sum(lines.map((l) => l.length)) / lines.length;
console.log("Number of lines:", lines.length);
console.log("Number of paragraphs", paragraphs.length);
console.log("Col-length is consistent:", colConsistentcy);
console.log("Average col-length", avgLength);
console.log("Sample:");
for (const line of lines.slice(0, 5)) {
console.log(line);
}
console.log("...");
for (const line of lines.slice(lines.length - 5, lines.length)) {
console.log(line);
}
console.log("=".repeat(process.stdout.columns));
return {
raw: input,
lines,
paragraphs,
};
};
const sum = (arr) => arr.reduce((a, b) => a + b, 0);
exports.sum = sum;
Loading…
Cancel
Save