aboutsummaryrefslogtreecommitdiff
path: root/day01.rs
blob: a864d5d23889c6abad022a296a6eee8ee0c8c07f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use std::fs::File;
use std::collections::HashSet;
use std::io::{BufRead,BufReader};

fn main() {
    let f = File::open("day01").unwrap();

    let v: Vec<i32> = BufReader::new(&f).lines()
        .map(|l| l.unwrap().parse().unwrap())
        .collect();
    println!("{}", v.iter().sum::<i32>());  // 553

    let mut seen = HashSet::new();
    let s = v.iter()
        .cycle()
        .scan(0, |a,e| {
            *a += e;
            Some(*a)
        })
        .find(|n| !seen.insert(*n));
    println!("{}", s.unwrap_or(-1));  // 78724
}