From e7712b210e6f57dad3ec2170862538c4ee3b118e Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Sun, 8 Dec 2024 22:45:54 +0200 Subject: [PATCH] chore: clippy, because i can't just keep ignoring it --- src/day2/mod.rs | 16 +++++++------- src/day3/mod.rs | 54 ++++++++++++++++++++++------------------------- src/day4/mod.rs | 4 ++-- src/day6/mod.rs | 34 ++++++++++------------------- src/day8/part1.rs | 2 +- src/day8/part2.rs | 2 +- src/main.rs | 4 ---- 7 files changed, 48 insertions(+), 68 deletions(-) diff --git a/src/day2/mod.rs b/src/day2/mod.rs index 0b895c9..c50d9cc 100644 --- a/src/day2/mod.rs +++ b/src/day2/mod.rs @@ -36,10 +36,10 @@ fn part_one(input: &Path) -> anyhow::Result { let last = last_level.unwrap(); let diff = (current - last).abs(); if diff < 4 && diff > 0 { - if current > last { - safes += 1; - } else if current < last { - safes -= 1; + match current.cmp(last) { + std::cmp::Ordering::Greater => safes += 1, + std::cmp::Ordering::Less => safes -= 1, + _ => {} } if safes.abs() == (r.len() as i32) - 1 { s = 1; @@ -95,10 +95,10 @@ fn is_safe_two(r: Vec) -> i32 { let last = last_level.unwrap(); let diff = (current - last).abs(); if diff < 4 && diff > 0 { - if current > last { - safes += 1; - } else if current < last { - safes -= 1; + match current.cmp(last) { + std::cmp::Ordering::Greater => safes += 1, + std::cmp::Ordering::Less => safes -= 1, + _ => {} } if safes.abs() == (r.len() as i32) - 1 { s = 1; diff --git a/src/day3/mod.rs b/src/day3/mod.rs index 04b6a22..97aca15 100644 --- a/src/day3/mod.rs +++ b/src/day3/mod.rs @@ -21,37 +21,33 @@ fn part_one_orig(input: &Path) -> anyhow::Result { reader.read_to_string(&mut buf)?; let mut answer = 0; - loop { - if let Some(mul_beg) = buf.find("mul(") { - if let Some(mul_end) = buf[mul_beg..].find(")").map(|i| i + mul_beg) { - let mul_input = &buf.clone()[mul_beg + 4..mul_end]; - let mut mul_inputs = mul_input.split(","); - - let mut err = false; - let first: i32 = match mul_inputs.next().unwrap_or_default().parse() { - Ok(f) => f, - Err(_) => { - err = true; - 0 - } - }; - let second: i32 = match mul_inputs.next().unwrap_or_default().parse() { - Ok(f) => f, - Err(_) => { - err = true; - 0 - } - }; - - if err { - buf.replace_range(mul_beg..mul_beg + 4, ""); - } else { - answer += first * second; - buf.replace_range(mul_beg..mul_end + 1, ""); + while let Some(mul_beg) = buf.find("mul(") { + if let Some(mul_end) = buf[mul_beg..].find(")").map(|i| i + mul_beg) { + let mul_input = &buf.clone()[mul_beg + 4..mul_end]; + let mut mul_inputs = mul_input.split(","); + + let mut err = false; + let first: i32 = match mul_inputs.next().unwrap_or_default().parse() { + Ok(f) => f, + Err(_) => { + err = true; + 0 } + }; + let second: i32 = match mul_inputs.next().unwrap_or_default().parse() { + Ok(f) => f, + Err(_) => { + err = true; + 0 + } + }; + + if err { + buf.replace_range(mul_beg..mul_beg + 4, ""); + } else { + answer += first * second; + buf.replace_range(mul_beg..mul_end + 1, ""); } - } else { - break; } } diff --git a/src/day4/mod.rs b/src/day4/mod.rs index 42799da..102ab0b 100644 --- a/src/day4/mod.rs +++ b/src/day4/mod.rs @@ -33,8 +33,8 @@ fn part_one(input: &Path) -> anyhow::Result { for y in 0..input[0].len() { let mut col = vec![]; - for x in 0..input.len() { - col.push(input[x][y]); + for c in &input { + col.push(c[y]); } answer += String::from_iter(col) .as_bytes() diff --git a/src/day6/mod.rs b/src/day6/mod.rs index 2ecc27b..73e2d84 100644 --- a/src/day6/mod.rs +++ b/src/day6/mod.rs @@ -94,23 +94,15 @@ fn part_one(input: &Path) -> anyhow::Result { } let mut guard = guard.unwrap(); - loop { - //print_map(&map); - if let Some(in_front) = get_in_front(&guard, &mut map) { - if in_front.obstacle { - guard.turn(); - } else { - in_front.visited = true; - guard.move_forward(); - } + while let Some(in_front) = get_in_front(&guard, &mut map) { + if in_front.obstacle { + guard.turn(); } else { - break; + in_front.visited = true; + guard.move_forward(); } - //std::thread::sleep(std::time::Duration::from_millis(50)); } - //print_map(&map); - Ok(map.iter().flatten().filter(|p| p.visited).count() as i32) } @@ -143,7 +135,7 @@ fn get_in_front<'a>(guard: &'a Guard, map: &'a mut [Vec]) -> Option<&'a mut } } -fn print_map(map: &[Vec]) { +fn _print_map(map: &[Vec]) { for row in map { for pos in row { if pos.obstacle { @@ -204,16 +196,12 @@ fn part_two(input: &Path) -> anyhow::Result { let mut modified_map = map.clone(); modified_map[pos.y][pos.x].obstacle = true; let start_time = std::time::Instant::now(); - loop { - if let Some(in_front) = get_in_front(&guard, &mut modified_map) { - if in_front.obstacle { - guard.turn(); - } else { - in_front.visited = true; - guard.move_forward(); - } + while let Some(in_front) = get_in_front(&guard, &mut modified_map) { + if in_front.obstacle { + guard.turn(); } else { - break; + in_front.visited = true; + guard.move_forward(); } if start_time.elapsed().as_millis() >= 1 { diff --git a/src/day8/part1.rs b/src/day8/part1.rs index 1f580a3..1809e99 100644 --- a/src/day8/part1.rs +++ b/src/day8/part1.rs @@ -10,7 +10,7 @@ struct Map { } impl Map { - fn print(&self) { + fn _print(&self) { for row in &self.tiles { for tile in row { match tile.tile_type { diff --git a/src/day8/part2.rs b/src/day8/part2.rs index eed964b..fddefd2 100644 --- a/src/day8/part2.rs +++ b/src/day8/part2.rs @@ -10,7 +10,7 @@ struct Map { } impl Map { - fn print(&self) { + fn _print(&self) { for row in &self.tiles { for tile in row { match tile.tile_type { diff --git a/src/main.rs b/src/main.rs index 15b5d16..62cd429 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,3 @@ -#![allow(clippy::comparison_chain)] -#![allow(clippy::while_let_loop)] -#![allow(clippy::needless_range_loop)] -#![allow(dead_code)] use std::path::PathBuf; use clap::{Parser, Subcommand}; -- 2.44.1