Advent-of-Code/2022/1/main.rs

39 lines
588 B
Rust
Raw Permalink Normal View History

2022-12-01 09:14:56 -05:00
use std::fs;
fn main()
{
let contents = fs::read_to_string("data.txt")
.expect("Could not find data.txt.");
let mut numbers = Vec::new();
let mut total = 0;
let mut max = 0;
for line in contents.split('\n')
{
if line == ""
{
if total > max
{
max = total;
}
numbers.push(total);
total = 0;
}
else
{
let i = line.parse::<i32>().unwrap();
total += i;
}
}
println!("Max: {max}");
numbers.sort();
let mut top_three = 0;
for amt in &numbers[numbers.len() - 3..numbers.len()]
{
top_three += amt;
}
println!("Top Three: {top_three}");
}