47 lines
1,000 B
JavaScript
47 lines
1,000 B
JavaScript
const { Glib, BNMath: Math } = require('../../lib');
|
|
|
|
function* chunk(input, size) {
|
|
let cc = [];
|
|
for (const row of input) {
|
|
cc.push(row);
|
|
if (cc.length === 3) {
|
|
yield cc;
|
|
cc = [];
|
|
}
|
|
}
|
|
if (cc.length !== 0) {
|
|
throw new Error('leftovers!');
|
|
}
|
|
}
|
|
|
|
const validTriangle = (sides) => {
|
|
const hypotenuse = Math.max(...sides);
|
|
const others = sides.slice();
|
|
others.splice(sides.indexOf(hypotenuse), 1);
|
|
return others[0] + others[1] > hypotenuse;
|
|
};
|
|
|
|
const parse = (input) =>
|
|
Glib.fromLines(input).map(
|
|
(triangle) =>
|
|
triangle
|
|
.trim()
|
|
.split(/\s+/)
|
|
.glib.toInts().array,
|
|
);
|
|
|
|
module.exports = {
|
|
'1': (input) => parse(input).filter(validTriangle).length,
|
|
'2': (input) =>
|
|
parse(input)
|
|
.chunk(3)
|
|
.flatMap(([a, b, c]) => {
|
|
const output = [];
|
|
for (let i = 0; i < 3; i++) {
|
|
output.push([a[i], b[i], c[i]]);
|
|
}
|
|
return output;
|
|
})
|
|
.filter(validTriangle).length,
|
|
};
|