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| {
let digits = line
.chars()
.filter(|c| c.is_ascii_digit())
.filter(char::is_ascii_digit)
.collect::<Vec<char>>();
let mut num_str = digits.first().unwrap().to_string();
num_str.push(*digits.last().unwrap());

View file

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

View file

@ -5,7 +5,7 @@ fn main() {
let (game_id, game_values) = line.split_once(": ").unwrap();
if !game_values.split("; ").any(|die_set| {
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();
match color {
"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}");

View file

@ -7,8 +7,8 @@ fn main() {
let mut min_red = 0;
let mut min_green = 0;
let mut min_blue = 0;
game_values.replace(";", ",").split(", ").for_each(|v| {
let (val, color) = v.split_once(" ").unwrap();
game_values.replace(';', ",").split(", ").for_each(|v| {
let (val, color) = v.split_once(' ').unwrap();
let val = val.parse::<usize>().unwrap();
match color {
"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 {
if input > 0 {
return input - 1;
input - 1
} else {
return input;
input
}
}
fn inc_if_lt(input: usize, max: usize) -> usize {
if input < max {
return input + 1;
input + 1
} else {
return input;
input
}
}
@ -29,8 +29,12 @@ fn main() {
.filter(|(_, char)| !char.is_ascii_digit() && *char != '.')
{
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) {
let cur_line = lines[y];
for (y, &cur_line) in lines
.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 mut cur_num_str = VecDeque::<char>::new();
if line_chars[x].is_ascii_digit() {
@ -40,7 +44,7 @@ fn main() {
checked_fields.insert((x, y));
cur_num_str.push_back(line_chars[x]);
if x > 0 {
for i in (0..=(x - 1)).rev() {
for i in (0..x).rev() {
if line_chars[i].is_ascii_digit() {
cur_num_str.push_front(line_chars[i]);
checked_fields.insert((i, y));
@ -50,9 +54,14 @@ fn main() {
}
}
if x < line.len() {
for i in (x + 1)..=(line.len() - 1) {
if line_chars[i].is_ascii_digit() {
cur_num_str.push_back(line_chars[i]);
for (i, item) in line_chars
.iter()
.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));
} else {
break;

View file

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

View file

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

View file

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

View file

@ -38,12 +38,12 @@ struct TranslationMap {
}
trait SolveLayer {
fn solve(&self, translation_maps: &Vec<TranslationMap>) -> Self;
fn solve(&self, translation_maps: &[TranslationMap]) -> Self;
}
impl SolveLayer for Vec<usize> {
fn solve(&self, translation_maps: &Vec<TranslationMap>) -> Self {
self.into_iter()
fn solve(&self, translation_maps: &[TranslationMap]) -> Self {
self.iter()
.map(|l| translation_maps.translate(*l))
.collect::<Vec<_>>()
}
@ -53,37 +53,10 @@ trait Translate {
fn translate(&self, input: usize) -> usize;
}
trait Fold<T, U> {
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> {
impl Translate for [TranslationMap] {
fn translate(&self, input: usize) -> usize {
self.iter()
.map(|m| m.translate(input))
.filter(Option::is_some)
.map(Option::unwrap)
.next()
.find_map(|m| m.translate(input))
.unwrap_or(input)
}
}

View file

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