diff --git a/src/solutions/day02.rs b/src/solutions/day02.rs index 2813bd3..8aadb40 100644 --- a/src/solutions/day02.rs +++ b/src/solutions/day02.rs @@ -95,7 +95,7 @@ impl Day02 { // Get the numbers correlating to a given colour and combine let nums: Vec = re - .find_iter(&input) + .find_iter(input) .map(|c| c.unwrap().as_str().parse::().unwrap_or(0)) .collect(); Ok(nums) diff --git a/src/solutions/day03.rs b/src/solutions/day03.rs index 7ef8707..633d4b5 100644 --- a/src/solutions/day03.rs +++ b/src/solutions/day03.rs @@ -16,7 +16,7 @@ impl Solution for Day03 { // Get the valid numbers from the input self.map_valid_nums(&lines, &mut symbols)?; - let ans: usize = symbols.values().flat_map(|v| v).sum(); + let ans: usize = symbols.values().flatten().sum(); Ok(Box::new(ans)) } @@ -89,7 +89,7 @@ impl Day03 { let num = line[start..c] .iter() .fold(0, |n, c| n * 10 + (c - b'0') as usize); - symbols.entry(symbol).or_insert(Vec::new()).push(num) + symbols.entry(symbol).or_default().push(num) } } c += 1; diff --git a/src/solutions/day04.rs b/src/solutions/day04.rs index 4cffd0a..efee037 100644 --- a/src/solutions/day04.rs +++ b/src/solutions/day04.rs @@ -12,7 +12,7 @@ impl Solution for Day04 { // Remove Card XXX: at start let cards: Vec<&str> = input .iter() - .map(|c| c.split(":").into_iter().last().unwrap()) + .map(|c| c.split(':').last().unwrap()) .collect(); let mut ans = 0; @@ -32,8 +32,7 @@ impl Solution for Day04 { // Remove Card XXX: at start and append card index let cards: Vec<(usize, &str)> = input .iter() - .map(|c| c.split(":").into_iter().last().unwrap()) - .into_iter() + .map(|c| c.split(':').last().unwrap()) .enumerate() .collect(); diff --git a/src/solutions/day06.rs b/src/solutions/day06.rs index 5d6753f..226dc47 100644 --- a/src/solutions/day06.rs +++ b/src/solutions/day06.rs @@ -27,17 +27,17 @@ impl Solution for Day06 { input: &mut Vec, ) -> Result, Box> { let times = vec![input[0] - .split(":") + .split(':') .nth(1) .unwrap() - .replace(" ", "") + .replace(' ', "") .parse::()?]; let distances = vec![input[1] - .split(":") + .split(':') .nth(1) .unwrap() - .replace(" ", "") + .replace(' ', "") .parse::()?]; Ok(Box::new(self.num_winning_races(×, &distances)?)) @@ -66,7 +66,7 @@ impl Day06 { beats.push(count); } - Ok(beats.iter().fold(1, |acc, &b| acc * b)) + Ok(beats.iter().product::()) } } diff --git a/src/solutions/day07.rs b/src/solutions/day07.rs index 8b72cb2..36e3e17 100644 --- a/src/solutions/day07.rs +++ b/src/solutions/day07.rs @@ -31,7 +31,7 @@ impl Solution for Day07 { if a.2 == b.2 { a.0.partial_cmp(&b.0).unwrap() } else { - (a.2 as u32).cmp(&(b.2 as u32)) + a.2.cmp(&{ b.2 }) } }); @@ -62,7 +62,7 @@ impl Solution for Day07 { if a.2 == b.2 { a.0.partial_cmp(&b.0).unwrap() } else { - (a.2 as u32).cmp(&(b.2 as u32)) + a.2.cmp(&{ b.2 }) } }); @@ -122,7 +122,7 @@ impl Day07 { map_cards.entry(c).and_modify(|c| *c += 1).or_insert(1u32); } - let mut counts: Vec<&u32> = map_cards.values().into_iter().collect(); + let mut counts: Vec<&u32> = map_cards.values().collect(); counts.sort(); // If there is a single key in the map we have 5OAK @@ -186,7 +186,7 @@ impl Day07 { .map(|(number, _)| *number) .collect::>(); - if most_frequenct_cards.len() == 0 { + if most_frequenct_cards.is_empty() { return Ok(input.clone()); } diff --git a/src/solutions/day08.rs b/src/solutions/day08.rs index da037d2..e6fecfa 100644 --- a/src/solutions/day08.rs +++ b/src/solutions/day08.rs @@ -75,11 +75,10 @@ impl Day08 { key, ( l.to_owned() - .replace(",", "") - .replace("(", "") + .replace([',', '('], "") .trim() .to_owned(), - r.trim().to_owned().replace(")", ""), + r.trim().to_owned().replace(')', ""), ), ); } diff --git a/src/solutions/day10.rs b/src/solutions/day10.rs index 20756b2..a3abc98 100644 --- a/src/solutions/day10.rs +++ b/src/solutions/day10.rs @@ -128,7 +128,7 @@ impl Day10 { .bytes() .find_map(|start_tile| { graph[start.0][start.1] = self.connections(start_tile); - self.find_loop(&graph, start) + self.find_loop(graph, start) }) .unwrap(); Ok(pipe_loop) diff --git a/src/solutions/day12.rs b/src/solutions/day12.rs index 9f1dcb1..2c320d0 100644 --- a/src/solutions/day12.rs +++ b/src/solutions/day12.rs @@ -20,7 +20,7 @@ impl Solution for Day12 { .map(|n| n.parse::().unwrap()) .collect::>(); - ans += self.solve(&springs.as_bytes(), None, &nums, &mut HashMap::new()); + ans += self.solve(springs.as_bytes(), None, &nums, &mut HashMap::new()); } Ok(Box::new(ans)) } @@ -30,7 +30,7 @@ impl Solution for Day12 { input: &mut Vec, ) -> Result, Box> { for l in input.iter_mut() { - let (s, n) = l.split_once(" ").unwrap(); + let (s, n) = l.split_once(' ').unwrap(); *l = format!("{s}?{s}?{s}?{s}?{s} {n},{n},{n},{n},{n}\n"); } @@ -45,7 +45,7 @@ impl Solution for Day12 { .map(|n| n.parse::().unwrap()) .collect::>(); - ans += self.solve(&springs.as_bytes(), None, &nums, &mut HashMap::new()); + ans += self.solve(springs.as_bytes(), None, &nums, &mut HashMap::new()); } Ok(Box::new(ans)) } diff --git a/src/solutions/day13.rs b/src/solutions/day13.rs index cc83a8e..1b304da 100644 --- a/src/solutions/day13.rs +++ b/src/solutions/day13.rs @@ -12,7 +12,7 @@ impl Solution for Day13 { let binding = input.clone().iter().join("\n"); let grids = binding .split("\n\n") - .map(|s| s.split("\n").map(|l| l.as_bytes()).collect::>()) + .map(|s| s.split('\n').map(|l| l.as_bytes()).collect::>()) .collect::>(); Ok(Box::new(self.solve(&grids, 0))) @@ -25,7 +25,7 @@ impl Solution for Day13 { let binding = input.clone().iter().join("\n"); let grids = binding .split("\n\n") - .map(|s| s.split("\n").map(|l| l.as_bytes()).collect::>()) + .map(|s| s.split('\n').map(|l| l.as_bytes()).collect::>()) .collect::>(); Ok(Box::new(self.solve(&grids, 1))) diff --git a/src/solutions/day15.rs b/src/solutions/day15.rs index 1beacde..846b38c 100644 --- a/src/solutions/day15.rs +++ b/src/solutions/day15.rs @@ -58,7 +58,7 @@ impl Day15 { for c in input.bytes() { hash += c as u32; hash *= seed; - hash = hash % divisor; + hash %= divisor; } hash @@ -70,11 +70,11 @@ impl Day15 { // '-' Logic or '=' Logic if bytes[bytes.len() - 1] == b'-' { - let label = str::from_utf8(&bytes[..bytes.len() - 1 as usize]).unwrap(); + let label = str::from_utf8(&bytes[..bytes.len() - 1_usize]).unwrap(); let hash = self.hash(label, 17, 256); boxes[hash as usize].retain(|(lens, _)| lens != label); } else { - let mut parts = instruction.split("="); + let mut parts = instruction.split('='); let label = parts.next().unwrap(); let focal_length = parts.next().unwrap().parse::().unwrap(); diff --git a/src/solutions/day16.rs b/src/solutions/day16.rs index 82f10b6..f54c3f9 100644 --- a/src/solutions/day16.rs +++ b/src/solutions/day16.rs @@ -79,7 +79,7 @@ impl Day16 { } // Get the number of 'energised' tiles now that we have finished processing every beam - energised.iter().flat_map(|row| row).filter(|x| x.iter().any(|&b| b)).count() + energised.iter().flatten().filter(|x| x.iter().any(|&b| b)).count() } } diff --git a/src/solutions/day17.rs b/src/solutions/day17.rs index fcdfe47..03d8705 100644 --- a/src/solutions/day17.rs +++ b/src/solutions/day17.rs @@ -52,7 +52,7 @@ impl Solution for Day17 { let mut grid: HashMap<(usize, usize), u32> = HashMap::new(); for (y, row) in input.iter().enumerate() { - for (x, col) in row.chars().into_iter().enumerate() { + for (x, col) in row.chars().enumerate() { grid.insert((x, y), col.to_digit(10).unwrap()); } } @@ -67,7 +67,7 @@ impl Solution for Day17 { let mut grid: HashMap<(usize, usize), u32> = HashMap::new(); for (y, row) in input.iter().enumerate() { - for (x, col) in row.chars().into_iter().enumerate() { + for (x, col) in row.chars().enumerate() { grid.insert((x, y), col.to_digit(10).unwrap()); } } diff --git a/src/utils.rs b/src/utils.rs index 6b4460b..019c19a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -42,9 +42,7 @@ pub fn gcd(first: u64, second: u64) -> u64 { let mut max = first; let mut min = second; if min > max { - let val = max; - max = min; - min = val; + std::mem::swap(&mut max, &mut min); } loop {