From 636fd0c9c63c8548a3b4e1148f5fbd0fc38b50e8 Mon Sep 17 00:00:00 2001 From: Russell Champoux Date: Tue, 3 Dec 2024 22:06:06 +0000 Subject: [PATCH] make clippy happy --- src/bin/01.rs | 11 +++++------ src/bin/02.rs | 24 +++++++++++------------- src/bin/03.rs | 12 ++++++------ 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/bin/01.rs b/src/bin/01.rs index 0006e99..ad8a87a 100644 --- a/src/bin/01.rs +++ b/src/bin/01.rs @@ -22,7 +22,7 @@ pub fn part_one(input: &str) -> Option { // println!("Line {}: abs({} - {}) = {}", i+1, vec_a[i], vec_b[i], diff) } // println!("Total {result}"); - return Some(result); + Some(result) } pub fn part_two(input: &str) -> Option { @@ -41,16 +41,15 @@ pub fn part_two(input: &str) -> Option { vec_a.sort(); vec_b.sort(); - for i in 0..vec_a.len() { - let needle = vec_a[i]; - let found = vec_b.iter().filter(|&&b| b == needle).count() as u32; - let similarity = found * needle; + for item in &vec_a { + let found = vec_b.iter().filter(|&b| b == item).count() as u32; + let similarity = found * item; result += similarity; // println!("Line {}: {} found {} times = {}, total {}", i + 1, needle, found, similarity, result); } // println!("Total {result}"); - return Some(result); + Some(result) } #[cfg(test)] diff --git a/src/bin/02.rs b/src/bin/02.rs index af5b20c..5d74774 100644 --- a/src/bin/02.rs +++ b/src/bin/02.rs @@ -7,10 +7,10 @@ pub fn part_one(input: &str) -> Option { let values: Vec<_> = line.split(" ").map(|f| f.parse::().unwrap()).collect(); let is_valid = is_valid_report(&values); // println!("Report {} {} valid", line, if is_valid { "is" } else { "is NOT" }); - if is_valid { return total + 1 } else { return total }; + if is_valid { total + 1 } else { total } }); - return Some(valid_reports); + Some(valid_reports) } pub fn part_two(input: &str) -> Option { @@ -21,10 +21,10 @@ pub fn part_two(input: &str) -> Option { let is_valid = is_any_valid_report(&values); // println!("Report {} {} valid", line, if is_valid { "is" } else { "is NOT" }); - if is_valid { return total + 1 } else { return total }; + if is_valid { total + 1 } else { total } }); - return Some(valid_reports); + Some(valid_reports) } enum Direction { @@ -33,7 +33,7 @@ enum Direction { Decreasing, } -fn is_valid_report(values: &Vec) -> bool { +fn is_valid_report(values: &[u32]) -> bool { let mut direction = Direction::Unknown; // println!("Checking values {:?}", values); let mut last_report = *values.first().unwrap(); @@ -66,10 +66,10 @@ fn is_valid_report(values: &Vec) -> bool { last_report = report; } - return true; + true } -fn is_any_valid_report(values: &Vec) -> bool { +fn is_any_valid_report(values: &[u32]) -> bool { if is_valid_report(values) { return true; } @@ -80,20 +80,18 @@ fn is_any_valid_report(values: &Vec) -> bool { } } - return false; + false } -fn copy_except(values: &Vec, exclude_idx: usize) -> Vec { +fn copy_except(values: &[u32], exclude_idx: usize) -> Vec { let mut new_values: Vec = Vec::new(); - let mut idx = 0; - for value in values { + for (idx, value) in values.iter().enumerate() { if idx != exclude_idx { new_values.push(*value); } - idx += 1; } // println!("copied new values {:?}", new_values); - return new_values; + new_values } #[cfg(test)] diff --git a/src/bin/03.rs b/src/bin/03.rs index 33e75d3..bcbc82e 100644 --- a/src/bin/03.rs +++ b/src/bin/03.rs @@ -11,10 +11,10 @@ pub fn part_one(input: &str) -> Option { let b = b_str.parse::().unwrap(); let product = a * b; - return total + product; + total + product }); - return Some(result); + Some(result) } pub fn part_two(input: &str) -> Option { @@ -25,21 +25,21 @@ pub fn part_two(input: &str) -> Option { // println!("Captures: {command} ({a_str}, {b_str})"); match command { - "do" => return (true, total), - "don't" => return (false, total), + "do" => (true, total), + "don't" => (false, total), "mul" => { if !enabled { return (enabled, total) }; let a = a_str.parse::().unwrap(); let b = b_str.parse::().unwrap(); let product = a * b; - return (enabled, total + product) + (enabled, total + product) }, _ => panic!("Could not match command {command}"), } }); - return Some(result); + Some(result) } #[cfg(test)]