advent-of-code/04/src/main.rs

36 lines
1.1 KiB
Rust
Raw Normal View History

2018-12-04 18:36:08 +01:00
#[macro_use] extern crate text_io;
use std::fs;
mod types;
use types::*;
2018-12-04 18:36:08 +01:00
fn parse_action(input: &String) -> GuardAction {
match input.chars().next().unwrap() {
'f' => GuardAction::FallAsleep,
'w' => GuardAction::WakeUp,
'G' => {
let gid: i32;
scan!(input.bytes() => "Guard #{} begins shift", gid);
GuardAction::BeginShift(gid)
}
_ => std::panic!()
}
2018-12-04 18:36:08 +01:00
}
fn event_from_line(line: &String) -> Event {
let (month, day, hour, minute, unparsed_action): (i32, i32, i32, i32, String);
scan!(line.bytes() => "[1518-{}-{} {}:{}] {}", month, day, hour, minute, unparsed_action);
let datetime = types::DateTime::new(month, day, hour, minute);
2018-12-04 18:36:08 +01:00
return Event::new(datetime, parse_action(&unparsed_action));
}
fn main() {
let file = fs::read_to_string("../input").expect("can’t access file");
let lines: Vec<&str> = file.split("\n").collect();
let mut events: Vec<Event> = Vec::new();
for line in lines {
events.push(event_from_line(&line.to_string()));
}
}