jsadvent/solutions/2017/2.js

39 lines
830 B
JavaScript
Raw Normal View History

2020-12-10 08:33:00 +00:00
const { Glib } = require('../../lib');
module.exports = {
'1': (input) =>
Glib.fromLines(input)
.map((row) =>
row
.trim()
.split(/\s+/)
.glib.toInts()
.minMax(),
)
.map(([min, max]) => max - min)
.sum(),
'2': (input) =>
Glib.fromLines(input.trim())
.map(
(row) =>
row
.trim()
.split(/\s+/)
.glib.toInts().array,
)
.map((row) => {
for (const dividend of row) {
for (const divisor of row) {
if (dividend === divisor) {
continue;
}
if (dividend % divisor === 0n) {
return dividend / divisor;
}
}
}
throw new Error('no solution');
})
.sum(),
};