wow this thing have more than one part
This commit is contained in:
parent
4538a035c6
commit
2c055a1094
|
@ -6,10 +6,15 @@ fn main() {
|
||||||
println!("Hi this is the fist day of AOC2022, first we will read the file {}", FILE_PATH);
|
println!("Hi this is the fist day of AOC2022, first we will read the file {}", FILE_PATH);
|
||||||
let contents = fs::read_to_string(FILE_PATH)
|
let contents = fs::read_to_string(FILE_PATH)
|
||||||
.expect("Should have been able to read the file");
|
.expect("Should have been able to read the file");
|
||||||
println!("The elf with the biggest calories is: {}", get_max_elf(&contents));
|
|
||||||
|
println!("And we get the elfs from the file!");
|
||||||
|
let mut elfs = get_elfs(&contents);
|
||||||
|
println!("The elf with the biggest calories is carring: {}", get_max_elf(&elfs));
|
||||||
|
println!("The elf with the 3 biggest calories are carring: {}", get_max_3_elf(&mut elfs));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_max_elf(input: &str) -> u32{
|
fn get_elfs(input: &str) -> Vec<u32> {
|
||||||
let binding = input.to_owned();
|
let binding = input.to_owned();
|
||||||
let input: Vec<&str> = binding.lines().collect();
|
let input: Vec<&str> = binding.lines().collect();
|
||||||
let mut elfs = Vec::new();
|
let mut elfs = Vec::new();
|
||||||
|
@ -19,9 +24,22 @@ fn get_max_elf(input: &str) -> u32{
|
||||||
if line == "" {ptr+=1; elfs.push(0); continue;}
|
if line == "" {ptr+=1; elfs.push(0); continue;}
|
||||||
elfs[ptr] += u32::from_str(line).expect("all content must be numbers");
|
elfs[ptr] += u32::from_str(line).expect("all content must be numbers");
|
||||||
}
|
}
|
||||||
|
elfs
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_max_elf(elfs: &Vec<u32>) -> u32{
|
||||||
*elfs.iter().max().expect("there must be a maximum lol")
|
*elfs.iter().max().expect("there must be a maximum lol")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_max_3_elf(elfs: &mut Vec<u32>) -> u32{
|
||||||
|
let mut out: u32 = 0;
|
||||||
|
elfs.sort();
|
||||||
|
for _ in 0..3 {
|
||||||
|
out += elfs.pop().expect("There must be at least 3 elfs");
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -41,7 +59,26 @@ mod test {
|
||||||
9000
|
9000
|
||||||
|
|
||||||
10000"#;
|
10000"#;
|
||||||
|
let tst = get_elfs(input);
|
||||||
assert_eq!(get_max_elf(input), 24000);
|
assert_eq!(get_max_elf(&tst), 24000);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn it_works2() {
|
||||||
|
let input = r#"1000
|
||||||
|
2000
|
||||||
|
3000
|
||||||
|
|
||||||
|
4000
|
||||||
|
|
||||||
|
5000
|
||||||
|
6000
|
||||||
|
|
||||||
|
7000
|
||||||
|
8000
|
||||||
|
9000
|
||||||
|
|
||||||
|
10000"#;
|
||||||
|
let mut tst = get_elfs(input);
|
||||||
|
assert_eq!(get_max_3_elf(&mut tst), 45000);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue