(rust) day 4: split into multiple files

This commit is contained in:
kageru 2018-12-04 20:36:22 +01:00
parent 8bac0e4632
commit c1464f9f33
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
2 changed files with 84 additions and 78 deletions

View File

@ -1,95 +1,26 @@
#[macro_use] extern crate text_io;
use std::{fs, cmp};
use std::fs;
mod types;
use types::*;
enum GuardAction {
BeginShift(i32),
FallAsleep,
WakeUp,
}
struct Guard {
events: Vec<Event>,
id: i32,
}
#[derive(Eq)]
struct DateTime {
month: i32,
day: i32,
hour: i32,
minute: i32,
sortable_time: i32,
}
// Calculate the absolute minute count relative to 01/01 0:00. Months are assumed to have 31
// days because we just want to ensure that a higher month always results in a higher
// timestamp. Think of this as a primitive and less correct unix epoch.
pub fn get_sortable_time(month: i32, day: i32, hour: i32, minute: i32) -> i32 {
return minute + hour * 60 + day * 60 * 24 + month * 60 * 24 * 31;
}
impl DateTime {
pub fn new(month: i32, day: i32, hour: i32, minute: i32) -> Self {
DateTime {
month,
day,
hour,
minute,
sortable_time: get_sortable_time(month, day, hour, minute)
}
}
}
impl Ord for DateTime {
fn cmp(&self, other: &DateTime) -> cmp::Ordering {
self.sortable_time.cmp(&other.sortable_time)
}
}
impl PartialOrd for DateTime {
fn partial_cmp(&self, other: &DateTime) -> Option<cmp::Ordering> {
Some(other.cmp(self))
}
}
impl PartialEq for DateTime {
fn eq(&self, other: &DateTime) -> bool {
self.sortable_time == other.sortable_time
}
}
struct Event {
datetime: DateTime,
action: GuardAction,
}
impl Event {
pub fn new(datetime: DateTime, action: GuardAction) -> Self {
Event {
datetime,
action
}
}
}
fn parse_action(input: &String) -> GuardAction {
match input[0] {
"f" => GuardAction::FallAsleep,
"w" => GuardAction::WakeUp,
"G" => {
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)
}
_ => // undefined?
_ => std::panic!()
}
}
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 = DateTime::new(month, day, hour, minute);
let datetime = types::DateTime::new(month, day, hour, minute);
return Event::new(datetime, parse_action(&unparsed_action));
}
@ -101,3 +32,4 @@ fn main() {
events.push(event_from_line(&line.to_string()));
}
}

74
04/src/types.rs Normal file
View File

@ -0,0 +1,74 @@
use std::cmp;
pub struct Event {
datetime: DateTime,
action: GuardAction,
}
pub enum GuardAction {
BeginShift(i32),
FallAsleep,
WakeUp,
}
pub struct Guard {
events: Vec<Event>,
id: i32,
}
#[derive(Eq)]
pub struct DateTime {
month: i32,
day: i32,
hour: i32,
minute: i32,
sortable_time: i32,
}
// Calculate the absolute minute count relative to 01/01 0:00. Months are assumed to have 31
// days because we just want to ensure that a higher month always results in a higher
// timestamp. Think of this as a primitive and less correct unix epoch.
pub fn get_sortable_time(month: i32, day: i32, hour: i32, minute: i32) -> i32 {
return minute + hour * 60 + day * 60 * 24 + month * 60 * 24 * 31;
}
impl DateTime {
pub fn new(month: i32, day: i32, hour: i32, minute: i32) -> Self {
DateTime {
month,
day,
hour,
minute,
sortable_time: get_sortable_time(month, day, hour, minute)
}
}
}
impl Ord for DateTime {
fn cmp(&self, other: &DateTime) -> cmp::Ordering {
self.sortable_time.cmp(&other.sortable_time)
}
}
impl PartialOrd for DateTime {
fn partial_cmp(&self, other: &DateTime) -> Option<cmp::Ordering> {
Some(other.cmp(self))
}
}
impl PartialEq for DateTime {
fn eq(&self, other: &DateTime) -> bool {
self.sortable_time == other.sortable_time
}
}
impl Event {
pub fn new(datetime: DateTime, action: GuardAction) -> Self {
Event {
datetime,
action
}
}
}