make clippy happy

This commit is contained in:
RussellChamp committed 2024-12-03 22:06:06 +00:00
1 parent a5cec39ee1
commit 636fd0c9c6
3 files changed
+22 -25

No files matched your search

+5 -6
View File
@@ -22,7 +22,7 @@ pub fn part_one(input: &str) -> Option<u32> {
// println!("Line {}: abs({} - {}) = {}", i+1, vec_a[i], vec_b[i], diff) // println!("Line {}: abs({} - {}) = {}", i+1, vec_a[i], vec_b[i], diff)
} }
// println!("Total {result}"); // println!("Total {result}");
return Some(result); Some(result)
} }
pub fn part_two(input: &str) -> Option<u32> { pub fn part_two(input: &str) -> Option<u32> {
@@ -41,16 +41,15 @@ pub fn part_two(input: &str) -> Option<u32> {
vec_a.sort(); vec_a.sort();
vec_b.sort(); vec_b.sort();
for i in 0..vec_a.len() { for item in &vec_a {
let needle = vec_a[i]; let found = vec_b.iter().filter(|&b| b == item).count() as u32;
let found = vec_b.iter().filter(|&&b| b == needle).count() as u32; let similarity = found * item;
let similarity = found * needle;
result += similarity; result += similarity;
// println!("Line {}: {} found {} times = {}, total {}", i + 1, needle, found, similarity, result); // println!("Line {}: {} found {} times = {}, total {}", i + 1, needle, found, similarity, result);
} }
// println!("Total {result}"); // println!("Total {result}");
return Some(result); Some(result)
} }
#[cfg(test)] #[cfg(test)]
+11 -13
View File
@@ -7,10 +7,10 @@ pub fn part_one(input: &str) -> Option<u32> {
let values: Vec<_> = line.split(" ").map(|f| f.parse::<u32>().unwrap()).collect(); let values: Vec<_> = line.split(" ").map(|f| f.parse::<u32>().unwrap()).collect();
let is_valid = is_valid_report(&values); let is_valid = is_valid_report(&values);
// println!("Report {} {} valid", line, if is_valid { "is" } else { "is NOT" }); // 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<u32> { pub fn part_two(input: &str) -> Option<u32> {
@@ -21,10 +21,10 @@ pub fn part_two(input: &str) -> Option<u32> {
let is_valid = is_any_valid_report(&values); let is_valid = is_any_valid_report(&values);
// println!("Report {} {} valid", line, if is_valid { "is" } else { "is NOT" }); // 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 { enum Direction {
@@ -33,7 +33,7 @@ enum Direction {
Decreasing, Decreasing,
} }
fn is_valid_report(values: &Vec<u32>) -> bool { fn is_valid_report(values: &[u32]) -> bool {
let mut direction = Direction::Unknown; let mut direction = Direction::Unknown;
// println!("Checking values {:?}", values); // println!("Checking values {:?}", values);
let mut last_report = *values.first().unwrap(); let mut last_report = *values.first().unwrap();
@@ -66,10 +66,10 @@ fn is_valid_report(values: &Vec<u32>) -> bool {
last_report = report; last_report = report;
} }
return true; true
} }
fn is_any_valid_report(values: &Vec<u32>) -> bool { fn is_any_valid_report(values: &[u32]) -> bool {
if is_valid_report(values) { if is_valid_report(values) {
return true; return true;
} }
@@ -80,20 +80,18 @@ fn is_any_valid_report(values: &Vec<u32>) -> bool {
} }
} }
return false; false
} }
fn copy_except(values: &Vec<u32>, exclude_idx: usize) -> Vec<u32> { fn copy_except(values: &[u32], exclude_idx: usize) -> Vec<u32> {
let mut new_values: Vec<u32> = Vec::new(); let mut new_values: Vec<u32> = Vec::new();
let mut idx = 0; for (idx, value) in values.iter().enumerate() {
for value in values {
if idx != exclude_idx { if idx != exclude_idx {
new_values.push(*value); new_values.push(*value);
} }
idx += 1;
} }
// println!("copied new values {:?}", new_values); // println!("copied new values {:?}", new_values);
return new_values; new_values
} }
#[cfg(test)] #[cfg(test)]
+6 -6
View File
@@ -11,10 +11,10 @@ pub fn part_one(input: &str) -> Option<u32> {
let b = b_str.parse::<u32>().unwrap(); let b = b_str.parse::<u32>().unwrap();
let product = a * b; let product = a * b;
return total + product; total + product
}); });
return Some(result); Some(result)
} }
pub fn part_two(input: &str) -> Option<u32> { pub fn part_two(input: &str) -> Option<u32> {
@@ -25,21 +25,21 @@ pub fn part_two(input: &str) -> Option<u32> {
// println!("Captures: {command} ({a_str}, {b_str})"); // println!("Captures: {command} ({a_str}, {b_str})");
match command { match command {
"do" => return (true, total), "do" => (true, total),
"don't" => return (false, total), "don't" => (false, total),
"mul" => { "mul" => {
if !enabled { return (enabled, total) }; if !enabled { return (enabled, total) };
let a = a_str.parse::<u32>().unwrap(); let a = a_str.parse::<u32>().unwrap();
let b = b_str.parse::<u32>().unwrap(); let b = b_str.parse::<u32>().unwrap();
let product = a * b; let product = a * b;
return (enabled, total + product) (enabled, total + product)
}, },
_ => panic!("Could not match command {command}"), _ => panic!("Could not match command {command}"),
} }
}); });
return Some(result); Some(result)
} }
#[cfg(test)] #[cfg(test)]