//...
fn part_one(guards: &HashMap) -> i32 {
let max_guard = guards
.iter()
.map(|(_, g)| g)
.max_by_key(|g| g.get_total_sleep())
.unwrap();
max_guard.id * max_guard.get_highest_freq_minute().0
}
fn part_two(guards: &HashMap) -> i32 {
let guard = guards
.iter()
.max_by_key(|(_, v)| v.get_highest_freq_minute().1)
.map(|(_, v)| v)
.unwrap();
guard.id * guard.get_highest_freq_minute().0 // can't map above, lifetime issues ahoy
}
//...
#[derive(Debug)]
struct Duration {
minutes: i32,
sleep_minutes: Vec,
}
impl Duration {
fn from_sleep(start: i32, end: i32) -> Duration {
Duration {
minutes: end - start,
sleep_minutes: (start..end).collect(),
}
}
}
type Minute = i32;
type Frequency = i32;
#[derive(Debug)]
struct Guard {
pub id: i32,
pub sleep_durations: Vec,
}
impl Guard {
fn new(id: i32) -> Guard {
Guard {
id,
sleep_durations: Vec::new(),
}
}
fn add_sleep_duration(&mut self, duration: Duration) {
self.sleep_durations.push(duration);
}
fn get_total_sleep(&self) -> i32 {
self.sleep_durations.iter().map(|d| d.minutes).sum()
}
fn get_highest_freq_minute(&self) -> (Minute, Frequency) {
let mut numbers = HashMap::new();
self.sleep_durations
.iter()
.flat_map(|d| d.sleep_minutes.clone())
.for_each(|min| {
*numbers.entry(min).or_insert(0) += 1;
});
let result = numbers
.iter()
.max_by_key(|&(_, count)| count)
.unwrap_or_else(|| (&0, &0)); //some guards don't sleep apparently...
(result.0.clone(), result.1.clone())
}
}
spent too long fucking around with data structures, should've just gone straight in with some imperative shit
whatever time to sleep