diff options
author | Justin Worthe <justin.worthe@entelect.co.za> | 2016-12-15 12:08:01 +0200 |
---|---|---|
committer | Justin Worthe <justin.worthe@entelect.co.za> | 2016-12-15 12:08:01 +0200 |
commit | 6c4f0f57cb50537f21dd121bc0089b4b4fd6a7c4 (patch) | |
tree | dfe779773a8fcef07eb5c57ac29bab456b324fa9 /aoc15/src/main.rs | |
parent | 30898c25fcd0d12a4f361e8b237c8dfb4b8172ea (diff) |
AOC15
Diffstat (limited to 'aoc15/src/main.rs')
-rw-r--r-- | aoc15/src/main.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/aoc15/src/main.rs b/aoc15/src/main.rs new file mode 100644 index 0000000..3db59ea --- /dev/null +++ b/aoc15/src/main.rs @@ -0,0 +1,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() +} |