From 5e8b9741372eacb210629f4f1a20d3a55c69b443 Mon Sep 17 00:00:00 2001 From: Keenan Tims Date: Mon, 16 Dec 2024 14:54:45 -0800 Subject: [PATCH] clippies! --- src/day14.rs | 2 +- src/day15.rs | 6 +++--- src/day16.rs | 26 +++++++++++++++----------- utils/misc/src/lib.rs | 7 +++++-- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/day14.rs b/src/day14.rs index 931f6d0..ce53376 100644 --- a/src/day14.rs +++ b/src/day14.rs @@ -32,7 +32,7 @@ enum Quadrant { } fn nom_i64(input: &str) -> IResult<&str, i64> { - let (i, number) = map_res(recognize(preceded(opt(tag("-")), digit1)), |s| i64::from_str(s))(input)?; + let (i, number) = map_res(recognize(preceded(opt(tag("-")), digit1)), i64::from_str)(input)?; Ok((i, number)) } fn nom_i64_pair(input: &str) -> IResult<&str, (i64, i64)> { diff --git a/src/day15.rs b/src/day15.rs index 86bdad2..08149fe 100644 --- a/src/day15.rs +++ b/src/day15.rs @@ -16,7 +16,7 @@ impl Display for Warehouse { impl Warehouse { fn step_robot(&mut self, dir: Move) { - let start = self.robot_pos.clone(); + let start = self.robot_pos; if self.push(&start, &dir) { self.robot_pos = &self.robot_pos + dir.ofs(); } @@ -40,12 +40,12 @@ impl Warehouse { // move both parts self.push(&target, dir); self.push(&(&target + (-1, 0)), dir); - self.map.swap(&target, pos); + self.map.swap(target, pos); } b'[' => { self.push(&target, dir); self.push(&(&target + (1, 0)), dir); - self.map.swap(&target, pos); + self.map.swap(target, pos); } c => panic!("unexpected char {}", c), } diff --git a/src/day16.rs b/src/day16.rs index 930daa8..34fed6c 100644 --- a/src/day16.rs +++ b/src/day16.rs @@ -1,9 +1,9 @@ use aoc_runner_derive::aoc; use grid::Grid; use std::{ + cmp::Ordering, collections::{BinaryHeap, HashMap}, str::FromStr, - usize, }; type CoordType = i16; @@ -73,7 +73,7 @@ impl Ord for PathState { } impl PartialOrd for PathState { fn partial_cmp(&self, other: &Self) -> Option { - self.state.partial_cmp(&other.state) + Some(self.cmp(other)) } } @@ -177,14 +177,18 @@ impl Maze { continue; } if state.position == finish { - if state.cost < best_cost { - path.push(state.position); - best_paths.clear(); - best_paths.push(path); - best_cost = state.cost - } else if state.cost == best_cost { - path.push(state.position); - best_paths.push(path); + match state.cost.cmp(&best_cost) { + Ordering::Less => { + path.push(state.position); + best_paths.clear(); + best_paths.push(path); + best_cost = state.cost + } + Ordering::Equal => { + path.push(state.position); + best_paths.push(path); + } + _ => {} } continue; } @@ -204,7 +208,7 @@ impl Maze { } } } - return (best_cost, best_paths); + (best_cost, best_paths) } } diff --git a/utils/misc/src/lib.rs b/utils/misc/src/lib.rs index 3aac068..b7d4b5c 100644 --- a/utils/misc/src/lib.rs +++ b/utils/misc/src/lib.rs @@ -1,6 +1,6 @@ use num_traits::Signed; +use std::fmt::Display; use std::ops::{Add, AddAssign}; -use std::fmt::{Debug, Display}; /// Wrapped signed integer with custom upper bound with wrapping of 0s to the upper bound #[derive(Eq, Clone, Copy)] @@ -71,7 +71,10 @@ impl PartialOrd for CustomWrapped { } } -impl Display for CustomWrapped where T: Display { +impl Display for CustomWrapped +where + T: Display, +{ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.val.fmt(f) }