cleanup many clippy pedantic warnings

Signed-off-by: Stephen Waits <swaits@amazon.com>
This commit is contained in:
Stephen Waits committed 2020-12-16 13:51:35 -08:00
1 parent 2a13ede1c4
commit 39746c982c
11 files changed
+45 -62

No files matched your search

+11 -28
View File
@@ -45,29 +45,23 @@ impl Passport {
// byr (Birth Year) - four digits; at least 1920 and at most 2002. // byr (Birth Year) - four digits; at least 1920 and at most 2002.
fn is_byr_valid(&self) -> bool { 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() utils::parse_usize_in_range(&s, 1920, 2002).is_some()
} else { })
false
}
} }
// iyr (Issue Year) - four digits; at least 2010 and at most 2020. // iyr (Issue Year) - four digits; at least 2010 and at most 2020.
fn is_iyr_valid(&self) -> bool { 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() utils::parse_usize_in_range(&s, 2010, 2020).is_some()
} else { })
false
}
} }
// eyr (Expiration Year) - four digits; at least 2020 and at most 2030. // eyr (Expiration Year) - four digits; at least 2020 and at most 2030.
fn is_eyr_valid(&self) -> bool { 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() utils::parse_usize_in_range(&s, 2020, 2030).is_some()
} else { })
false
}
} }
// hgt (Height) - a number followed by either cm or in: // 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"); Regex::new(r"^#[0-9a-f]{6}$").expect("regex compilation failure");
} }
if let Some(s) = &self.hcl { self.hcl.as_ref().map_or(false, |s| HCL_RE.is_match(s))
HCL_RE.is_match(s)
} else {
false
}
} }
// ecl (Eye Color) - exactly one of: amb blu brn gry grn hzl oth. // 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"); Regex::new(r"^(amb|blu|brn|gry|grn|hzl|oth)$").expect("regex compilation failure");
} }
if let Some(s) = &self.ecl { self.ecl.as_ref().map_or(false, |s| ECL_RE.is_match(s))
ECL_RE.is_match(s)
} else {
false
}
} }
// pid (Passport ID) - a nine-digit number, including leading zeroes. // 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"); Regex::new(r"^[0-9]{9}$").expect("regex compilation failure");
} }
if let Some(s) = &self.pid { self.pid.as_ref().map_or(false, |s| PID_RE.is_match(s))
PID_RE.is_match(s)
} else {
false
}
} }
// cid (Country ID) - ignored, missing or not. // cid (Country ID) - ignored, missing or not.
#[allow(clippy::unused_self)]
fn is_cid_valid(&self) -> bool { fn is_cid_valid(&self) -> bool {
true true
} }
@@ -149,7 +132,7 @@ impl FromStr for Passport {
.expect("regex compilation failure"); .expect("regex compilation failure");
} }
let mut ret: Self = Default::default(); let mut ret: Self = Passport::default();
s.split_whitespace().for_each(|s| { s.split_whitespace().for_each(|s| {
PASSPORT_RE.captures_iter(&s).for_each(|cap| match &cap[1] { PASSPORT_RE.captures_iter(&s).for_each(|cap| match &cap[1] {
"byr" => ret.byr = Some(cap[2].to_string()), "byr" => ret.byr = Some(cap[2].to_string()),
+8 -1
View File
@@ -8,7 +8,14 @@ use std::{
}; };
fn count_uniq_alpha_chars(s: &str) -> usize { fn count_uniq_alpha_chars(s: &str) -> usize {
HashSet::<usize>::from_iter(s.chars().filter(|c| c.is_alphabetic()).map(|c| c as usize)).len() HashSet::<usize>::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 { fn count_common_answers(s: &str) -> usize {
+7 -8
View File
@@ -91,16 +91,16 @@ impl Console {
// execute instruction at pc // execute instruction at pc
self.hits[self.pc] += 1; self.hits[self.pc] += 1;
match self.program[self.pc] { match self.program.get(self.pc).unwrap() {
Instruction::Acc(n) => { Instruction::Acc(n) => {
self.accumulator += n; self.accumulator += n;
self.pc += 1; self.pc += 1;
} }
Instruction::Jmp(n) => { Instruction::Jmp(n) => {
if n < 0 { if *n < 0 {
self.pc -= n.abs() as usize; self.pc -= n.abs() as usize;
} else { } else {
self.pc += n as usize; self.pc += *n as usize;
} }
} }
Instruction::Nop(_) => { Instruction::Nop(_) => {
@@ -127,13 +127,12 @@ impl Console {
.hits .hits
.iter() .iter()
.enumerate() .enumerate()
.filter(|(_, hits)| **hits > 0) // filter only instructions we hit .filter_map(|(i, hits)| if *hits > 0 { Some(i) } else { None })
.map(|(i, _)| i) // map just the index .filter(|i| match self.program.get(*i as usize).unwrap() {
.filter(|i| match self.program[*i as usize] {
// filter instructions we can modify // filter instructions we can modify
Instruction::Jmp(_) => true, Instruction::Jmp(_) => true,
Instruction::Nop(n) => n != 0, Instruction::Nop(n) => *n != 0,
_ => false, Instruction::Acc(_) => false,
}) })
.collect(); .collect();
+5 -5
View File
@@ -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 // 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. // is sum of the paths in N-3, N-2, and N-1.
fn paths_by_runlength(runlength: usize) -> usize { fn paths_by_runlength(runlength: usize) -> usize {
let mut p: Vec<usize> = vec![0, 1, 1]; // vec of most recent 4 in sequence let mut p: Vec<usize> = vec![0, 1, 1];
const L: usize = 3; // length of p Vec let p_len = p.len();
for i in L..runlength + 1 { for i in p.len()..=runlength {
p[i % L] = p[(i - 3) % L] + p[(i - 2) % L] + p[(i - 1) % L]; 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 { fn count_paths(diffs: &[i64]) -> usize {
+7 -7
View File
@@ -12,7 +12,7 @@ enum Location {
// compute the 8 immediately adjacent coordinates which contian chairs // compute the 8 immediately adjacent coordinates which contian chairs
fn adjacent_coords( fn adjacent_coords(
map: &[Vec<Location>], seat_map: &[Vec<Location>],
pos: (usize, usize), pos: (usize, usize),
max: (usize, usize), max: (usize, usize),
) -> Vec<(usize, usize)> { ) -> Vec<(usize, usize)> {
@@ -27,9 +27,9 @@ fn adjacent_coords(
let max_row = max.0.min(pos.0 + 1); let max_row = max.0.min(pos.0 + 1);
let max_col = max.1.min(pos.1 + 1); let max_col = max.1.min(pos.1 + 1);
(min_row..max_row + 1) (min_row..=max_row)
.flat_map(|i| (min_col..max_col + 1).map(move |j| (i, j))) .flat_map(|i| (min_col..=max_col).map(move |j| (i, j)))
.filter(|p| *p != pos && map[p.0][p.1] != Location::Floor) .filter(|p| *p != pos && seat_map[p.0][p.1] != Location::Floor)
.collect() .collect()
} }
@@ -49,7 +49,7 @@ fn compute_basic_adjacency(map: &[Vec<Location>]) -> Vec<Vec<Vec<(usize, usize)>
fn search_direction_for_chair( fn search_direction_for_chair(
map: &[Vec<Location>], map: &[Vec<Location>],
start: (usize, usize), start: (usize, usize),
step: &(i32, i32), step: (i32, i32),
) -> Option<(usize, usize)> { ) -> Option<(usize, usize)> {
let mut i = start.0 as i32 + step.0; let mut i = start.0 as i32 + step.0;
let mut j = start.1 as i32 + step.1; let mut j = start.1 as i32 + step.1;
@@ -80,7 +80,7 @@ fn compute_advanced_adjacency(map: &[Vec<Location>]) -> Vec<Vec<Vec<(usize, usiz
dirs.iter() dirs.iter()
.filter_map(move |d| { .filter_map(move |d| {
let dir = *d; let dir = *d;
search_direction_for_chair(map, (i, j), &dir) search_direction_for_chair(map, (i, j), dir)
}) })
.collect() .collect()
}) })
@@ -99,7 +99,7 @@ fn seating_round(
for row in 0..map.len() { for row in 0..map.len() {
for col in 0..map[row].len() { for col in 0..map[row].len() {
match map[row][col] { match map[row].get(col).unwrap() {
Location::Floor => (), Location::Floor => (),
Location::Unoccupied => { Location::Unoccupied => {
if !adj[row][col] if !adj[row][col]
+1 -2
View File
@@ -17,11 +17,10 @@ fn navigate(data: &str) -> usize {
"E" => pos_e += dist, "E" => pos_e += dist,
"W" => pos_e -= dist, "W" => pos_e -= dist,
"F" => match heading { "F" => match heading {
0 => pos_n += dist, 0 | 360 => pos_n += dist,
90 => pos_e += dist, 90 => pos_e += dist,
180 => pos_n -= dist, 180 => pos_n -= dist,
270 => pos_e -= dist, 270 => pos_e -= dist,
360 => pos_n += dist,
_ => panic!("non cardinal heading!"), _ => panic!("non cardinal heading!"),
}, },
"L" => heading = (heading + 360 - dist) % 360, "L" => heading = (heading + 360 - dist) % 360,
+1
View File
@@ -37,6 +37,7 @@ fn mins_to_next_departure(dep_time: usize, bus: &BusRoute) -> usize {
// the Chinese Remainder Theorem. // the Chinese Remainder Theorem.
// //
// code from https://rosettacode.org/wiki/Chinese_remainder_theorem#Rust // 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) { fn egcd(a: i64, b: i64) -> (i64, i64, i64) {
if a == 0 { if a == 0 {
(b, 0, 1) (b, 0, 1)
+1 -2
View File
@@ -42,7 +42,7 @@ fn parse_write(s: &str) -> Instruction {
fn parse(input: &str) -> Vec<Instruction> { fn parse(input: &str) -> Vec<Instruction> {
input input
.lines() .lines()
.map(|l| l.trim()) .map(str::trim)
.map(|l| match &l[0..4] { .map(|l| match &l[0..4] {
"mask" => parse_mask(l), "mask" => parse_mask(l),
"mem[" => parse_write(l), "mem[" => parse_write(l),
@@ -74,7 +74,6 @@ fn simulate1(program: &[Instruction]) -> u64 {
memory.values().sum() memory.values().sum()
} }
#[inline(always)]
fn set_bit(n: u64, bit: usize, value: bool) -> u64 { fn set_bit(n: u64, bit: usize, value: bool) -> u64 {
if value { if value {
n | (1 << bit) n | (1 << bit)
+1 -1
View File
@@ -33,7 +33,7 @@ pub fn run() -> Result<(i64, i64)> {
let answers = utils::read_i64s("data/output-15.txt")?; let answers = utils::read_i64s("data/output-15.txt")?;
let p1 = play_speaking_game(&data, 2020); 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!(p1, answers[0]);
assert_eq!(p2, answers[1]); assert_eq!(p2, answers[1]);
+3 -3
View File
@@ -41,7 +41,7 @@ fn parse_fields(input: &str) -> Result<Vec<Field>> {
} }
fn parse_tickets(input: &str) -> Result<(Vec<i64>, Vec<Vec<i64>>)> { fn parse_tickets(input: &str) -> Result<(Vec<i64>, Vec<Vec<i64>>)> {
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<i64> = lines let my_ticket: Vec<i64> = lines
.next() .next()
@@ -98,8 +98,8 @@ fn find_field_locations(fields: &[Field], my_ticket: &[i64], tickets: &[Vec<i64>
let mut matches: HashMap<&str, usize> = HashMap::new(); let mut matches: HashMap<&str, usize> = HashMap::new();
for _ in 0..my_ticket.len() { for _ in 0..my_ticket.len() {
// find a matching field // find a matching field
for col_id in 0..viable_fields.len() { for (col_id, fields_for_column) in viable_fields.iter().enumerate() {
let possible_fields = viable_fields[col_id] let possible_fields = fields_for_column
.iter() .iter()
.filter(|(k, _)| !matches.contains_key(*k)) .filter(|(k, _)| !matches.contains_key(*k))
.filter(|(_, v)| **v == valid_tickets.len()) .filter(|(_, v)| **v == valid_tickets.len())
-5
View File
@@ -23,11 +23,6 @@ pub fn parse_usize_in_range(s: &str, min: usize, max: usize) -> Option<usize> {
} }
} }
#[allow(dead_code)]
pub fn times(n: usize) -> impl Iterator<Item = usize> {
(0..n).into_iter()
}
// Lexicographic combinations. // Lexicographic combinations.
// //
// This algorithm generates all t-combinations of n numbers {0, 1, ..., n-1}, // This algorithm generates all t-combinations of n numbers {0, 1, ..., n-1},