From 39746c982c89f1709afbc568b0fd830b98ad2eb7 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Wed, 16 Dec 2020 13:51:35 -0800 Subject: [PATCH] cleanup many clippy pedantic warnings Signed-off-by: Stephen Waits --- src/day04.rs | 39 +++++++++++---------------------------- src/day06.rs | 9 ++++++++- src/day08.rs | 15 +++++++-------- src/day10.rs | 10 +++++----- src/day11.rs | 14 +++++++------- src/day12.rs | 3 +-- src/day13.rs | 1 + src/day14.rs | 3 +-- src/day15.rs | 2 +- src/day16.rs | 6 +++--- src/utils.rs | 5 ----- 11 files changed, 45 insertions(+), 62 deletions(-) diff --git a/src/day04.rs b/src/day04.rs index 47bd278..01ea39d 100644 --- a/src/day04.rs +++ b/src/day04.rs @@ -45,29 +45,23 @@ impl Passport { // byr (Birth Year) - four digits; at least 1920 and at most 2002. fn is_byr_valid(&self) -> bool { - if let Some(s) = &self.byr { + self.byr.as_ref().map_or(false, |s| { utils::parse_usize_in_range(&s, 1920, 2002).is_some() - } else { - false - } + }) } // iyr (Issue Year) - four digits; at least 2010 and at most 2020. fn is_iyr_valid(&self) -> bool { - if let Some(s) = &self.iyr { + self.iyr.as_ref().map_or(false, |s| { utils::parse_usize_in_range(&s, 2010, 2020).is_some() - } else { - false - } + }) } // eyr (Expiration Year) - four digits; at least 2020 and at most 2030. fn is_eyr_valid(&self) -> bool { - if let Some(s) = &self.eyr { + self.eyr.as_ref().map_or(false, |s| { utils::parse_usize_in_range(&s, 2020, 2030).is_some() - } else { - false - } + }) } // hgt (Height) - a number followed by either cm or in: @@ -98,11 +92,7 @@ impl Passport { Regex::new(r"^#[0-9a-f]{6}$").expect("regex compilation failure"); } - if let Some(s) = &self.hcl { - HCL_RE.is_match(s) - } else { - false - } + self.hcl.as_ref().map_or(false, |s| HCL_RE.is_match(s)) } // ecl (Eye Color) - exactly one of: amb blu brn gry grn hzl oth. @@ -112,11 +102,7 @@ impl Passport { Regex::new(r"^(amb|blu|brn|gry|grn|hzl|oth)$").expect("regex compilation failure"); } - if let Some(s) = &self.ecl { - ECL_RE.is_match(s) - } else { - false - } + self.ecl.as_ref().map_or(false, |s| ECL_RE.is_match(s)) } // pid (Passport ID) - a nine-digit number, including leading zeroes. @@ -126,14 +112,11 @@ impl Passport { Regex::new(r"^[0-9]{9}$").expect("regex compilation failure"); } - if let Some(s) = &self.pid { - PID_RE.is_match(s) - } else { - false - } + self.pid.as_ref().map_or(false, |s| PID_RE.is_match(s)) } // cid (Country ID) - ignored, missing or not. + #[allow(clippy::unused_self)] fn is_cid_valid(&self) -> bool { true } @@ -149,7 +132,7 @@ impl FromStr for Passport { .expect("regex compilation failure"); } - let mut ret: Self = Default::default(); + let mut ret: Self = Passport::default(); s.split_whitespace().for_each(|s| { PASSPORT_RE.captures_iter(&s).for_each(|cap| match &cap[1] { "byr" => ret.byr = Some(cap[2].to_string()), diff --git a/src/day06.rs b/src/day06.rs index e1dd2f0..5280185 100644 --- a/src/day06.rs +++ b/src/day06.rs @@ -8,7 +8,14 @@ use std::{ }; fn count_uniq_alpha_chars(s: &str) -> usize { - HashSet::::from_iter(s.chars().filter(|c| c.is_alphabetic()).map(|c| c as usize)).len() + HashSet::::from_iter(s.chars().filter_map(|c| { + if c.is_alphabetic() { + Some(c as usize) + } else { + None + } + })) + .len() } fn count_common_answers(s: &str) -> usize { diff --git a/src/day08.rs b/src/day08.rs index 96394d5..4285970 100644 --- a/src/day08.rs +++ b/src/day08.rs @@ -91,16 +91,16 @@ impl Console { // execute instruction at pc self.hits[self.pc] += 1; - match self.program[self.pc] { + match self.program.get(self.pc).unwrap() { Instruction::Acc(n) => { self.accumulator += n; self.pc += 1; } Instruction::Jmp(n) => { - if n < 0 { + if *n < 0 { self.pc -= n.abs() as usize; } else { - self.pc += n as usize; + self.pc += *n as usize; } } Instruction::Nop(_) => { @@ -127,13 +127,12 @@ impl Console { .hits .iter() .enumerate() - .filter(|(_, hits)| **hits > 0) // filter only instructions we hit - .map(|(i, _)| i) // map just the index - .filter(|i| match self.program[*i as usize] { + .filter_map(|(i, hits)| if *hits > 0 { Some(i) } else { None }) + .filter(|i| match self.program.get(*i as usize).unwrap() { // filter instructions we can modify Instruction::Jmp(_) => true, - Instruction::Nop(n) => n != 0, - _ => false, + Instruction::Nop(n) => *n != 0, + Instruction::Acc(_) => false, }) .collect(); diff --git a/src/day10.rs b/src/day10.rs index a4ac6c5..609d461 100644 --- a/src/day10.rs +++ b/src/day10.rs @@ -13,12 +13,12 @@ pub fn count_diff1_diff3(diffs: &[i64]) -> usize { // follows the pattern 0, 1, 1, 2, 4, where the total paths for a runlength N // is sum of the paths in N-3, N-2, and N-1. fn paths_by_runlength(runlength: usize) -> usize { - let mut p: Vec = vec![0, 1, 1]; // vec of most recent 4 in sequence - const L: usize = 3; // length of p Vec - for i in L..runlength + 1 { - p[i % L] = p[(i - 3) % L] + p[(i - 2) % L] + p[(i - 1) % L]; + let mut p: Vec = vec![0, 1, 1]; + let p_len = p.len(); + for i in p.len()..=runlength { + p[i % p_len] = p[(i - 3) % p_len] + p[(i - 2) % p_len] + p[(i - 1) % p_len]; } - p[runlength % L] + p[runlength % p_len] } fn count_paths(diffs: &[i64]) -> usize { diff --git a/src/day11.rs b/src/day11.rs index 4f0e961..c42ef14 100644 --- a/src/day11.rs +++ b/src/day11.rs @@ -12,7 +12,7 @@ enum Location { // compute the 8 immediately adjacent coordinates which contian chairs fn adjacent_coords( - map: &[Vec], + seat_map: &[Vec], pos: (usize, usize), max: (usize, usize), ) -> Vec<(usize, usize)> { @@ -27,9 +27,9 @@ fn adjacent_coords( let max_row = max.0.min(pos.0 + 1); let max_col = max.1.min(pos.1 + 1); - (min_row..max_row + 1) - .flat_map(|i| (min_col..max_col + 1).map(move |j| (i, j))) - .filter(|p| *p != pos && map[p.0][p.1] != Location::Floor) + (min_row..=max_row) + .flat_map(|i| (min_col..=max_col).map(move |j| (i, j))) + .filter(|p| *p != pos && seat_map[p.0][p.1] != Location::Floor) .collect() } @@ -49,7 +49,7 @@ fn compute_basic_adjacency(map: &[Vec]) -> Vec fn search_direction_for_chair( map: &[Vec], start: (usize, usize), - step: &(i32, i32), + step: (i32, i32), ) -> Option<(usize, usize)> { let mut i = start.0 as i32 + step.0; let mut j = start.1 as i32 + step.1; @@ -80,7 +80,7 @@ fn compute_advanced_adjacency(map: &[Vec]) -> Vec (), Location::Unoccupied => { if !adj[row][col] diff --git a/src/day12.rs b/src/day12.rs index aeb5872..80b4fbf 100644 --- a/src/day12.rs +++ b/src/day12.rs @@ -17,11 +17,10 @@ fn navigate(data: &str) -> usize { "E" => pos_e += dist, "W" => pos_e -= dist, "F" => match heading { - 0 => pos_n += dist, + 0 | 360 => pos_n += dist, 90 => pos_e += dist, 180 => pos_n -= dist, 270 => pos_e -= dist, - 360 => pos_n += dist, _ => panic!("non cardinal heading!"), }, "L" => heading = (heading + 360 - dist) % 360, diff --git a/src/day13.rs b/src/day13.rs index e13bbba..05ec07f 100644 --- a/src/day13.rs +++ b/src/day13.rs @@ -37,6 +37,7 @@ fn mins_to_next_departure(dep_time: usize, bus: &BusRoute) -> usize { // the Chinese Remainder Theorem. // // code from https://rosettacode.org/wiki/Chinese_remainder_theorem#Rust +#[allow(clippy::many_single_char_names)] fn egcd(a: i64, b: i64) -> (i64, i64, i64) { if a == 0 { (b, 0, 1) diff --git a/src/day14.rs b/src/day14.rs index 8563fe0..67ba10e 100644 --- a/src/day14.rs +++ b/src/day14.rs @@ -42,7 +42,7 @@ fn parse_write(s: &str) -> Instruction { fn parse(input: &str) -> Vec { input .lines() - .map(|l| l.trim()) + .map(str::trim) .map(|l| match &l[0..4] { "mask" => parse_mask(l), "mem[" => parse_write(l), @@ -74,7 +74,6 @@ fn simulate1(program: &[Instruction]) -> u64 { memory.values().sum() } -#[inline(always)] fn set_bit(n: u64, bit: usize, value: bool) -> u64 { if value { n | (1 << bit) diff --git a/src/day15.rs b/src/day15.rs index d6b0361..989761a 100644 --- a/src/day15.rs +++ b/src/day15.rs @@ -33,7 +33,7 @@ pub fn run() -> Result<(i64, i64)> { let answers = utils::read_i64s("data/output-15.txt")?; let p1 = play_speaking_game(&data, 2020); - let p2 = play_speaking_game(&data, 30000000); + let p2 = play_speaking_game(&data, 30_000_000); assert_eq!(p1, answers[0]); assert_eq!(p2, answers[1]); diff --git a/src/day16.rs b/src/day16.rs index ba66367..013b54a 100644 --- a/src/day16.rs +++ b/src/day16.rs @@ -41,7 +41,7 @@ fn parse_fields(input: &str) -> Result> { } fn parse_tickets(input: &str) -> Result<(Vec, Vec>)> { - let mut lines = input.lines().map(|l| l.trim()).filter(|l| l.contains(',')); + let mut lines = input.lines().map(str::trim).filter(|l| l.contains(',')); let my_ticket: Vec = lines .next() @@ -98,8 +98,8 @@ fn find_field_locations(fields: &[Field], my_ticket: &[i64], tickets: &[Vec let mut matches: HashMap<&str, usize> = HashMap::new(); for _ in 0..my_ticket.len() { // find a matching field - for col_id in 0..viable_fields.len() { - let possible_fields = viable_fields[col_id] + for (col_id, fields_for_column) in viable_fields.iter().enumerate() { + let possible_fields = fields_for_column .iter() .filter(|(k, _)| !matches.contains_key(*k)) .filter(|(_, v)| **v == valid_tickets.len()) diff --git a/src/utils.rs b/src/utils.rs index e5fbfd1..b587c1b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -23,11 +23,6 @@ pub fn parse_usize_in_range(s: &str, min: usize, max: usize) -> Option { } } -#[allow(dead_code)] -pub fn times(n: usize) -> impl Iterator { - (0..n).into_iter() -} - // Lexicographic combinations. // // This algorithm generates all t-combinations of n numbers {0, 1, ..., n-1},