Fix clippy warnings

This commit is contained in:
BlackDemonFire 2023-12-05 14:06:27 +01:00
parent a53b8d78d7
commit 1ffe216c51
Signed by: lucy
SSH key fingerprint: SHA256:5NG0Um+vQQHuWkYn+Vd84YFm10edFBJiOIpOuHjl8UE
10 changed files with 60 additions and 73 deletions

View file

@ -5,7 +5,7 @@ fn main() {
.map(|line| { .map(|line| {
let digits = line let digits = line
.chars() .chars()
.filter(|c| c.is_ascii_digit()) .filter(char::is_ascii_digit)
.collect::<Vec<char>>(); .collect::<Vec<char>>();
let mut num_str = digits.first().unwrap().to_string(); let mut num_str = digits.first().unwrap().to_string();
num_str.push(*digits.last().unwrap()); num_str.push(*digits.last().unwrap());

View file

@ -15,7 +15,7 @@ fn main() {
.map(|line| { .map(|line| {
let digits = line let digits = line
.chars() .chars()
.filter(|c| c.is_ascii_digit()) .filter(char::is_ascii_digit)
.collect::<Vec<char>>(); .collect::<Vec<char>>();
let mut num_str = digits.first().unwrap().to_string(); let mut num_str = digits.first().unwrap().to_string();
num_str.push(*digits.last().unwrap()); num_str.push(*digits.last().unwrap());

View file

@ -5,7 +5,7 @@ fn main() {
let (game_id, game_values) = line.split_once(": ").unwrap(); let (game_id, game_values) = line.split_once(": ").unwrap();
if !game_values.split("; ").any(|die_set| { if !game_values.split("; ").any(|die_set| {
die_set.split(", ").any(|v| { die_set.split(", ").any(|v| {
let (val, color) = v.split_once(" ").unwrap(); let (val, color) = v.split_once(' ').unwrap();
let val = val.parse::<usize>().unwrap(); let val = val.parse::<usize>().unwrap();
match color { match color {
"red" => val > 12, "red" => val > 12,
@ -15,7 +15,7 @@ fn main() {
} }
}) })
}) { }) {
result += game_id.split_once(" ").unwrap().1.parse::<i32>().unwrap(); result += game_id.split_once(' ').unwrap().1.parse::<i32>().unwrap();
} }
} }
println!("d02t1: {result}"); println!("d02t1: {result}");

View file

@ -7,8 +7,8 @@ fn main() {
let mut min_red = 0; let mut min_red = 0;
let mut min_green = 0; let mut min_green = 0;
let mut min_blue = 0; let mut min_blue = 0;
game_values.replace(";", ",").split(", ").for_each(|v| { game_values.replace(';', ",").split(", ").for_each(|v| {
let (val, color) = v.split_once(" ").unwrap(); let (val, color) = v.split_once(' ').unwrap();
let val = val.parse::<usize>().unwrap(); let val = val.parse::<usize>().unwrap();
match color { match color {
"red" => min_red = min_red.max(val), "red" => min_red = min_red.max(val),

View file

@ -2,17 +2,17 @@ use std::collections::{HashSet, VecDeque};
fn dec_if_pos(input: usize) -> usize { fn dec_if_pos(input: usize) -> usize {
if input > 0 { if input > 0 {
return input - 1; input - 1
} else { } else {
return input; input
} }
} }
fn inc_if_lt(input: usize, max: usize) -> usize { fn inc_if_lt(input: usize, max: usize) -> usize {
if input < max { if input < max {
return input + 1; input + 1
} else { } else {
return input; input
} }
} }
@ -29,8 +29,12 @@ fn main() {
.filter(|(_, char)| !char.is_ascii_digit() && *char != '.') .filter(|(_, char)| !char.is_ascii_digit() && *char != '.')
{ {
for x in dec_if_pos(char_idx)..=inc_if_lt(char_idx, line.len()) { for x in dec_if_pos(char_idx)..=inc_if_lt(char_idx, line.len()) {
for y in dec_if_pos(line_idx)..=inc_if_lt(line_idx, total_lines) { for (y, &cur_line) in lines
let cur_line = lines[y]; .iter()
.enumerate()
.take(inc_if_lt(line_idx, total_lines) + 1)
.skip(dec_if_pos(line_idx))
{
let line_chars = cur_line.chars().collect::<Vec<_>>(); let line_chars = cur_line.chars().collect::<Vec<_>>();
let mut cur_num_str = VecDeque::<char>::new(); let mut cur_num_str = VecDeque::<char>::new();
if line_chars[x].is_ascii_digit() { if line_chars[x].is_ascii_digit() {
@ -40,7 +44,7 @@ fn main() {
checked_fields.insert((x, y)); checked_fields.insert((x, y));
cur_num_str.push_back(line_chars[x]); cur_num_str.push_back(line_chars[x]);
if x > 0 { if x > 0 {
for i in (0..=(x - 1)).rev() { for i in (0..x).rev() {
if line_chars[i].is_ascii_digit() { if line_chars[i].is_ascii_digit() {
cur_num_str.push_front(line_chars[i]); cur_num_str.push_front(line_chars[i]);
checked_fields.insert((i, y)); checked_fields.insert((i, y));
@ -50,9 +54,14 @@ fn main() {
} }
} }
if x < line.len() { if x < line.len() {
for i in (x + 1)..=(line.len() - 1) { for (i, item) in line_chars
if line_chars[i].is_ascii_digit() { .iter()
cur_num_str.push_back(line_chars[i]); .enumerate()
.take((line.len() - 1) + 1)
.skip(x + 1)
{
if item.is_ascii_digit() {
cur_num_str.push_back(*item);
checked_fields.insert((i, y)); checked_fields.insert((i, y));
} else { } else {
break; break;

View file

@ -2,17 +2,17 @@ use std::collections::{HashSet, VecDeque};
fn dec_if_pos(input: usize) -> usize { fn dec_if_pos(input: usize) -> usize {
if input > 0 { if input > 0 {
return input - 1; input - 1
} else { } else {
return input; input
} }
} }
fn inc_if_lt(input: usize, max: usize) -> usize { fn inc_if_lt(input: usize, max: usize) -> usize {
if input < max { if input < max {
return input + 1; input + 1
} else { } else {
return input; input
} }
} }
@ -26,8 +26,12 @@ fn main() {
for (char_idx, _char) in line.chars().enumerate().filter(|(_, char)| *char == '*') { for (char_idx, _char) in line.chars().enumerate().filter(|(_, char)| *char == '*') {
let mut nums = Vec::<usize>::new(); let mut nums = Vec::<usize>::new();
for x in dec_if_pos(char_idx)..=inc_if_lt(char_idx, line.len()) { for x in dec_if_pos(char_idx)..=inc_if_lt(char_idx, line.len()) {
for y in dec_if_pos(line_idx)..=inc_if_lt(line_idx, total_lines) { for (y, &cur_line) in lines
let cur_line = lines[y]; .iter()
.enumerate()
.take(inc_if_lt(line_idx, total_lines) + 1)
.skip(dec_if_pos(line_idx))
{
let line_chars = cur_line.chars().collect::<Vec<_>>(); let line_chars = cur_line.chars().collect::<Vec<_>>();
let mut cur_num_str = VecDeque::<char>::new(); let mut cur_num_str = VecDeque::<char>::new();
if line_chars[x].is_ascii_digit() { if line_chars[x].is_ascii_digit() {
@ -37,7 +41,7 @@ fn main() {
checked_fields.insert((x, y)); checked_fields.insert((x, y));
cur_num_str.push_back(line_chars[x]); cur_num_str.push_back(line_chars[x]);
if x > 0 { if x > 0 {
for i in (0..=(x - 1)).rev() { for i in (0..x).rev() {
if line_chars[i].is_ascii_digit() { if line_chars[i].is_ascii_digit() {
cur_num_str.push_front(line_chars[i]); cur_num_str.push_front(line_chars[i]);
checked_fields.insert((i, y)); checked_fields.insert((i, y));
@ -47,9 +51,14 @@ fn main() {
} }
} }
if x < line.len() { if x < line.len() {
for i in (x + 1)..=(line.len() - 1) { for (i, item) in line_chars
if line_chars[i].is_ascii_digit() { .iter()
cur_num_str.push_back(line_chars[i]); .enumerate()
.take((line.len() - 1) + 1)
.skip(x + 1)
{
if item.is_ascii_digit() {
cur_num_str.push_back(*item);
checked_fields.insert((i, y)); checked_fields.insert((i, y));
} else { } else {
break; break;

View file

@ -3,6 +3,7 @@ fn main() {
println!("d04t1: {}", d04t1(input)); println!("d04t1: {}", d04t1(input));
} }
#[must_use]
pub fn d04t1(input: &str) -> usize { pub fn d04t1(input: &str) -> usize {
let mut result = 0; let mut result = 0;
for line in input.lines() { for line in input.lines() {

View file

@ -8,6 +8,7 @@ struct Card {
winning_amount: usize, winning_amount: usize,
} }
#[must_use]
pub fn d04t2(input: &str) -> usize { pub fn d04t2(input: &str) -> usize {
let mut result = 0; let mut result = 0;
let cards: Vec<Card> = input let cards: Vec<Card> = input
@ -17,7 +18,7 @@ pub fn d04t2(input: &str) -> usize {
.split_once(": ") .split_once(": ")
.map(|(cid, cd)| { .map(|(cid, cd)| {
( (
cid.split_once(" ") cid.split_once(' ')
.unwrap() .unwrap()
.1 .1
.trim() .trim()
@ -42,11 +43,11 @@ pub fn d04t2(input: &str) -> usize {
.unwrap(); .unwrap();
let winning_amount = actual_numbers let winning_amount = actual_numbers
.iter() .iter()
.filter(|num| winning_numbers.contains(&num)) .filter(|num| winning_numbers.contains(num))
.count(); .count();
Card { Card {
winning_amount,
card_id, card_id,
winning_amount,
} }
}) })
.collect(); .collect();

View file

@ -38,12 +38,12 @@ struct TranslationMap {
} }
trait SolveLayer { trait SolveLayer {
fn solve(&self, translation_maps: &Vec<TranslationMap>) -> Self; fn solve(&self, translation_maps: &[TranslationMap]) -> Self;
} }
impl SolveLayer for Vec<usize> { impl SolveLayer for Vec<usize> {
fn solve(&self, translation_maps: &Vec<TranslationMap>) -> Self { fn solve(&self, translation_maps: &[TranslationMap]) -> Self {
self.into_iter() self.iter()
.map(|l| translation_maps.translate(*l)) .map(|l| translation_maps.translate(*l))
.collect::<Vec<_>>() .collect::<Vec<_>>()
} }
@ -53,37 +53,10 @@ trait Translate {
fn translate(&self, input: usize) -> usize; fn translate(&self, input: usize) -> usize;
} }
trait Fold<T, U> { impl Translate for [TranslationMap] {
fn fold(self) -> U;
}
impl<T> Fold<T, Option<T>> for Option<Option<T>> {
fn fold(self) -> Option<T> {
if let Some(o) = self {
o
} else {
None
}
}
}
impl<T> Fold<T, Vec<T>> for Vec<Vec<T>> {
fn fold(self) -> Vec<T> {
let mut ret = Vec::new();
for inner in self {
for value in inner {
ret.push(value)
}
}
ret
}
}
impl Translate for Vec<TranslationMap> {
fn translate(&self, input: usize) -> usize { fn translate(&self, input: usize) -> usize {
self.iter() self.iter()
.map(|m| m.translate(input)) .find_map(|m| m.translate(input))
.filter(Option::is_some)
.map(Option::unwrap)
.next()
.unwrap_or(input) .unwrap_or(input)
} }
} }

View file

@ -11,14 +11,11 @@ pub fn d05t2(input: &str) -> usize {
.map(str::parse::<usize>) .map(str::parse::<usize>)
.map(Result::unwrap) .map(Result::unwrap)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let seeds = seeds let seeds = seeds.chunks(2).flat_map(|chunk| {
.chunks(2)
.map(|chunk| {
let start = chunk[0]; let start = chunk[0];
let range = chunk[1]; let range = chunk[1];
(start..(start + range)).collect::<Vec<_>>() (start..(start + range)).collect::<Vec<_>>()
}) });
.flatten();
let seed_to_spoil_map = parse_maps(groups.next().unwrap(), "seed-to-soil map:"); let seed_to_spoil_map = parse_maps(groups.next().unwrap(), "seed-to-soil map:");
let soil_to_fertilizer_map = parse_maps(groups.next().unwrap(), "soil-to-fertilizer map:"); let soil_to_fertilizer_map = parse_maps(groups.next().unwrap(), "soil-to-fertilizer map:");
let fertilizer_to_water_map = parse_maps(groups.next().unwrap(), "fertilizer-to-water map:"); let fertilizer_to_water_map = parse_maps(groups.next().unwrap(), "fertilizer-to-water map:");
@ -54,10 +51,7 @@ trait Translate {
impl Translate for Vec<TranslationMap> { impl Translate for Vec<TranslationMap> {
fn translate(&self, input: usize) -> usize { fn translate(&self, input: usize) -> usize {
self.iter() self.iter()
.map(|m| m.translate(input)) .find_map(|m| m.translate(input))
.filter(Option::is_some)
.map(Option::unwrap)
.next()
.unwrap_or(input) .unwrap_or(input)
} }
} }