aoc2022 day 01

This commit is contained in:
Max Hohlfeld 2022-12-01 18:29:21 +01:00
parent 61b91dca9b
commit cebb68f4c7
4 changed files with 2292 additions and 0 deletions

2254
src/aoc2022/day01/input.txt Normal file

File diff suppressed because it is too large Load Diff

33
src/aoc2022/day01/mod.rs Normal file
View File

@ -0,0 +1,33 @@
fn get_calories(input: &str) -> Vec<usize> {
let mut calories: Vec<usize> = Vec::new();
let mut sum = 0_usize;
for line in input.lines() {
match line.parse::<usize>() {
Ok(calorie) => sum += calorie,
Err(_) => {
calories.push(sum);
sum = 0;
}
}
}
calories.sort();
calories
}
pub fn task_one(input: &str) -> String {
let calories = get_calories(input);
calories.last().unwrap().to_string()
}
pub fn task_two(input: &str) -> String {
let calories = get_calories(input);
let count = calories.len();
let slice = calories.get(count - 3 ..).unwrap();
slice.into_iter().sum::<usize>().to_string()
}

1
src/aoc2022/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod day01;

View File

@ -1,4 +1,5 @@
mod aoc2021; mod aoc2021;
mod aoc2022;
use std::fs; use std::fs;
@ -23,4 +24,7 @@ fn main() {
let result_day04_task1 = aoc2021::day04::task_one(&input_day04); let result_day04_task1 = aoc2021::day04::task_one(&input_day04);
let result_day04_task2 = aoc2021::day04::task_two(&input_day04); let result_day04_task2 = aoc2021::day04::task_two(&input_day04);
println!("AOC2021 Day 04 \t Task 1: {},\t\tTask 2: {}", result_day04_task1, result_day04_task2); println!("AOC2021 Day 04 \t Task 1: {},\t\tTask 2: {}", result_day04_task1, result_day04_task2);
let input_d1 = fs::read_to_string("src/aoc2022/day01/input.txt").expect("Error on reading file.");
println!("AOC2022 Day 01 \t Task 1: {},\t\tTask 2: {}", aoc2022::day01::task_one(&input_d1), aoc2022::day01::task_two(&input_d1));
} }