blob: 3db59eab3cdedc3f2dc6b4ac174c49d94acd6df7 (
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
36
|
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)).unwrap();
println!("First pass at t={}", pass);
}
fn can_pass(gears: &Vec<(i32, i32)>, time: i32) -> bool {
for i in 0..gears.len() {
let (gear_pos, gear_size) = gears[i];
if (gear_pos + time + i as i32 + 1) % gear_size != 0 {
return false;
}
}
true
}
fn read_file() -> Vec<(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()
.map(|line| line.unwrap().trim().to_string())
.filter(|line| line.len() > 0)
.map(|line| {
let cap = line_regex.captures(line.as_ref()).unwrap();
(cap.at(2).unwrap().parse().unwrap(), cap.at(1).unwrap().parse().unwrap())
})
.collect()
}
|