]> aoc.elinar.fr Git - aoc_2024/commitdiff
Follow clippy advice
authoralex <>
Tue, 10 Dec 2024 09:47:51 +0000 (10:47 +0100)
committeralex <>
Tue, 10 Dec 2024 09:47:51 +0000 (10:47 +0100)
src/day01.rs
src/day02.rs
src/day03.rs
src/day04.rs
src/day05.rs
src/day06.rs
src/day07.rs
src/day08.rs
src/day09.rs
src/day10.rs
src/template.rs

index cc163894175cdf6b72a485e627eb56f78e39a22d..826d40e46a7ca490426c61b610dc4cf8c7665cff 100644 (file)
@@ -4,10 +4,10 @@ use std::iter::zip;
 use std::collections::HashMap;
 
 pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
-    let res = run_part1(&input)?;
+    let res = run_part1(input)?;
     println!("{res}");
 
-    let res = run_part2(&input)?;
+    let res = run_part2(input)?;
     println!("{res}");
 
     Ok(())
index 6037683a98e3365208375a469b6bbd92650776ec..1aec8c3d5f773281c6aca07c1e26a315bd1067f5 100644 (file)
@@ -2,10 +2,10 @@ use std::error::Error;
 use std::path::Path;
 
 pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
-    let res = run_part1(&input)?;
+    let res = run_part1(input)?;
     println!("{res}");
 
-    let res = run_part2(&input)?;
+    let res = run_part2(input)?;
     println!("{res}");
 
     Ok(())
index 51bf91d6e1691bf321dc9c2b85a738c330a853b6..3beb841f4318c4835cdbba526ef21a517011b7e0 100644 (file)
@@ -3,10 +3,10 @@ use std::path::Path;
 use regex::Regex;
 
 pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
-    let res = run_part1(&input)?;
+    let res = run_part1(input)?;
     println!("{res}");
 
-    let res = run_part2(&input)?;
+    let res = run_part2(input)?;
     println!("{res}");
 
     Ok(())
index f68472f72ac47b6d83b19ee17a943b8c6342648a..887fc0016e3c8290e758c518b3670559d2186f29 100644 (file)
@@ -2,10 +2,10 @@ use std::error::Error;
 use std::path::Path;
 
 pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
-    let res = run_part1(&input)?;
+    let res = run_part1(input)?;
     println!("{res}");
 
-    let res = run_part2(&input)?;
+    let res = run_part2(input)?;
     println!("{res}");
 
     Ok(())
@@ -86,7 +86,7 @@ fn run_part1(input: &str) -> Result<u32, Box<dyn Error>> {
 fn run_part2(input: &str) -> Result<u32, Box<dyn Error>> {
     println!("Running {} - part 2", get_day());
 
-    fn is_mas(coord: (i32, i32), dir: ((i32, i32), (i32, i32)), map: &Vec<Vec<char>>) -> bool {
+    fn is_mas(coord: (i32, i32), dir: ((i32, i32), (i32, i32)), map: &[Vec<char>]) -> bool {
         let (r, c) = coord;
         (
             map[(r + dir.0.0) as usize][(c + dir.0.1) as usize] == 'M' &&
index 0133eb0e72539cc8fb9d3ea08cdffca33765d46e..cbb0ebc2b17f7b71c5c198766d423c4b3bbf10e3 100644 (file)
@@ -34,11 +34,11 @@ impl Puzzle {
         }
     }
 
-    pub fn is_ordered(self: &Self, line: &[u32]) -> bool {
+    pub fn is_ordered(&self, line: &[u32]) -> bool {
         for i in 1..line.len() {
             let v = &line[i];
             for before in &line[0..i] {
-                if self.order.get(v).is_some() && self.order.get(v).unwrap().contains(before) {
+                if self.order.contains_key(v) && self.order.get(v).unwrap().contains(before) {
                     return false
                 }
             }
@@ -73,10 +73,8 @@ fn run_part2(input: &str) -> Result<u32, Box<dyn Error>> {
                 for i in 1..pages.len() {
                     let v = pages[i];
                     for j in 0..i {
-                        if puzzle.order.get(&v).is_some() && puzzle.order.get(&v).unwrap().contains(&pages[j]) {
-                            let tmp = pages[j];
-                            pages[j] = pages[i];
-                            pages[i] = tmp;
+                        if puzzle.order.contains_key(&v) && puzzle.order.get(&v).unwrap().contains(&pages[j]) {
+                            pages.swap(i, j);
                         }
                     }
                 }
@@ -89,10 +87,10 @@ fn run_part2(input: &str) -> Result<u32, Box<dyn Error>> {
 }
 
 pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
-    let res = run_part1(&input)?;
+    let res = run_part1(input)?;
     println!("{res}");
 
-    let res = run_part2(&input)?;
+    let res = run_part2(input)?;
     println!("{res}");
 
     Ok(())
index f6431f6f40cff5a4e8f49186b367e51ac162e701..53a49f28270c1a0a22dca8acd9cd2255daefa43d 100644 (file)
@@ -32,7 +32,7 @@ impl Puzzle {
         }
     }
 
-    fn guard_path(self: &mut Self) {
+    fn guard_path(&mut self) {
         let mut turn_right: HashMap<(isize, isize), (isize, isize)> = HashMap::new();
         turn_right.insert((-1,0), (0,1));
         turn_right.insert((0,1), (1,0));
@@ -54,7 +54,7 @@ impl Puzzle {
         }
     }
 
-    fn is_guard_loop(self: &Self) -> bool {
+    fn is_guard_loop(&self) -> bool {
         let mut turn_right: HashMap<(isize, isize), (isize, isize)> = HashMap::new();
         turn_right.insert((-1,0), (0,1));
         turn_right.insert((0,1), (1,0));
@@ -89,10 +89,10 @@ impl Puzzle {
 }
 
 pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
-    let res = run_part1(&input)?;
+    let res = run_part1(input)?;
     println!("{res}");
 
-    let res = run_part2(&input)?;
+    let res = run_part2(input)?;
     println!("{res}");
 
     Ok(())
index ae83c265f018bdd8d83478c921a065dcd6a7f5f2..c8d32d503c0157d9bf619e5d9e424e8ac9d749fb 100644 (file)
@@ -57,10 +57,10 @@ fn run_part2(input: &str) -> Result<u64, Box<dyn Error>> {
 }
 
 pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
-    let res = run_part1(&input)?;
+    let res = run_part1(input)?;
     println!("{res}");
 
-    let res = run_part2(&input)?;
+    let res = run_part2(input)?;
     println!("{res}");
 
     Ok(())
index d9f976e372b3aa1b800aa217543c3159eeadae62..efa68e492c3685e56e255b7ff772d63dda11dd7a 100644 (file)
@@ -34,7 +34,7 @@ fn solve(puzzle: &Puzzle, depth: &[isize]) -> u32 {
 
                 puzzle.map.iter()
                     .filter(|(k, v)| *v == value && *k != key)
-                    .for_each(|(k, v)| {
+                    .for_each(|(k, _)| {
                         let (x1, x2) = if k <= key { (k, key) } else { (key, k) };
                         let d = (x1.0 - x2.0, x1.1 - x2.1);
                         for i in depth {
@@ -68,10 +68,10 @@ fn run_part2(input: &str) -> Result<u32, Box<dyn Error>> {
 }
 
 pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
-    let res = run_part1(&input)?;
+    let res = run_part1(input)?;
     println!("{res}");
 
-    let res = run_part2(&input)?;
+    let res = run_part2(input)?;
     println!("{res}");
 
     Ok(())
index ac087a3697754c7b4659b0806c3cf07120ae7dd1..6b0486631d1ac03e77bfa348caf38569aec821d7 100644 (file)
@@ -29,7 +29,7 @@ impl Puzzle {
         Self { blocks, free }
     }
 
-    fn fill_free(self: &mut Self, free_idx: usize, last_block: usize) -> usize {
+    fn fill_free(&mut self, free_idx: usize, last_block: usize) -> usize {
         let (free_start, free_length) = self.free[free_idx];
         if free_length == 0 {
             return last_block;
@@ -49,14 +49,14 @@ impl Puzzle {
         block_idx
     }
 
-    fn first_free_idx(self: &Self, size: u64) -> Option<usize> {
+    fn first_free_idx(&self, size: u64) -> Option<usize> {
         self.free.iter().enumerate()
             .filter(|(_, (_, length))| *length >= size)
             .map(|(i, _)| i)
             .nth(0)
     }
 
-    fn can_compact(self: &Self) -> bool {
+    fn can_compact(&self) -> bool {
         let last_block = self.blocks.last().unwrap();
         let last_block_end = last_block.0 + last_block.1 - 1;
         self.free.iter()
@@ -64,7 +64,7 @@ impl Puzzle {
             .any(|(start, _)| *start < last_block_end)
     }
 
-    fn compact(self: &mut Self) {
+    fn compact(&mut self) {
         let mut block_idx = 0;
         while self.can_compact() {
             let first_free = self.first_free_idx(1);
@@ -78,24 +78,21 @@ impl Puzzle {
         }
     }
 
-    fn compact_by_size(self: &mut Self) {
+    fn compact_by_size(&mut self) {
         for i in (0..self.blocks.len()).rev() {
-            match self.first_free_idx(self.blocks[i].1) {
-                Some(first_free_idx) => {
-                    let first_free = self.free.get_mut(first_free_idx).unwrap();
-                    let block = self.blocks.get_mut(i).unwrap();
-                    if block.0 > first_free.0 {
-                        block.0 = first_free.0;
-                        first_free.0 += block.1;
-                        first_free.1 -= block.1;
-                    }
-                },
-                _ => {}
+            if let Some(first_free_idx) = self.first_free_idx(self.blocks[i].1) {
+                let first_free = self.free.get_mut(first_free_idx).unwrap();
+                let block = self.blocks.get_mut(i).unwrap();
+                if block.0 > first_free.0 {
+                    block.0 = first_free.0;
+                    first_free.0 += block.1;
+                    first_free.1 -= block.1;
+                }
             }
         }
     }
 
-    fn checksum(self: &Self) -> u64 {
+    fn checksum(&self) -> u64 {
         self.blocks.iter()
             .map(|&(start, length, value)| {
                 (start..(start+length)).map(|i| i*value).sum::<u64>()
@@ -121,10 +118,10 @@ fn run_part2(input: &str) -> Result<u64, Box<dyn Error>> {
 }
 
 pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
-    let res = run_part1(&input)?;
+    let res = run_part1(input)?;
     println!("{res}");
 
-    let res = run_part2(&input)?;
+    let res = run_part2(input)?;
     println!("{res}");
 
     Ok(())
index 2780f42fb2099ec376c064ae438737842ec0f953..f3348660fb73ef480335bdefd37bab00ae744e84 100644 (file)
@@ -13,7 +13,7 @@ impl Puzzle {
         let mut start_pos = Vec::new();
         input.lines().enumerate()
             .for_each(|(r, l)| {
-                l.as_bytes().into_iter().enumerate()
+                l.as_bytes().iter().enumerate()
                     .for_each(|(c, b)| {
                         if *b != b'.' {
                             map.insert((r as isize, c as isize), *b);
@@ -26,7 +26,7 @@ impl Puzzle {
         Self { map, start_pos }
     }
 
-    fn score(self: &Self,
+    fn score(&self,
              pos: (isize, isize),
              value: u8,
              visited: &mut Option<HashSet<(isize, isize)>>
@@ -65,7 +65,7 @@ impl Puzzle {
             .sum()
     }
 
-    fn solve(self: &Self, with_rating: bool) -> u32 {
+    fn solve(&self, with_rating: bool) -> u32 {
         self.start_pos.iter()
             .map(|pos| {
                 match with_rating {
@@ -95,10 +95,10 @@ fn run_part2(input: &str) -> Result<u32, Box<dyn Error>> {
 }
 
 pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
-    let res = run_part1(&input)?;
+    let res = run_part1(input)?;
     println!("{res}");
 
-    let res = run_part2(&input)?;
+    let res = run_part2(input)?;
     println!("{res}");
 
     Ok(())
index 9bf5f2469699d20dc576d6fc5d9a55502e99eb3b..8dee1e580481777948e976d620d79862ca4fc7aa 100644 (file)
@@ -14,10 +14,10 @@ fn run_part2(input: &str) -> Result<u32, Box<dyn Error>> {
 }
 
 pub fn run(input: &str) -> Result<(), Box<dyn Error>> {
-    let res = run_part1(&input)?;
+    let res = run_part1(input)?;
     println!("{res}");
 
-    let res = run_part2(&input)?;
+    let res = run_part2(input)?;
     println!("{res}");
 
     Ok(())