day01-2 use scan with tuple expansion
The previous problem with this was, that I tried returning "Some((Ref<i32>, _))" from the lambda. That caused the compiler to complain about the lifetime of the `current` value, since I tried passing on the Ref instead of the encapsulated value.
This commit is contained in:
@@ -7,26 +7,23 @@ pub fn task1() {
|
|||||||
.map(|line| line.parse::<i32>().unwrap())
|
.map(|line| line.parse::<i32>().unwrap())
|
||||||
.sum();
|
.sum();
|
||||||
|
|
||||||
println!("Result: {}", frequency);
|
println!("Part 1: {}", frequency);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn task2() {
|
pub fn task2() {
|
||||||
let contents = utils::read_file("input/day01.txt");
|
let start_state = (vec![0].into_iter().collect::<HashSet<i32>>(), 0);
|
||||||
|
let final_state = utils::read_file("input/day01.txt")
|
||||||
let seen: HashSet<i32> = vec![0].into_iter().collect();
|
|
||||||
let start_state = (seen, 0);
|
|
||||||
let final_state = contents
|
|
||||||
.lines()
|
.lines()
|
||||||
.map(|line| line.parse::<i32>().unwrap())
|
.map(|line| line.parse::<i32>().unwrap())
|
||||||
.cycle()
|
.cycle()
|
||||||
.scan(start_state, |state, freq_change| {
|
.scan(start_state, |(set, current), freq_change| {
|
||||||
state.1 += freq_change;
|
*current += freq_change;
|
||||||
if state.0.insert(state.1) {
|
if set.insert(*current) {
|
||||||
Some((state.1, false))
|
Some((*current, false))
|
||||||
} else {
|
} else {
|
||||||
Some((state.1, true))
|
Some((*current, true))
|
||||||
}
|
}
|
||||||
}).find(|state| state.1);
|
}).find(|state| state.1);
|
||||||
|
|
||||||
println!("{:?} was there already!", final_state.unwrap().0);
|
println!("Part 2: {} was there already!", final_state.unwrap().0);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user