From 601de2c56569c14c2bfbb0757e71ffbbf4b59dcc Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Sun, 2 Jan 2022 18:30:13 +0100 Subject: [PATCH] Readability --- 2021/src/day23.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/2021/src/day23.rs b/2021/src/day23.rs index edb05a1..b67dc2b 100644 --- a/2021/src/day23.rs +++ b/2021/src/day23.rs @@ -29,6 +29,10 @@ impl Pod { Pod::D => 1000, } } + + pub fn dest(self) -> usize { + self as usize + } } impl TryFrom for Pod { @@ -129,7 +133,7 @@ impl State { .filter_map(|(pos, &pod)| { let pod = pod?; - let destination_pos = room_hallway_pos(pod as usize); + let destination_pos = room_hallway_pos(pod.dest()); Some(abs_delta(pos, destination_pos) as u32 * pod.cost()) }) @@ -147,12 +151,12 @@ impl State { .enumerate() .rev() .skip_while(|&(_, &entry)| { - entry.map(|pod| pod as usize == room_index).unwrap_or(false) + entry.map(|pod| pod.dest() == room_index).unwrap_or(false) }) .filter_map(|(room_pos, &pod)| { let pod = pod?; - let destination_pos = room_hallway_pos(pod as usize); + let destination_pos = room_hallway_pos(pod.dest()); let steps = 1 + room_pos + abs_delta(hallway_pos, destination_pos).max(2); @@ -175,7 +179,7 @@ impl State { // Check if we even want to move anything out of this room if room .iter() - .all(|entry| entry.map(|pod| pod as usize == index).unwrap_or(true)) + .all(|entry| entry.map(|pod| pod.dest() == index).unwrap_or(true)) { continue; } @@ -240,7 +244,7 @@ impl State { .enumerate() .filter_map(|(pos, pod)| pod.map(|pod| (pos, pod))) { - let room = pod as usize; + let room = pod.dest(); let new_hallway_pos = room_hallway_pos(room); // Check if the path is free