Skip to content

Instantly share code, notes, and snippets.

@dbarasti
Last active December 2, 2022 09:40
Show Gist options
  • Save dbarasti/6724261851732eb6041fefd381040111 to your computer and use it in GitHub Desktop.
Save dbarasti/6724261851732eb6041fefd381040111 to your computer and use it in GitHub Desktop.
First excercise of the AoC in functional Rust
use itertools::Itertools;
use std::{
fs::File,
io::{BufRead, BufReader},
};
fn main() -> std::io::Result<()> {
let f = File::open("test.txt")?;
let reader = BufReader::new(f);
let max = reader
.lines()
.map(|line| line.unwrap().parse::<i32>().unwrap_or(0))
.group_by(|calories| *calories> 0)
.into_iter()
.map(|(_, backpack)| backpack.sum::<i32>())
.max();
if let Some(res) = max {
println!("{}", res);
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment