use alloc::vec;
use alloc::vec::Vec;
use crate::graph::Vertex;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct Move {
pub(super) from: Vertex,
pub(super) to: Vertex,
pub(super) finished: bool,
pub(super) cost: u32,
pub(super) rise: u32,
}
pub(super) struct Operators {
moves: Vec<Vec<Move>>,
least: Vec<u32>,
most: Vec<u32>,
}
impl Operators {
pub(super) fn new(mut moves: Vec<Vec<Move>>) -> Self {
for list in &mut moves {
list.sort_by_key(|step| step.rise);
}
let mut least = vec![0; moves.len() + 1];
let mut most = vec![0; moves.len() + 1];
for (agent, list) in moves.iter().enumerate().rev() {
let rise = |step: Option<&Move>| step.map_or(0, |step| step.rise);
least[agent] = least[agent + 1] + rise(list.first());
most[agent] = most[agent + 1] + rise(list.last());
}
Self { moves, least, most }
}
pub(super) fn next_rise(&self, rise: u32) -> Option<u32> {
let mut reachable = vec![true];
for list in &self.moves {
let widest = list.last().map_or(0, |step| step.rise as usize);
let mut next = vec![false; reachable.len() + widest];
for (sum, _) in reachable
.iter()
.enumerate()
.filter(|(_, reached)| **reached)
{
for step in list {
next[sum + step.rise as usize] = true;
}
}
reachable = next;
}
reachable
.iter()
.enumerate()
.skip(rise as usize + 1)
.find(|(_, reached)| **reached)
.map(|(sum, _)| u32::try_from(sum).expect("rise sums fit in u32"))
}
pub(super) fn with_rise(&self, rise: u32) -> Vec<Vec<Move>> {
let mut found = Vec::new();
self.extend(&mut Vec::with_capacity(self.moves.len()), rise, &mut found);
found
}
fn extend(&self, partial: &mut Vec<Move>, left: u32, found: &mut Vec<Vec<Move>>) {
let agent = partial.len();
if agent == self.moves.len() {
found.push(partial.clone());
return;
}
for &step in &self.moves[agent] {
let Some(rest) = left.checked_sub(step.rise) else {
break;
};
let reachable = (self.least[agent + 1]..=self.most[agent + 1]).contains(&rest);
if reachable && !partial.iter().any(|other| collide(*other, step)) {
partial.push(step);
self.extend(partial, rest, found);
partial.pop();
}
}
}
}
fn collide(a: Move, b: Move) -> bool {
let swap = a.from != a.to && a.from == b.to && a.to == b.from;
a.to == b.to || swap
}
#[cfg(test)]
mod tests {
use super::*;
fn step(from: Vertex, to: Vertex, rise: u32) -> Move {
Move {
from,
to,
finished: false,
cost: 1,
rise,
}
}
#[test]
fn combines_rises_and_skips_collisions() {
let operators = Operators::new(vec![
vec![step(0, 1, 0), step(0, 0, 1)],
vec![step(3, 2, 0), step(3, 3, 1), step(3, 1, 0)],
]);
let zero = operators.with_rise(0);
let one = operators.with_rise(1);
assert_eq!(zero.len(), 1, "0->1 with 3->1 collides");
assert_eq!(zero[0][1].to, 2);
assert_eq!(one.len(), 3);
assert_eq!(operators.next_rise(0), Some(1));
assert_eq!(operators.next_rise(1), Some(2));
assert_eq!(operators.next_rise(2), None);
}
#[test]
fn handles_rises_above_two() {
let operators = Operators::new(vec![
vec![step(0, 4, 0), step(0, 1, 3)],
vec![step(2, 2, 1)],
]);
assert_eq!(operators.next_rise(1), Some(4));
assert_eq!(operators.with_rise(4).len(), 1);
assert_eq!(operators.next_rise(4), None);
}
#[test]
fn swaps_collide() {
let operators = Operators::new(vec![vec![step(0, 1, 0)], vec![step(1, 0, 0)]]);
assert_eq!(operators.with_rise(0), Vec::<Vec<Move>>::new());
assert_eq!(operators.next_rise(0), None);
}
}