jsadvent/solutions/2018/3.js

34 lines
1,016 B
JavaScript
Raw Normal View History

2020-12-10 08:33:00 +00:00
const { Glib, Sets, Vector, twodee } = require('../../lib');
const parse = (input) =>
Glib.fromLines(input).map((line) => {
const [id, at, offset, size] = line.split(' ');
const offsetPoint = Vector.fromString(`(${offset.slice(0, -1)})`);
const sizeVector = Vector.fromString(`(${size.replace('x', ',')})`);
const rectangle = new twodee.Rectangle(offsetPoint, sizeVector);
return {
id: id.slice(1),
rectangle,
};
});
module.exports = {
'1': (input) => {
return parse(input)
.flatMap(({ rectangle }) => rectangle.allPoints().map((pt) => pt.string))
.frequency()
.glibValues.filter((v) => v >= 2n).length;
},
'2': (input) => {
const claimed = parse(input)
.flatMap(({ rectangle }) => rectangle.allPoints().map((pt) => pt.string))
.frequency();
return parse(input)
.filter(({ rectangle }) =>
rectangle.allPoints().every((pt) => claimed.get(pt.string) === 1n),
)
.map((i) => i.id)
.join();
},
};