Sblog

Advent of Code 2022 - Day 1

Date: 2022-12-01

Advent of code 2022 has started.

The first day's task is certainly easy, as expected. You can find the exercise description here: https://adventofcode.com/2022/day/1.

I'm using advent of code as an exercise to get more familiar with Rust. I am going to be using .unwrap() here as I don't feel like proper error handling is needed in the context of these exercises.

I feel like this solution is simple enough that understanding the code should be easy enough without any extra explanations.

use std::{fs::read_to_string, path::Path};

fn main() {
    let path = Path::new("./data/input.txt");
    let input = read_to_string(&path).unwrap();
    let mut calories = input
        .split("\n\n")
        .map(|group| group.lines().map(|line| line.parse::<u32>().unwrap()).sum())
        .collect::<Vec<u32>>();

    // descending sort
    calories.sort_by(|a, b| b.cmp(a));

    // part 1
    println!("{:#?}", calories[0]);
    // part 2
    println!("{:#?}", calories[0] + calories[1] + calories[2]);
}