You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.2 KiB
49 lines
1.2 KiB
use std::fs;
|
|
|
|
pub fn step1() -> i32 {
|
|
let data = collect_data();
|
|
let elfdata = organise_data(data);
|
|
let highest = return_highest(elfdata);
|
|
return highest;
|
|
}
|
|
|
|
pub fn step2() -> i32 {
|
|
let data = collect_data();
|
|
let mut elfdata = organise_data(data);
|
|
elfdata.sort_by(|a, b| b.cmp(a));
|
|
let total = add_together(vec![elfdata[0], elfdata[1], elfdata[2]]);
|
|
return total;
|
|
}
|
|
|
|
fn add_together(data: Vec<i32>) -> i32 {
|
|
return data.iter().sum();
|
|
}
|
|
|
|
fn collect_data() -> Vec<String> {
|
|
let data = fs::read_to_string("src/day1/input.txt").expect("Unable to read file");
|
|
let eachline: Vec<String> = data.split("\n").map(str::to_string).collect();
|
|
return eachline;
|
|
}
|
|
|
|
fn organise_data(data: Vec<String>) -> Vec<i32> {
|
|
let mut elfscores: Vec<i32> = Vec::new();
|
|
|
|
let mut amount: i32 = 0;
|
|
for line in data {
|
|
if line.len() < 1 {
|
|
if amount > 0 {
|
|
elfscores.push(amount);
|
|
amount = 0;
|
|
}
|
|
continue;
|
|
}
|
|
amount += line.parse::<i32>().unwrap();
|
|
}
|
|
|
|
return elfscores;
|
|
}
|
|
|
|
fn return_highest(data: Vec<i32>) -> i32 {
|
|
return *data.iter().max().unwrap();
|
|
}
|