jsadvent/solutions/2018/1.js

25 lines
576 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)
.toInts()
.sum(),
2: (input) =>
Glib.fromLines(input)
.toInts()
.infinite()
.partialReduce(
({ seen, position }, move, index) => {
position = position + move;
if (seen.has(position)) {
return position;
}
seen.add(position);
return { seen, position };
},
(state) => typeof state !== 'bigint',
{ seen: new Set([0n]), position: 0n },
),
};