You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

28 lines
710 B

import { readFileSync } from 'node:fs';
const input = readFileSync('input', 'utf-8');
const elfCalories = input
.split(/\n{2,}/)
.filter(calories => calories)
.map(calories =>
calories
.split('\n')
.map(count => parseInt(count))
);
const totalElfCalories = elfCalories
.map(calories =>
calories.reduce((total, count) => total + count, 0)
);
totalElfCalories.sort((a, b) => a - b);
const mostCalories = totalElfCalories[totalElfCalories.length - 1];
console.log(`Elf with most calories has: ${mostCalories}`);
const topThreeCalories = totalElfCalories
.slice(-3)
.reduce((total, count) => total + count, 0);
console.log(`Top three calory count: ${topThreeCalories}`);