Created
December 1, 2022 17:10
-
-
Save quiiver/cf622eb3e1733631776961f8966a0916 to your computer and use it in GitHub Desktop.
This file contains hidden or 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::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