summaryrefslogtreecommitdiff
path: root/2018/src/day1.rs
blob: 741f5b31364414b4a3a591774a29e1eacd6dd426 (plain)
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
use std::io;
use std::io::BufRead;
use std::collections::HashSet;

pub fn main<T: BufRead>(reader: T) -> io::Result<()> {
    let values: Vec<i64> =
        reader.lines().map(|l| l.unwrap().parse::<i64>().unwrap()).collect();

    println!("{}", values.iter().sum::<i64>());

    let mut seen = HashSet::new();
    seen.insert(0);

    let mut res = 0;
    for val in values.iter().cycle() {
        res += val;
        if seen.contains(&res) {
            println!("{}", res);
            return Ok(());
        } else {
            seen.insert(res);
        }
    }
    unreachable!()
}