github.com/mnlphlp/aoc22@v0.0.0-20230330151331-c1dc4bff1b9b/aoc22_rust/src/day01.rs (about)

     1  use crate::types::Task;
     2  
     3  fn sum_calories(elves: &Vec<&str>) -> Vec<i32> {
     4      elves
     5          .iter()
     6          .map(|elf| {
     7              elf.lines()
     8                  .fold(0, |sum, line| sum + line.parse::<i32>().unwrap())
     9          })
    10          .collect()
    11  }
    12  
    13  pub fn solve(input: &str, _test: bool, task: Task) -> (String, String) {
    14      let mut res1 = "".to_string();
    15      let mut res2 = "".to_string();
    16  
    17      let elves = input.split("\n\n").collect();
    18      let mut calories = sum_calories(&elves);
    19      calories.sort();
    20  
    21      if !matches!(task, Task::Two) {
    22          let max_calories = calories.last().unwrap();
    23          res1 = max_calories.to_string();
    24      }
    25      if !matches!(task, Task::One) {
    26          let top_calories = calories.iter().rev().take(3).sum::<i32>();
    27          res2 = top_calories.to_string();
    28      }
    29  
    30      (res1, res2)
    31  }