use width_search::{WidthProblem, iterated_width};
const MAP: &[&str] = &[
"#########",
"#S..#..G#",
"#...D...#",
"#...#####",
"#..K#",
"#####",
];
#[derive(Clone, Copy, Debug)]
enum Move {
Up,
Down,
Left,
Right,
}
struct Grid {
cells: Vec<Vec<u8>>,
width: usize,
}
#[derive(Clone)]
struct S {
x: usize,
y: usize,
key: bool,
}
impl Grid {
fn find(&self, c: u8) -> (usize, usize) {
for (y, row) in self.cells.iter().enumerate() {
if let Some(x) = row.iter().position(|&b| b == c) {
return (x, y);
}
}
panic!("{} not on map", char::from(c))
}
fn at(&self, x: usize, y: usize) -> u8 {
*self.cells.get(y).and_then(|r| r.get(x)).unwrap_or(&b'#')
}
fn feature(index: usize) -> u32 {
u32::try_from(index).expect("map has fewer than 2^32 cells")
}
}
impl WidthProblem for Grid {
type State = S;
type Action = Move;
fn initial_state(&self) -> S {
let (x, y) = self.find(b'S');
S { x, y, key: false }
}
fn successors(&self, s: &S, out: &mut Vec<(Move, S)>) {
for (m, dx, dy) in [
(Move::Up, 0, -1),
(Move::Down, 0, 1),
(Move::Left, -1, 0),
(Move::Right, 1, 0),
] {
let (Some(x), Some(y)) = (s.x.checked_add_signed(dx), s.y.checked_add_signed(dy))
else {
continue;
};
match self.at(x, y) {
b'#' => {}
b'D' if !s.key => {}
c => out.push((
m,
S {
x,
y,
key: s.key || c == b'K',
},
)),
}
}
}
fn features(&self, s: &S, out: &mut Vec<u32>) {
let positions = self.cells.len() * self.width;
out.push(Self::feature(s.y * self.width + s.x));
out.push(Self::feature(positions + usize::from(s.key)));
}
fn num_features(&self) -> usize {
self.cells.len() * self.width + 2
}
fn is_goal(&self, s: &S) -> bool {
self.at(s.x, s.y) == b'G'
}
}
fn main() {
let cells: Vec<Vec<u8>> = MAP.iter().map(|r| r.bytes().collect()).collect();
let width = cells.iter().map(Vec::len).max().unwrap();
let grid = Grid { cells, width };
let (k, r) = iterated_width(&grid, 3);
println!(
"solved with width {k:?}: {} expanded, {} generated",
r.expanded, r.generated
);
println!("plan: {:?}", r.plan.expect("solvable"));
}