34 lines
752 B
Rust
34 lines
752 B
Rust
static INPUT: &str = include_str!("input.txt");
|
|
|
|
fn solution(x: i32) -> i32 {
|
|
let mut totals = vec![Default::default(); x.try_into().unwrap()];
|
|
let mut total = 0;
|
|
for line in INPUT.lines() {
|
|
if line.is_empty() {
|
|
add_to_total(&mut totals, total);
|
|
total = 0;
|
|
} else {
|
|
let n: i32 = line.trim().parse().expect("invalid input");
|
|
total += n;
|
|
};
|
|
}
|
|
return totals.iter().sum();
|
|
}
|
|
|
|
fn add_to_total(totals: &mut [i32], total: i32) {
|
|
let mut i = total;
|
|
for n in totals.iter_mut() {
|
|
if i > *n {
|
|
std::mem::swap(&mut (*n), &mut i);
|
|
};
|
|
}
|
|
}
|
|
|
|
pub fn solution_a() -> i32 {
|
|
solution(1)
|
|
}
|
|
|
|
pub fn solution_b() -> i32 {
|
|
solution(3)
|
|
}
|