Last active
June 25, 2023 21:58
-
-
Save Philogy/67060bcfd15a80034ac7cec00629a81f to your computer and use it in GitHub Desktop.
Advent Of Code 2022 (Day 1) in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fs; | |
fn sort_desc<T: Ord>(v: &mut Vec<T>) { | |
v.sort_by(|a, b| b.cmp(a)); | |
} | |
/// # Advent Of Code 2022 (Day 1). | |
/// | |
/// Challenge is you an input which displays "calories of elves" ([link](https://adventofcode.com/2022/day/1)): | |
/// | |
/// ` | |
/// 4525 \ | |
/// 4368 / elf 1 | |
/// | |
/// 17242 > elf 2 | |
/// | |
/// 10920 \ | |
/// 14072 / elf 3 | |
/// ` | |
/// | |
/// ## Part 1 | |
/// Find the elf with the most total calories. | |
/// | |
/// ## Part 2 | |
/// Compute the total of the calories of the top 3 elves. | |
/// | |
fn main() { | |
let input = fs::read_to_string("./src/input.txt").unwrap(); | |
let groups = input | |
.split("\n\n") | |
.map(|group| group.lines().map(|l| l.parse::<u64>().unwrap())); | |
let mut cals: Vec<u64> = groups.map(|group| group.sum()).collect(); | |
sort_desc(&mut cals); | |
let most_cals = cals[0]; | |
println!("Most Calories: {most_cals:?}"); | |
let top_total = cals.iter().take(3).sum::<u64>(); | |
println!("Total Calories (Top 3): {top_total:?}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment