jsadvent/solutions/2018/5.js
2020-12-10 02:33:00 -06:00

43 lines
978 B
JavaScript

const { Glib } = require('../../lib');
const fuelFor = (mass) => mass / 3n - 2n;
function* tyrant(mass) {
while ((mass = fuelFor(mass)) > 0n) {
yield mass;
}
}
const react = (input) => {
let lastLength;
do {
lastLength = input.length;
for (let i = 0; i < input.length - 1; i++) {
if (
input[i] !== input[i + 1] &&
input[i].toUpperCase() === input[i + 1].toUpperCase()
) {
input = input.splice(i, 2);
}
}
} while (input.length < lastLength);
return input;
};
module.exports = {
'1': (input) => react(input).length,
2: (input) => {
// pre-reacting saves a ton of time and makes no moves that invalidate others
input = react(input);
return Glib.fromIterable(input)
.map((i) => i.toLowerCase())
.unique()
.map((char) => {
const reduced = react(input.replace(new RegExp(char, 'gi'), ''));
return reduced.length;
})
.toInts()
.min();
},
};