use std::collections::{BTreeMap, VecDeque};
use std::time::{Duration, SystemTime};
pub const GROWTH_WINDOW: Duration = crate::api::GROWTH_WINDOW;
pub const BRAKE_FOR: Duration = Duration::from_mins(30);
pub const GUARD_HOLD: Duration = Duration::from_mins(5);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Guard {
Clear,
Crossed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Use<Id> {
pub runner: Id,
pub memory: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Braked {
Grew {
until: SystemTime,
grew: u64,
kept: u64,
},
Restarted {
until: SystemTime,
},
Guarded {
until: SystemTime,
},
}
impl Braked {
pub fn until(self) -> SystemTime {
match self {
Braked::Grew { until, .. }
| Braked::Restarted { until }
| Braked::Guarded { until } => until,
}
}
}
#[derive(Debug, Clone)]
struct Look<Id> {
at: SystemTime,
used: BTreeMap<Id, u64>,
room: u64,
}
#[derive(Debug, Clone)]
pub struct Growth<Id> {
looks: VecDeque<Look<Id>>,
braked: Option<Braked>,
}
impl<Id> Default for Growth<Id> {
fn default() -> Self {
Self {
looks: VecDeque::new(),
braked: None,
}
}
}
impl<Id: Ord> Growth<Id> {
pub fn restarted(now: SystemTime) -> Self {
Self {
looks: VecDeque::new(),
braked: Some(Braked::Restarted {
until: now + GROWTH_WINDOW,
}),
}
}
pub fn begin_again(&mut self, now: SystemTime) {
let restarted = Braked::Restarted {
until: now + GROWTH_WINDOW,
};
self.looks.clear();
self.braked = Some(
self.braked(now)
.filter(|standing| standing.until() > restarted.until())
.unwrap_or(restarted),
);
}
pub fn look(
&mut self,
now: SystemTime,
used: impl IntoIterator<Item = Use<Id>>,
room: u64,
guard: Guard,
) {
let used: BTreeMap<Id, u64> = used
.into_iter()
.map(|runner| (runner.runner, runner.memory))
.collect();
let from = now
.checked_sub(GROWTH_WINDOW)
.unwrap_or(SystemTime::UNIX_EPOCH);
while self.looks.front().is_some_and(|look| look.at < from) {
self.looks.pop_front();
}
let grown = self
.looks
.iter()
.map(|look| (grew(&look.used, &used), look.room))
.filter(|(grew, room)| grew > room)
.max_by_key(|(grew, room)| grew - room);
if let Some((grew, kept)) = grown {
self.braked = Some(Braked::Grew {
until: now + BRAKE_FOR,
grew,
kept,
});
} else if guard == Guard::Crossed && self.braked.is_none_or(|braked| braked.until() <= now)
{
self.braked = Some(Braked::Guarded {
until: now + GUARD_HOLD,
});
} else if self.braked.is_some_and(|braked| braked.until() <= now) {
self.braked = None;
}
self.looks.push_back(Look {
at: now,
used,
room,
});
}
pub fn braked(&self, now: SystemTime) -> Option<Braked> {
self.braked.filter(|braked| now < braked.until())
}
}
#[cfg(test)]
impl<Id> Growth<Id> {
pub(crate) fn age(&mut self, by: Duration) {
for look in &mut self.looks {
look.at -= by;
}
self.braked = self.braked.map(|braked| match braked {
Braked::Grew { until, grew, kept } => Braked::Grew {
until: until - by,
grew,
kept,
},
Braked::Restarted { until } => Braked::Restarted { until: until - by },
Braked::Guarded { until } => Braked::Guarded { until: until - by },
});
}
}
fn grew<Id: Ord>(then: &BTreeMap<Id, u64>, now: &BTreeMap<Id, u64>) -> u64 {
let (before, after) = then
.iter()
.filter_map(|(runner, before)| Some((*before, *now.get(runner)?)))
.fold((0_u64, 0_u64), |(before, after), (was, is)| {
(before.saturating_add(was), after.saturating_add(is))
});
after.saturating_sub(before)
}