1use crate::Loc;
2
3impl Loc {
4 pub fn manhattan(self, other: Loc) -> usize {
8 let dx: usize = self.x.abs_diff(other.x) as usize;
9 let dy: usize = self.y.abs_diff(other.y) as usize;
10 dx + dy
11 }
12}
13
14#[cfg(test)]
15mod tests {
16 use crate::Loc;
17
18 #[test]
19 fn manhattan_same() {
20 let loc: Loc = Loc::new(3, 4);
21 assert_eq!(loc.manhattan(loc), 0);
22 }
23
24 #[test]
25 fn manhattan_axis() {
26 let a: Loc = Loc::new(0, 0);
27 let b: Loc = Loc::new(5, 0);
28 assert_eq!(a.manhattan(b), 5);
29 }
30
31 #[test]
32 fn manhattan_diagonal() {
33 let a: Loc = Loc::new(0, 0);
34 let b: Loc = Loc::new(3, 4);
35 assert_eq!(a.manhattan(b), 7);
36 }
37
38 #[test]
39 fn manhattan_symmetric() {
40 let a: Loc = Loc::new(1, 5);
41 let b: Loc = Loc::new(4, 2);
42 assert_eq!(a.manhattan(b), b.manhattan(a));
43 }
44}