-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
lib.rs
32 lines (27 loc) · 778 Bytes
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use std::collections::HashSet;
pub struct PartOne;
pub struct PartTwo;
impl aoclib::Solvable<&str, i32> for PartOne {
fn solve(input: &str) -> aoclib::Solution<i32> {
Ok(input.lines().map(|s| s.parse::<i32>().unwrap()).sum())
}
}
impl aoclib::Solvable<&str, i32> for PartTwo {
fn solve(input: &str) -> aoclib::Solution<i32> {
let history: &mut HashSet<i32> = &mut HashSet::new();
let result: &mut Option<i32> = &mut None;
let mut sum: i32 = 0;
while result.is_none() {
for n in input.lines().map(|s| s.parse::<i32>().unwrap()) {
let prev_length = history.len();
sum += n;
history.insert(sum);
if prev_length == history.len() {
result.replace(sum);
break;
}
}
}
result.ok_or(aoclib::SolutionError::new("Not found"))
}
}