Readability

This commit is contained in:
2022-01-02 18:30:13 +01:00
parent 894524bc81
commit 601de2c565

View File

@@ -29,6 +29,10 @@ impl Pod {
Pod::D => 1000, Pod::D => 1000,
} }
} }
pub fn dest(self) -> usize {
self as usize
}
} }
impl TryFrom<char> for Pod { impl TryFrom<char> for Pod {
@@ -129,7 +133,7 @@ impl<const S: usize> State<S> {
.filter_map(|(pos, &pod)| { .filter_map(|(pos, &pod)| {
let pod = 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()) Some(abs_delta(pos, destination_pos) as u32 * pod.cost())
}) })
@@ -147,12 +151,12 @@ impl<const S: usize> State<S> {
.enumerate() .enumerate()
.rev() .rev()
.skip_while(|&(_, &entry)| { .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)| { .filter_map(|(room_pos, &pod)| {
let pod = 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); let steps = 1 + room_pos + abs_delta(hallway_pos, destination_pos).max(2);
@@ -175,7 +179,7 @@ impl<const S: usize> State<S> {
// Check if we even want to move anything out of this room // Check if we even want to move anything out of this room
if room if room
.iter() .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; continue;
} }
@@ -240,7 +244,7 @@ impl<const S: usize> State<S> {
.enumerate() .enumerate()
.filter_map(|(pos, pod)| pod.map(|pod| (pos, pod))) .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); let new_hallway_pos = room_hallway_pos(room);
// Check if the path is free // Check if the path is free