55 lines
1.4 KiB
Rust
55 lines
1.4 KiB
Rust
use std::fs;
|
|
|
|
fn main() {
|
|
// part_1();
|
|
part_2();
|
|
}
|
|
|
|
fn part_1() {
|
|
let contents = fs::read_to_string("./input").expect("Couldn't read file!");
|
|
let numbers: Vec<usize> = contents
|
|
.lines()
|
|
.map(|s| s.parse::<usize>().unwrap())
|
|
.collect();
|
|
|
|
let initial_condition: (usize, usize) = (0, 1000000);
|
|
|
|
let increases = numbers.iter().fold(initial_condition, |acc, num| {
|
|
if num > &acc.1 {
|
|
(acc.0 + 1, *num)
|
|
} else {
|
|
(acc.0, *num)
|
|
}
|
|
});
|
|
|
|
println!("{}", increases.0);
|
|
}
|
|
|
|
fn part_2() {
|
|
let contents = fs::read_to_string("./input").expect("Couldn't read file!");
|
|
let numbers: Vec<usize> = contents
|
|
.lines()
|
|
.map(|s| s.parse::<usize>().unwrap())
|
|
.collect();
|
|
|
|
// inc n - 3 n - 2 n - 1
|
|
let initial_condition: (usize, (usize, usize, usize)) = (0, (0, 0, 0));
|
|
|
|
let increases = numbers.iter().fold(initial_condition, |acc, num| {
|
|
let new_previous_numbers = (acc.1 .1, acc.1 .2, *num);
|
|
if acc.1 .0 == 0 {
|
|
return (0, new_previous_numbers);
|
|
}
|
|
|
|
let previous_sum = acc.1 .0 + acc.1 .1 + acc.1 .2;
|
|
let current_sum = acc.1 .1 + acc.1 .2 + num;
|
|
if current_sum > previous_sum {
|
|
(acc.0 + 1, new_previous_numbers)
|
|
} else {
|
|
(acc.0, new_previous_numbers)
|
|
}
|
|
});
|
|
|
|
println!("{}", increases.0);
|
|
}
|