use width_search::{SearchResult, WidthProblem, hiw, ihiw, iterated_width};
const LEVEL: &[&str] = &[
"###############",
"#@....#...C..>#",
"#.###.#.#######",
"#.#a#.A.#c..#",
"#.#.#.###.#.#",
"#...#...B...#",
"#####.###.###",
" #..b#",
" #####",
];
const KEYS: usize = 3;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
struct State {
cell: usize,
keys: u8,
}
struct Dungeon {
cells: Vec<Vec<u8>>,
width: usize,
}
impl Dungeon {
fn at(&self, index: usize) -> u8 {
let (x, y) = (index % self.width, index / self.width);
*self.cells.get(y).and_then(|r| r.get(x)).unwrap_or(&b'#')
}
fn num_cells(&self) -> usize {
self.cells.len() * self.width
}
fn id(index: usize) -> u32 {
u32::try_from(index).expect("fewer than 2^32 features")
}
fn keys_held(s: &State, out: &mut Vec<u32>) {
out.extend((0..KEYS).filter(|&k| s.keys & (1 << k) != 0).map(Self::id));
}
}
impl WidthProblem for Dungeon {
type State = State;
type Action = char;
fn initial_state(&self) -> State {
let cell = (0..self.num_cells())
.find(|&i| self.at(i) == b'@')
.expect("a start");
State { cell, keys: 0 }
}
fn successors(&self, s: &State, out: &mut Vec<(char, State)>) {
let (x, y) = (s.cell % self.width, s.cell / self.width);
for (name, dx, dy) in [('^', 0, -1), ('v', 0, 1), ('<', -1, 0), ('>', 1, 0)] {
let (Some(x), Some(y)) = (x.checked_add_signed(dx), y.checked_add_signed(dy)) else {
continue;
};
if x >= self.width {
continue;
}
let cell = y * self.width + x;
let keys = match self.at(cell) {
b'#' | b' ' => continue,
door @ b'A'..=b'C' if s.keys & (1 << (door - b'A')) == 0 => continue,
key @ b'a'..=b'c' => s.keys | (1 << (key - b'a')),
_ => s.keys,
};
out.push((name, State { cell, keys }));
}
}
fn features(&self, s: &State, out: &mut Vec<u32>) {
out.push(Self::id(s.cell));
Self::keys_held(s, out);
for f in out.iter_mut().skip(1) {
*f += Self::id(self.num_cells());
}
}
fn num_features(&self) -> usize {
self.num_cells() + KEYS
}
fn is_goal(&self, s: &State) -> bool {
self.at(s.cell) == b'>'
}
}
fn report(name: &str, r: &SearchResult<char>) {
let plan = r
.plan
.as_ref()
.map_or("unsolvable?".to_string(), |p| format!("{} steps", p.len()));
println!("{name:<28} {plan:>12} {:>8} expanded", r.expanded);
}
fn main() {
let cells: Vec<Vec<u8>> = LEVEL.iter().map(|r| r.bytes().collect()).collect();
let width = cells.iter().map(Vec::len).max().unwrap_or(0);
let dungeon = Dungeon { cells, width };
let (_, r) = iterated_width(&dungeon, 1);
report("IW(1)", &r);
let (k, r) = iterated_width(&dungeon, 3);
let k = k.expect("the level has width 3 at most");
report(&format!("IW(1) .. IW({k})"), &r);
let r = hiw(&dungeon, Dungeon::keys_held, KEYS, 1, 1);
report("HIW(1, 1), keys as high level", &r);
report("IHIW(1, 1), finds the keys", &ihiw(&dungeon, 1, 1, 7));
let plan: String = r.plan.expect("the level is solvable").into_iter().collect();
println!("\nHIW path: {plan}");
}