Skip to content

Instantly share code, notes, and snippets.

@quiiver
Created December 1, 2022 17:10
Show Gist options
  • Select an option

  • Save quiiver/cf622eb3e1733631776961f8966a0916 to your computer and use it in GitHub Desktop.

Select an option

Save quiiver/cf622eb3e1733631776961f8966a0916 to your computer and use it in GitHub Desktop.
use std::env;
use std::fs;
pub fn read_file(day: u8) -> String {
let cwd = env::current_dir().unwrap();
let filepath = cwd
.join("inputs")
.join(format!("day{:02}.txt", day));
let f = fs::read_to_string(filepath);
f.expect("could not open input file")
}
fn main() {
let mut sum_calories: Vec<u32> = read_file(1)
.split("\n\n")
.map(|x| x.split('\n')
.filter(|x| !x.is_empty())
.map(|n| n.parse::<u32>().unwrap())
.sum()
)
.collect::<Vec<u32>>();
sum_calories.sort();
println!(
"part 1: {:?}",
sum_calories.last().unwrap()
,
);
println!(
"part 2: {:?}",
sum_calories.iter().rev().take(3).sum::<u32>()
,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment