use crate::geom::{DirSet, Direction, Point};
#[must_use]
pub fn min_bends(from: Point, heading: Direction, to: Point, arrivals: DirSet) -> Option<u32> {
arrivals
.iter()
.map(|arrival| bends_to(from, heading, to, arrival))
.min()
}
fn bends_to(from: Point, heading: Direction, to: Point, arrival: Direction) -> u32 {
let (dx, dy) = rotate_to_right(
i64::from(to.x) - i64::from(from.x),
i64::from(to.y) - i64::from(from.y),
heading,
);
let turns = (arrival.index() + 4 - heading.index()) % 4;
match turns {
0 if dy == 0 && dx >= 0 => 0,
0 if dx > 0 => 2,
0 => 4,
1 if dx >= 0 && dy > 0 => 1,
3 if dx >= 0 && dy < 0 => 1,
1 | 3 => 3,
_ if dy != 0 => 2,
_ => 4,
}
}
fn rotate_to_right(dx: i64, dy: i64, heading: Direction) -> (i64, i64) {
match heading {
Direction::Right => (dx, dy),
Direction::Up => (dy, -dx),
Direction::Left => (-dx, -dy),
Direction::Down => (-dy, dx),
}
}
#[cfg(test)]
mod tests {
extern crate std;
use std::collections::VecDeque;
use std::vec;
use super::*;
const R: i32 = 12;
fn bfs(target: Point, arrival: Direction) -> std::vec::Vec<u32> {
let side = usize::try_from(2 * R + 1).unwrap();
let index = |p: Point, d: Direction| {
let x = usize::try_from(p.x + R).unwrap();
let y = usize::try_from(p.y + R).unwrap();
(y * side + x) * 4 + d.index()
};
let inside = |p: Point| p.x.abs() <= R && p.y.abs() <= R;
let mut dist = vec![u32::MAX; side * side * 4];
let mut queue = VecDeque::new();
dist[index(target, arrival)] = 0;
queue.push_back((target, arrival));
while let Some((p, d)) = queue.pop_front() {
let here = dist[index(p, d)];
let (sx, sy) = d.delta();
let prev = Point::new(p.x - sx, p.y - sy);
if !inside(prev) {
continue;
}
for e in Direction::ALL {
let cost = match e.turns_to(d) {
0 => 0,
1 => 1,
_ => continue,
};
let slot = index(prev, e);
if here + cost < dist[slot] {
dist[slot] = here + cost;
if cost == 0 {
queue.push_front((prev, e));
} else {
queue.push_back((prev, e));
}
}
}
}
dist
}
#[test]
fn closed_form_matches_bfs() {
let side = usize::try_from(2 * R + 1).unwrap();
for arrival in Direction::ALL {
let target = Point::new(0, 0);
let dist = bfs(target, arrival);
for x in -4..=4 {
for y in -4..=4 {
for heading in Direction::ALL {
let p = Point::new(x, y);
let ix = usize::try_from(x + R).unwrap();
let iy = usize::try_from(y + R).unwrap();
let expected = dist[(iy * side + ix) * 4 + heading.index()];
let expected = if p == target && heading == arrival {
0
} else {
expected
};
assert_eq!(
bends_to(p, heading, target, arrival),
expected,
"from {p} heading {heading:?} arriving {arrival:?}"
);
}
}
}
}
}
#[test]
fn compare_with_paper_table() {
let o = Point::new(0, 0);
let d = Direction::Up;
assert_eq!(bends_to(o, d, Point::new(0, 5), Direction::Up), 0);
assert_eq!(bends_to(o, d, Point::new(3, 5), Direction::Right), 1);
assert_eq!(bends_to(o, d, Point::new(3, 5), Direction::Up), 2);
assert_eq!(bends_to(o, d, Point::new(3, 5), Direction::Down), 2);
assert_eq!(bends_to(o, d, Point::new(3, -5), Direction::Right), 3);
assert_eq!(bends_to(o, d, Point::new(0, -5), Direction::Down), 4);
assert_eq!(bends_to(o, d, Point::new(3, -5), Direction::Up), 4);
assert_eq!(bends_to(o, d, Point::new(3, 5), Direction::Left), 3);
assert_eq!(bends_to(o, d, Point::new(5, 0), Direction::Right), 1);
}
}