Make clippy happy

This commit is contained in:
Timothy Warren 2023-12-14 07:56:59 -05:00
parent 6d9aaf081d
commit a70059328e

View File

@ -2,7 +2,7 @@ use std::collections::{HashMap, VecDeque};
use std::ops::Range; use std::ops::Range;
use DataType::*; use DataType::*;
const FILE_STR: &'static str = include_str!("input.txt"); const FILE_STR: &str = include_str!("input.txt");
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
enum DataType { enum DataType {
@ -109,7 +109,7 @@ impl Almanac {
let maps = data_chunks let maps = data_chunks
.into_iter() .into_iter()
.map(|chunk| { .map(|chunk| {
let mut lines: VecDeque<_> = chunk.split("\n").collect(); let mut lines: VecDeque<_> = chunk.split('\n').collect();
let type_line_parts: Vec<_> = let type_line_parts: Vec<_> =
lines.pop_front().unwrap().split_whitespace().collect(); lines.pop_front().unwrap().split_whitespace().collect();
let types: Vec<_> = type_line_parts[0] let types: Vec<_> = type_line_parts[0]
@ -119,11 +119,11 @@ impl Almanac {
let (from, to) = (types[0], types[1]); let (from, to) = (types[0], types[1]);
let mappings: Vec<DataMap> = lines let mappings: Vec<DataMap> = lines
.into_iter() .into_iter()
.filter(|l| l.len() > 0) .filter(|l| !l.is_empty())
.map(|line| { .map(|line| {
line.split_whitespace() line.split_whitespace()
.map(|s| s.trim()) .map(|s| s.trim())
.filter(|s| s.len() > 0) .filter(|s| !s.is_empty())
.map(|s| s.parse::<u64>().unwrap()) .map(|s| s.parse::<u64>().unwrap())
.collect::<Vec<u64>>() .collect::<Vec<u64>>()
}) })
@ -144,11 +144,10 @@ impl Almanac {
fn x_from_y(&self, x: DataType, y: DataType, search: u64) -> u64 { fn x_from_y(&self, x: DataType, y: DataType, search: u64) -> u64 {
self.maps self.maps
.get(&(y, x)) .get(&(y, x))
.expect(&format!("Missing mapping from {:?} to {:?}", x, y)) .unwrap_or_else(|| panic!("Missing mapping from {:?} to {:?}", x, y))
.into_iter() .iter()
.filter(|dm| dm.to_range.contains(&search))
.find_map(|dm| dm.get_dest_idx(search)) .find_map(|dm| dm.get_dest_idx(search))
.unwrap_or_else(|| search) .unwrap_or(search)
} }
fn location_from_seed(&self, seed: u64) -> u64 { fn location_from_seed(&self, seed: u64) -> u64 {
@ -162,31 +161,36 @@ impl Almanac {
self.x_from_y(Location, Humidity, humid) self.x_from_y(Location, Humidity, humid)
} }
fn locations_from_seeds(&self) -> HashMap<u64, u64> { fn locations_from_seeds(&self) -> Vec<u64> {
self.seeds self.seeds
.iter() .iter()
.map(|s| (*s, self.location_from_seed(*s))) .map(|s| self.x_from_y(Soil, Seed, *s))
.map(|s| self.x_from_y(Fertilizer, Soil, s))
.map(|f| self.x_from_y(Water, Fertilizer, f))
.map(|w| self.x_from_y(Light, Water, w))
.map(|l| self.x_from_y(Temperature, Light, l))
.map(|t| self.x_from_y(Humidity, Temperature, t))
.map(|h| self.x_from_y(Location, Humidity, h))
.collect() .collect()
} }
fn lowest_seed_location(&self) -> u64 { fn lowest_seed_location(&self) -> u64 {
self.locations_from_seeds() self.locations_from_seeds().iter().copied().min().unwrap()
.iter()
.map(|(_, l)| *l)
.min()
.unwrap()
} }
fn lowest_seed_range_location(&self) -> u64 { fn lowest_seed_range_location(&self) -> u64 {
let range_flattener = |r: Range<u64>| r.clone().collect::<Vec<_>>();
self.seed_ranges self.seed_ranges
.iter() .clone()
.map(|r| { .into_iter()
r.clone() .flat_map(range_flattener)
.into_iter() .map(|s| self.x_from_y(Soil, Seed, s))
.map(|s| self.location_from_seed(s)) .map(|s| self.x_from_y(Fertilizer, Soil, s))
.min() .map(|f| self.x_from_y(Water, Fertilizer, f))
.unwrap() .map(|w| self.x_from_y(Light, Water, w))
}) .map(|l| self.x_from_y(Temperature, Light, l))
.map(|t| self.x_from_y(Humidity, Temperature, t))
.map(|h| self.x_from_y(Location, Humidity, h))
.min() .min()
.unwrap() .unwrap()
} }
@ -229,7 +233,7 @@ mod tests {
#[test] #[test]
fn test_locations_from_seeds() { fn test_locations_from_seeds() {
let almanac = Almanac::parse(EXAMPLE_FILE_STR); let almanac = Almanac::parse(EXAMPLE_FILE_STR);
let expected = HashMap::from([(79, 82), (14, 43), (55, 86), (13, 35)]); let expected = Vec::from([82, 43, 86, 35]);
assert_eq!(expected, almanac.locations_from_seeds()); assert_eq!(expected, almanac.locations_from_seeds());
} }