AdventOfCode/y2022/src/days/d1.rs

50 lines
897 B
Rust

pub fn process_part1(input: &str) -> i32 {
let mut vector: Vec<i32> = input
.split("\n\n")
.map(|lines| lines.lines().map(|line| line.parse::<i32>().unwrap()).sum())
.collect();
vector.sort_by(|a, b| b.cmp(a));
vector[0]
}
pub fn process_part2(input: &str) -> i32 {
let mut vector: Vec<i32> = input
.split("\n\n")
.map(|lines| lines.lines().map(|line| line.parse::<i32>().unwrap()).sum())
.collect();
vector.sort_by(|a, b| b.cmp(a));
vector.iter().take(3).sum()
}
#[cfg(test)]
mod tests {
use super::*;
const INPUT: &str = "1000
2000
3000
4000
5000
6000
7000
8000
9000
10000";
#[test]
fn it_works() {
let result = process_part1(INPUT);
assert_eq!(result, 24000);
}
#[test]
fn part2() {
let result = process_part2(INPUT);
assert_eq!(result, 45000);
}
}