01020304050607080910111213141516171819202122232425

Advent of Code

2022/1

Calorie Counting

in C#

by encse

Santa's reindeer typically eat regular reindeer food, but they need a lot of magical energy to deliver presents on Christmas. For that, their favorite snack is a special type of star fruit that only grows deep in the jungle. The Elves have brought you on their annual expedition to the grove where the fruit grows.

To supply enough magical energy, the expedition needs to retrieve a minimum of fifty stars by December 25th. Although the Elves assure you that the grove has plenty of fruit, you decide to grab any fruit you see along the way, just in case.

Visit the website for the full story and full puzzle description.

using System.Collections.Generic;
using System.Linq;

namespace AdventOfCode.Y2022.Day01;

[ProblemName("Calorie Counting")]      
class Solution : Solver {

    public object PartOne(string input) => 
        GetCaloriesPerElf(input).First();

    public object PartTwo(string input) =>
        GetCaloriesPerElf(input).Take(3).Sum();

    // Returns the calories carried by the elves in descending order.
    private IEnumerable<int> GetCaloriesPerElf(string input) =>
        from elf in input.Split("\n\n")
        let calories = elf.Split('\n').Select(int.Parse).Sum()
        orderby calories descending
        select calories;
}

Please ☆ my repo if you like it!

© 2025 Advent of Code is a registered trademark in the US Images provided by Bing image creator