2018-12-04 18:36:08 +01:00
#[ macro_use ] extern crate text_io ;
2018-12-04 20:36:22 +01:00
use std ::fs ;
mod types ;
use types ::* ;
2018-12-04 18:36:08 +01:00
fn parse_action ( input : & String ) -> GuardAction {
2018-12-04 20:36:22 +01:00
match input . chars ( ) . next ( ) . unwrap ( ) {
'f' = > GuardAction ::FallAsleep ,
'w' = > GuardAction ::WakeUp ,
'G' = > {
2018-12-04 19:33:07 +01:00
let gid : i32 ;
scan! ( input . bytes ( ) = > " Guard #{} begins shift " , gid ) ;
GuardAction ::BeginShift ( gid )
}
2018-12-04 20:36:22 +01:00
_ = > std ::panic! ( )
2018-12-04 19:33:07 +01:00
}
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 ) ;
2018-12-04 20:36:22 +01:00
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 ( ) ) ) ;
}
}
2018-12-04 20:36:22 +01:00