(rust) day 4, preparation for big input

This commit is contained in:
kageru 2018-12-05 11:27:59 +01:00
parent 1aa9be71c2
commit 631798b7c2
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
2 changed files with 11 additions and 9 deletions

View File

@ -18,12 +18,12 @@ fn parse_action(input: &String) -> GuardAction {
}
fn event_from_line(line: String) -> Event {
let (month, day, hour, minute, unparsed_action): (i32, i32, i32, i32, String);
let (year, month, day, hour, minute, unparsed_action): (i32, i32, i32, i32, i32, String);
// I’m only adding the \n here to use it as a marker for scan!,
// which would otherwise stop at the first space.
let line2 = line.to_string() + "\n";
scan!(line2.bytes() => "[1518-{}-{} {}:{}] {}\n", month, day, hour, minute, unparsed_action);
let datetime = types::DateTime::new(month, day, hour, minute);
scan!(line2.bytes() => "[{}-{}-{} {}:{}] {}\n", year, month, day, hour, minute, unparsed_action);
let datetime = types::DateTime::new(year, month, day, hour, minute);
return Event::new(datetime, parse_action(&unparsed_action));
}
@ -113,7 +113,7 @@ fn get_sleepy_minute(shifts: &Vec<Shift>) -> (i32, i32) {
}
fn main() {
let lines: Vec<&str> = include_str!("../input").split("\n").collect();
let lines: Vec<&str> = include_str!("../input_big").split("\n").collect();
let mut events: Vec<Event> = Vec::new();
for line in lines {

View File

@ -22,6 +22,7 @@ pub struct Shift {
#[derive(Eq)]
pub struct DateTime {
pub year: i32,
pub month: i32,
pub day: i32,
pub hour: i32,
@ -31,8 +32,8 @@ pub struct DateTime {
// 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;
pub fn get_sortable_time(year: i32, month: i32, day: i32, hour: i32, minute: i32) -> i32 {
return minute + hour * 60 + day * 60 * 24 + month * 60 * 24 * 31 + year * 60 * 24 * 31 * 12;
}
impl Shift {
@ -51,20 +52,21 @@ impl Guard {
}
impl DateTime {
pub fn new(month: i32, day: i32, hour: i32, minute: i32) -> Self {
pub fn new(year: i32, month: i32, day: i32, hour: i32, minute: i32) -> Self {
DateTime {
year,
month,
day,
hour,
minute,
sortable_time: get_sortable_time(month, day, hour, minute)
sortable_time: get_sortable_time(year, month, day, hour, minute)
}
}
}
impl fmt::Display for DateTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}.{} {}:{}", self.day, self.month, self.hour, self.minute)
write!(f, "{}.{}.{} {}:{}", self.day, self.month, self.year, self.hour, self.minute)
}
}