(rust) more wip for day 4. almost done, but I need sleep
This commit is contained in:
parent
c1464f9f33
commit
b2401c3d27
|
@ -1,5 +1,5 @@
|
|||
#[macro_use] extern crate text_io;
|
||||
use std::fs;
|
||||
use std::collections::HashMap;
|
||||
mod types;
|
||||
use types::*;
|
||||
|
||||
|
@ -17,19 +17,81 @@ fn parse_action(input: &String) -> GuardAction {
|
|||
}
|
||||
}
|
||||
|
||||
fn event_from_line(line: &String) -> Event {
|
||||
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);
|
||||
// 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);
|
||||
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()));
|
||||
fn events_to_shifts(events: Vec<Event>) -> Vec<Shift> {
|
||||
let mut shifts: Vec<Shift> = Vec::new();
|
||||
// It was easier to parse by just adding a dummy shift at the beginning
|
||||
let mut current_shift = Shift::new(-1);
|
||||
for event in events {
|
||||
match event.action {
|
||||
GuardAction::BeginShift(gid) => {
|
||||
shifts.push(current_shift);
|
||||
current_shift = Shift::new(gid);
|
||||
},
|
||||
_ => current_shift.events.push(event)
|
||||
}
|
||||
|
||||
}
|
||||
// Remove the dummy we added earlier
|
||||
shifts.remove(0);
|
||||
return shifts;
|
||||
}
|
||||
|
||||
fn group_shifts_by_guard(shifts: Vec<Shift>) -> HashMap<i32, Vec<Shift>> {
|
||||
let mut shifts_by_guard: HashMap<i32, Vec<Shift>> = HashMap::new();
|
||||
for shift in shifts {
|
||||
let shifts_for_guard = shifts_by_guard.entry(shift.guard.id).or_insert_with(Vec::new);
|
||||
shifts_for_guard.push(shift);
|
||||
}
|
||||
return shifts_by_guard;
|
||||
}
|
||||
|
||||
fn calculate_sleep(events: Vec<Event>) -> Vec<bool> {
|
||||
let mut minutes = vec![false; 60];
|
||||
let mut sleeping = false;
|
||||
let mut last = 0;
|
||||
for e in events {
|
||||
if sleeping {
|
||||
for i in (last..e.datetime.minute) {
|
||||
minutes[i as usize] = true;
|
||||
}
|
||||
}
|
||||
last = e.datetime.minute;
|
||||
sleeping = match e.action {
|
||||
GuardAction::FallAsleep => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
return minutes;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let lines: Vec<&str> = include_str!("../input").split("\n").collect();
|
||||
|
||||
let mut events: Vec<Event> = Vec::new();
|
||||
for line in lines {
|
||||
events.push(event_from_line(line.to_string()));
|
||||
}
|
||||
|
||||
events.sort();
|
||||
let shifts = events_to_shifts(events);
|
||||
/*
|
||||
for shift in shifts {
|
||||
for event in shift.events {
|
||||
println!("{}", event.datetime);
|
||||
}
|
||||
}
|
||||
*/
|
||||
let shifts_by_guard = group_shifts_by_guard(shifts);
|
||||
let sleepy_guard = find_sleepy_guard(&shifts_by_guard);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use std::cmp;
|
||||
use std::{cmp,fmt};
|
||||
|
||||
pub struct Event {
|
||||
datetime: DateTime,
|
||||
action: GuardAction,
|
||||
pub datetime: DateTime,
|
||||
pub action: GuardAction,
|
||||
}
|
||||
|
||||
pub enum GuardAction {
|
||||
|
@ -12,17 +12,21 @@ pub enum GuardAction {
|
|||
}
|
||||
|
||||
pub struct Guard {
|
||||
events: Vec<Event>,
|
||||
id: i32,
|
||||
pub id: i32,
|
||||
}
|
||||
|
||||
pub struct Shift {
|
||||
pub guard: Guard,
|
||||
pub events: Vec<Event>,
|
||||
}
|
||||
|
||||
#[derive(Eq)]
|
||||
pub struct DateTime {
|
||||
month: i32,
|
||||
day: i32,
|
||||
hour: i32,
|
||||
minute: i32,
|
||||
sortable_time: i32,
|
||||
pub month: i32,
|
||||
pub day: i32,
|
||||
pub hour: i32,
|
||||
pub minute: i32,
|
||||
pub 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
|
||||
|
@ -31,6 +35,20 @@ pub struct DateTime {
|
|||
return minute + hour * 60 + day * 60 * 24 + month * 60 * 24 * 31;
|
||||
}
|
||||
|
||||
impl Shift {
|
||||
pub fn new(gid: i32) -> Self {
|
||||
Shift {
|
||||
guard: Guard::new(gid),
|
||||
events: Vec::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Guard {
|
||||
pub fn new(id: i32) -> Self {
|
||||
Guard { id }
|
||||
}
|
||||
}
|
||||
|
||||
impl DateTime {
|
||||
pub fn new(month: i32, day: i32, hour: i32, minute: i32) -> Self {
|
||||
|
@ -42,7 +60,12 @@ impl DateTime {
|
|||
sortable_time: get_sortable_time(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)
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for DateTime {
|
||||
|
@ -72,3 +95,23 @@ impl Event {
|
|||
}
|
||||
}
|
||||
|
||||
impl Eq for Event {}
|
||||
|
||||
impl Ord for Event {
|
||||
fn cmp(&self, other: &Event) -> cmp::Ordering {
|
||||
self.datetime.cmp(&other.datetime)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for Event {
|
||||
fn partial_cmp(&self, other: &Event) -> Option<cmp::Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Event {
|
||||
fn eq(&self, other: &Event) -> bool {
|
||||
self.datetime == other.datetime
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user