Applied clippy suggestions

This commit is contained in:
Luke Else 2024-01-12 14:26:05 +00:00
parent 760881cbc3
commit 9609c69c9e
13 changed files with 29 additions and 33 deletions

View File

@ -95,7 +95,7 @@ impl Day02 {
// Get the numbers correlating to a given colour and combine // Get the numbers correlating to a given colour and combine
let nums: Vec<u32> = re let nums: Vec<u32> = re
.find_iter(&input) .find_iter(input)
.map(|c| c.unwrap().as_str().parse::<u32>().unwrap_or(0)) .map(|c| c.unwrap().as_str().parse::<u32>().unwrap_or(0))
.collect(); .collect();
Ok(nums) Ok(nums)

View File

@ -16,7 +16,7 @@ impl Solution for Day03 {
// Get the valid numbers from the input // Get the valid numbers from the input
self.map_valid_nums(&lines, &mut symbols)?; 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)) Ok(Box::new(ans))
} }
@ -89,7 +89,7 @@ impl Day03 {
let num = line[start..c] let num = line[start..c]
.iter() .iter()
.fold(0, |n, c| n * 10 + (c - b'0') as usize); .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; c += 1;

View File

@ -12,7 +12,7 @@ impl Solution for Day04 {
// Remove Card XXX: at start // Remove Card XXX: at start
let cards: Vec<&str> = input let cards: Vec<&str> = input
.iter() .iter()
.map(|c| c.split(":").into_iter().last().unwrap()) .map(|c| c.split(':').last().unwrap())
.collect(); .collect();
let mut ans = 0; let mut ans = 0;
@ -32,8 +32,7 @@ impl Solution for Day04 {
// Remove Card XXX: at start and append card index // Remove Card XXX: at start and append card index
let cards: Vec<(usize, &str)> = input let cards: Vec<(usize, &str)> = input
.iter() .iter()
.map(|c| c.split(":").into_iter().last().unwrap()) .map(|c| c.split(':').last().unwrap())
.into_iter()
.enumerate() .enumerate()
.collect(); .collect();

View File

@ -27,17 +27,17 @@ impl Solution for Day06 {
input: &mut Vec<String>, input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> { ) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
let times = vec![input[0] let times = vec![input[0]
.split(":") .split(':')
.nth(1) .nth(1)
.unwrap() .unwrap()
.replace(" ", "") .replace(' ', "")
.parse::<u64>()?]; .parse::<u64>()?];
let distances = vec![input[1] let distances = vec![input[1]
.split(":") .split(':')
.nth(1) .nth(1)
.unwrap() .unwrap()
.replace(" ", "") .replace(' ', "")
.parse::<u64>()?]; .parse::<u64>()?];
Ok(Box::new(self.num_winning_races(&times, &distances)?)) Ok(Box::new(self.num_winning_races(&times, &distances)?))
@ -66,7 +66,7 @@ impl Day06 {
beats.push(count); beats.push(count);
} }
Ok(beats.iter().fold(1, |acc, &b| acc * b)) Ok(beats.iter().product::<u32>())
} }
} }

View File

@ -31,7 +31,7 @@ impl Solution for Day07 {
if a.2 == b.2 { if a.2 == b.2 {
a.0.partial_cmp(&b.0).unwrap() a.0.partial_cmp(&b.0).unwrap()
} else { } 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 { if a.2 == b.2 {
a.0.partial_cmp(&b.0).unwrap() a.0.partial_cmp(&b.0).unwrap()
} else { } 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); 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(); counts.sort();
// If there is a single key in the map we have 5OAK // If there is a single key in the map we have 5OAK
@ -186,7 +186,7 @@ impl Day07 {
.map(|(number, _)| *number) .map(|(number, _)| *number)
.collect::<Vec<u32>>(); .collect::<Vec<u32>>();
if most_frequenct_cards.len() == 0 { if most_frequenct_cards.is_empty() {
return Ok(input.clone()); return Ok(input.clone());
} }

View File

@ -75,11 +75,10 @@ impl Day08 {
key, key,
( (
l.to_owned() l.to_owned()
.replace(",", "") .replace([',', '('], "")
.replace("(", "")
.trim() .trim()
.to_owned(), .to_owned(),
r.trim().to_owned().replace(")", ""), r.trim().to_owned().replace(')', ""),
), ),
); );
} }

View File

@ -128,7 +128,7 @@ impl Day10 {
.bytes() .bytes()
.find_map(|start_tile| { .find_map(|start_tile| {
graph[start.0][start.1] = self.connections(start_tile); graph[start.0][start.1] = self.connections(start_tile);
self.find_loop(&graph, start) self.find_loop(graph, start)
}) })
.unwrap(); .unwrap();
Ok(pipe_loop) Ok(pipe_loop)

View File

@ -20,7 +20,7 @@ impl Solution for Day12 {
.map(|n| n.parse::<usize>().unwrap()) .map(|n| n.parse::<usize>().unwrap())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
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)) Ok(Box::new(ans))
} }
@ -30,7 +30,7 @@ impl Solution for Day12 {
input: &mut Vec<String>, input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> { ) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
for l in input.iter_mut() { 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"); *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::<usize>().unwrap()) .map(|n| n.parse::<usize>().unwrap())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
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)) Ok(Box::new(ans))
} }

View File

@ -12,7 +12,7 @@ impl Solution for Day13 {
let binding = input.clone().iter().join("\n"); let binding = input.clone().iter().join("\n");
let grids = binding let grids = binding
.split("\n\n") .split("\n\n")
.map(|s| s.split("\n").map(|l| l.as_bytes()).collect::<Vec<_>>()) .map(|s| s.split('\n').map(|l| l.as_bytes()).collect::<Vec<_>>())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
Ok(Box::new(self.solve(&grids, 0))) Ok(Box::new(self.solve(&grids, 0)))
@ -25,7 +25,7 @@ impl Solution for Day13 {
let binding = input.clone().iter().join("\n"); let binding = input.clone().iter().join("\n");
let grids = binding let grids = binding
.split("\n\n") .split("\n\n")
.map(|s| s.split("\n").map(|l| l.as_bytes()).collect::<Vec<_>>()) .map(|s| s.split('\n').map(|l| l.as_bytes()).collect::<Vec<_>>())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
Ok(Box::new(self.solve(&grids, 1))) Ok(Box::new(self.solve(&grids, 1)))

View File

@ -58,7 +58,7 @@ impl Day15 {
for c in input.bytes() { for c in input.bytes() {
hash += c as u32; hash += c as u32;
hash *= seed; hash *= seed;
hash = hash % divisor; hash %= divisor;
} }
hash hash
@ -70,11 +70,11 @@ impl Day15 {
// '-' Logic or '=' Logic // '-' Logic or '=' Logic
if bytes[bytes.len() - 1] == b'-' { 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); let hash = self.hash(label, 17, 256);
boxes[hash as usize].retain(|(lens, _)| lens != label); boxes[hash as usize].retain(|(lens, _)| lens != label);
} else { } else {
let mut parts = instruction.split("="); let mut parts = instruction.split('=');
let label = parts.next().unwrap(); let label = parts.next().unwrap();
let focal_length = parts.next().unwrap().parse::<u32>().unwrap(); let focal_length = parts.next().unwrap().parse::<u32>().unwrap();

View File

@ -79,7 +79,7 @@ impl Day16 {
} }
// Get the number of 'energised' tiles now that we have finished processing every beam // 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()
} }
} }

View File

@ -52,7 +52,7 @@ impl Solution for Day17 {
let mut grid: HashMap<(usize, usize), u32> = HashMap::new(); let mut grid: HashMap<(usize, usize), u32> = HashMap::new();
for (y, row) in input.iter().enumerate() { 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()); 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(); let mut grid: HashMap<(usize, usize), u32> = HashMap::new();
for (y, row) in input.iter().enumerate() { 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()); grid.insert((x, y), col.to_digit(10).unwrap());
} }
} }

View File

@ -42,9 +42,7 @@ pub fn gcd(first: u64, second: u64) -> u64 {
let mut max = first; let mut max = first;
let mut min = second; let mut min = second;
if min > max { if min > max {
let val = max; std::mem::swap(&mut max, &mut min);
max = min;
min = val;
} }
loop { loop {