31 lines
852 B
Rust
31 lines
852 B
Rust
use crate::utils;
|
|
use std::collections::HashSet;
|
|
|
|
pub fn task1() {
|
|
let frequency: i32 = utils::read_file("input/day01.txt")
|
|
.lines()
|
|
.map(|line| line.parse::<i32>().unwrap())
|
|
.sum();
|
|
|
|
println!("Part 1: {}", frequency);
|
|
}
|
|
|
|
pub fn task2() {
|
|
let start_state = (vec![0].into_iter().collect::<HashSet<i32>>(), 0);
|
|
let final_state = utils::read_file("input/day01.txt")
|
|
.lines()
|
|
.map(|line| line.parse::<i32>().unwrap())
|
|
.cycle()
|
|
.scan(start_state, |(set, current), freq_change| {
|
|
*current += freq_change;
|
|
if set.insert(*current) {
|
|
Some((*current, false))
|
|
} else {
|
|
Some((*current, true))
|
|
}
|
|
})
|
|
.find(|state| state.1);
|
|
|
|
println!("Part 2: {} was there already!", final_state.unwrap().0);
|
|
}
|