Advent-of-Code-2022/1/solution.mjs
2022-12-02 12:23:21 -07:00

29 lines
710 B
JavaScript

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}`);