mirror of
https://gitlab.com/MisterBiggs/aoc-2023-rust.git
synced 2025-06-15 14:36:50 +00:00
day 1 complete
This commit is contained in:
parent
971581dd85
commit
a12c497551
95
src/day1.rs
95
src/day1.rs
@ -8,44 +8,79 @@ pub fn run() {
|
||||
println!("\tPart 2: {}", part2(&input));
|
||||
}
|
||||
|
||||
fn part1(food_list: &str) -> isize {
|
||||
todo!()
|
||||
fn part1(food_input: &str) -> usize {
|
||||
food_input
|
||||
.split("\n\n")
|
||||
.collect::<Vec<&str>>()
|
||||
.into_iter()
|
||||
.map(|elf| {
|
||||
elf.split_whitespace()
|
||||
.map(|food| food.parse::<usize>().unwrap())
|
||||
.sum()
|
||||
})
|
||||
.collect::<Vec<usize>>()
|
||||
.into_iter()
|
||||
.max()
|
||||
.unwrap()
|
||||
}
|
||||
fn part2(food_list: &str) -> usize {
|
||||
todo!()
|
||||
fn part2(food_input: &str) -> usize {
|
||||
let mut elves_calories = food_input
|
||||
.split("\n\n")
|
||||
.collect::<Vec<&str>>()
|
||||
.into_iter()
|
||||
.map(|elf| {
|
||||
elf.split_whitespace()
|
||||
.map(|food| food.parse::<usize>().unwrap())
|
||||
.sum()
|
||||
})
|
||||
.collect::<Vec<usize>>();
|
||||
|
||||
elves_calories.sort_by(|l, r| r.cmp(l));
|
||||
|
||||
elves_calories[..3].iter().sum::<usize>()
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
// use super::*;
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// #[test]
|
||||
// fn test_1() {
|
||||
// let mut input = "(())".to_string();
|
||||
// assert_eq!(part1(&input), 0);
|
||||
#[test]
|
||||
fn test_1() {
|
||||
let input = "1000
|
||||
2000
|
||||
3000
|
||||
|
||||
// input = "()()".to_string();
|
||||
// assert_eq!(part1(&input), 0);
|
||||
4000
|
||||
|
||||
// input = "(((".to_string();
|
||||
// assert_eq!(part1(&input), 3);
|
||||
5000
|
||||
6000
|
||||
|
||||
// input = "(()(()(".to_string();
|
||||
// assert_eq!(part1(&input), 3);
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
// input = "())".to_string();
|
||||
// assert_eq!(part1(&input), -1);
|
||||
10000";
|
||||
|
||||
// input = ")())())".to_string();
|
||||
// assert_eq!(part1(&input), -3);
|
||||
// }
|
||||
assert_eq!(part1(input), 24000);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn test_2() {
|
||||
// let mut input = ")".to_string();
|
||||
// assert_eq!(part2(&input), 1);
|
||||
#[test]
|
||||
fn test_2() {
|
||||
let input = "1000
|
||||
2000
|
||||
3000
|
||||
|
||||
// input = "()())".to_string();
|
||||
// assert_eq!(part2(&input), 5);
|
||||
// }
|
||||
// }
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000";
|
||||
|
||||
assert_eq!(part2(input), 45000);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user