summaryrefslogtreecommitdiff
path: root/2016/aoc15/src/main.rs
blob: a7f0f64c3db66e831aac7e3374a9bd0dc7233780 (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
33
34
35
extern crate regex;

use regex::Regex;
use std::io::BufReader;
use std::io::prelude::*;
use std::fs::File;


fn main() {
    let gears = read_file();
    let pass = (0..).find(|&i| can_pass(&gears, i)).expect("Reached end of infinite range without finding output");
    println!("First pass at t={}", pass);
}

fn can_pass(gears: &Vec<(i32, i32, i32)>, time: i32) -> bool {
    gears.iter().all(|&(time_offset, init_pos, gear_size)| (init_pos + time_offset + time) % gear_size == 0)
}

fn read_file() -> Vec<(i32, i32, i32)> {
    let file = BufReader::new(File::open("input.txt").unwrap());
    let line_regex = Regex::new(r"Disc #(\d+) has (\d+) positions; at time=0, it is at position (\d+).").unwrap();
    file.lines()
        .filter_map(|line| {
            line_regex.captures(line.unwrap().as_ref()).and_then(|cap| {
                let time_offset = cap.at(1).and_then(|s| s.parse::<i32>().ok());
                let init_pos = cap.at(3).and_then(|s| s.parse::<i32>().ok());
                let gear_size = cap.at(2).and_then(|s| s.parse::<i32>().ok());
                match (time_offset, init_pos, gear_size) {
                    (Some(a), Some(b), Some(c)) => Some((a,b,c)),
                    _ => None
                }
            })
        })
        .collect()
}