summaryrefslogtreecommitdiff
path: root/2018/src/day1.rs
blob: b86387b0bf9289c367df68de0d423ae8fdab925d (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<(String, String)> {
    let values: Vec<i64> =
        reader.lines().map(|l| l.unwrap().parse::<i64>().unwrap()).collect();

    let part1 = 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) {
            return Ok((part1.to_string(), res.to_string()));
        } else {
            seen.insert(res);
        }
    }

    Err(io::Error::new(io::ErrorKind::Other, "Invalid input"))
}