This commit is contained in:
Jack Bond-Preston 2018-12-05 20:26:42 +00:00
parent c6c58f12ec
commit 0353778bfb
2 changed files with 37 additions and 0 deletions

1
aoc-5/inputs.txt Normal file

File diff suppressed because one or more lines are too long

36
aoc-5/main.js Normal file
View File

@ -0,0 +1,36 @@
let fs = require("fs");
let input = fs.readFileSync("inputs.txt", "utf8").split('');
function swapCase(c) {
return c === c.toUpperCase() ? c.toLowerCase() : c.toUpperCase();
}
function collapsePolymer(polymer) {
for (let i = 0; i < polymer.length; i++) {
let c = polymer[i];
if (i !== polymer.length - 1 && c === swapCase(polymer[i + 1])) {
polymer.splice(i, 2);
i = Math.max(-1, i - 2);
}
}
return polymer.length;
}
let shortest = Math.min(...
'abcdefghijklmnopqrstuvwxyz'
.split('')
.map(
x => collapsePolymer(
input.filter(
y => y.toUpperCase() !== x.toUpperCase()
)
)
)
);
console.log(`[Part 1] Polymer length: ${ collapsePolymer(input) }`);
console.log(`[Part 2] Shortest polymer length: ${ shortest }`);