Day 5
This commit is contained in:
parent
c6c58f12ec
commit
0353778bfb
1
aoc-5/inputs.txt
Normal file
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
36
aoc-5/main.js
Normal 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 }`);
|
Loading…
Reference in New Issue
Block a user