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

36 lines
936 B
JavaScript

const { Glib } = require('../../lib');
module.exports = {
'1': (input) =>
Glib.fromLines(input)
.map((boxId) => new Set(boxId.characterFrequencies.values()))
.reduce(
([two, three], counts) => [
two + (counts.has(2n) ? 1n : 0n),
three + (counts.has(3n) ? 1n : 0n),
],
[0n, 0n],
)
.glib.product(),
'2': (input) => {
const boxIds = Glib.fromLines(input).map(
(boxId) =>
Glib.fromSequence(0, boxId.length - 1).map((i) =>
boxId.splice(Number(i), 1),
).array,
).array;
for (let i = 0; i < boxIds.length; i++) {
const left = boxIds[i];
for (let j = i + 1; j < boxIds.length; j++) {
const right = boxIds[j];
for (let k = 0; k < left.length; k++) {
if (left[k] === right[k]) {
return left[k];
}
}
}
}
throw new Error('no solution');
},
};