From 3eb289c5bd376df5f7091ee02268901e0d9e03d6 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Tue, 18 Dec 2018 15:30:19 -0800 Subject: [PATCH] [2018][rust][15.1] s/next_step/chosen/ --- 2018/rust/src/bin/day_15.rs | 38 +++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/2018/rust/src/bin/day_15.rs b/2018/rust/src/bin/day_15.rs index a3a2ee1..db66bf3 100644 --- a/2018/rust/src/bin/day_15.rs +++ b/2018/rust/src/bin/day_15.rs @@ -225,7 +225,7 @@ impl Unit { .collect() } - fn next_step(&self, square: &Square, map: &Map) -> Option { + fn chosen(&self, square: &Square, map: &Map) -> Option { self.reachable(&square, &map) .into_iter() .fold(HashMap::new(), |mut m, (s, d)| { @@ -240,7 +240,7 @@ impl Unit { } #[test] -fn test_unit_targets() { +fn test_unit() { let map: Map = r" ####### #E..G.# @@ -274,9 +274,39 @@ fn test_unit_targets() { .iter() .map(|&x| Square(x)) .all(|x| reachable.contains_key(&x))); +} + +#[test] +fn test_unit_chosen() { + let map: Map = r" +####### +#E..G.# +#...#.# +#.G.#G# +####### + " + .parse() + .unwrap(); - let next_step = unit.next_step(&square, &map); - assert_eq!(next_step.unwrap().0, (1, 3)); + let square = Square((1, 1)); + let unit = map.units.get(&square).unwrap(); + let chosen = unit.chosen(&square, &map); + assert_eq!(chosen.unwrap().0, (1, 3)); + + let map: Map = r" +####### +#.E...# +#.....# +#...G.# +####### + " + .parse() + .unwrap(); + + let square = Square((1, 2)); + let unit = map.units.get(&square).unwrap(); + let chosen = unit.chosen(&square, &map); + assert_eq!(chosen.unwrap().0, (2, 4)); } #[derive(Debug, Eq, PartialEq)]