Day 01 - Calorie Counting

parsingsorting

Language: Rust

Problem https://adventofcode.com/2022/day/1


Part 1: Each Elf’s food items are listed as calorie values separated by newlines, with a blank line between Elves. Find the highest total across all Elves.

I split on double newlines to get each Elf’s block, sum the numbers in each, and take the max:

let sums: Vec<i32> = input.split("\n\n")
    .map(|group| {
        group.lines()
            .filter_map(|line| line.parse::<i32>().ok())
            .sum()
    })
    .collect();

let max = sums.iter().max().unwrap();

Part 2: Same setup, but now find the top three Elves and sum their totals. I sort the sums descending and take the first three:

sums.sort_by(|a, b| b.cmp(a));
let top3_sum = sums.iter().take(3).sum::<i32>();

Solution: https://github.com/Elyrial/AdventOfCode/blob/main/src/solutions/year2022/day01.rs

No C writeup yet.