summaryrefslogtreecommitdiff
path: root/src/bin/day_1.rs
blob: 46935797f264a601ffad16a46ad746de263cdd83 (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
26
27
28
29
30
31
32
extern crate advent_of_code_2018;
use advent_of_code_2018::*;

extern crate im_rc;

use std::error::Error;
use std::path::PathBuf;

use im_rc::HashSet;

// cargo watch -cs "cargo run --release --bin day_1"

fn main() -> Result<(), Box<Error>> {
    let input = read_file(&PathBuf::from("inputs/1.txt"))?;
    let input_ints: Vec<i32> = input.iter().map(|str| str.parse::<i32>().unwrap()).collect();

    println!("Input: {:?}", input_ints);

    let sum: i32 = input_ints.iter().sum();
    println!("Sum: {}", sum);

    let first_repeat = input_ints.iter().cycle().try_fold((0, HashSet::new()), |(acc, seen), &i| {
        if seen.contains(&acc) {
            Err(acc)
        } else {
            Ok((acc + i, seen.update(acc)))
        }
    }).err().unwrap();
    println!("First repeat: {}", first_repeat);

    Ok(())
}