From d303f55a4e05c05fe21cb89fb5bb4ef7454cbed4 Mon Sep 17 00:00:00 2001 From: kageru Date: Thu, 15 Dec 2022 20:50:38 +0100 Subject: [PATCH] clippy --- 2022/src/bin/day08.rs | 4 ++-- 2022/src/bin/day11.rs | 29 +++++++++++++++-------------- 2022/src/bin/day12.rs | 2 +- 2022/src/bin/day14.rs | 4 ++-- 2022/src/common.rs | 2 +- 5 files changed, 21 insertions(+), 20 deletions(-) diff --git a/2022/src/bin/day08.rs b/2022/src/bin/day08.rs index c039925..84f0914 100644 --- a/2022/src/bin/day08.rs +++ b/2022/src/bin/day08.rs @@ -28,8 +28,8 @@ fn is_visible_1d<'a>(iter: impl IntoIterator, rev: bool) -> Vec(v: &Vec>) -> Vec> { - let mut v = v.clone(); +fn transpose(v: &[Vec]) -> Vec> { + let mut v = v.to_vec(); let size = v.len(); for i in 0..size { for j in i + 1..size { diff --git a/2022/src/bin/day11.rs b/2022/src/bin/day11.rs index 5720c13..6d4f1de 100644 --- a/2022/src/bin/day11.rs +++ b/2022/src/bin/day11.rs @@ -61,21 +61,22 @@ fn monkey_business(parse for _ in 0..ITERATIONS { let mut i = 0; while i < monkeys.len() { - let monkey = monkeys.get_mut(i).unwrap(); - monkey.inspection_count += monkey.items.len(); - while !monkey.items.is_empty() { - moved.extend(monkey.items.drain(..).map(|mut stress| { - stress = match monkey.op { - MonkeyOp::Add(x) => stress + x.unwrap_or(stress), - MonkeyOp::Mul(x) => stress * x.unwrap_or(stress), - } / STRESS_REDUCTION; - if stress > lcm { - stress %= lcm; - } - (stress, if stress % monkey.div_test == 0 { monkey.true_dst } else { monkey.false_dst }) - })); + { + let monkey = monkeys.get_mut(i).unwrap(); + monkey.inspection_count += monkey.items.len(); + while !monkey.items.is_empty() { + moved.extend(monkey.items.drain(..).map(|mut stress| { + stress = match monkey.op { + MonkeyOp::Add(x) => stress + x.unwrap_or(stress), + MonkeyOp::Mul(x) => stress * x.unwrap_or(stress), + } / STRESS_REDUCTION; + if stress > lcm { + stress %= lcm; + } + (stress, if stress % monkey.div_test == 0 { monkey.true_dst } else { monkey.false_dst }) + })); + } } - drop(monkey); for (item, dst) in moved.drain(..) { monkeys.get_mut(dst).unwrap().items.push(item); } diff --git a/2022/src/bin/day12.rs b/2022/src/bin/day12.rs index c9b3883..8d896ac 100644 --- a/2022/src/bin/day12.rs +++ b/2022/src/bin/day12.rs @@ -11,7 +11,7 @@ use aoc2022::{ const DAY: usize = 12; type Parsed = (Position2D, Position2D, VecGrid); -fn find_and_modify(grid: &mut Vec>, needle: u8, replacement: u8) -> (usize, usize) { +fn find_and_modify(grid: &mut [Vec], needle: u8, replacement: u8) -> (usize, usize) { grid.iter_mut() .enumerate() .find_map(|(x, line)| { diff --git a/2022/src/bin/day14.rs b/2022/src/bin/day14.rs index d188856..8e69a68 100644 --- a/2022/src/bin/day14.rs +++ b/2022/src/bin/day14.rs @@ -44,14 +44,14 @@ fn simulate((x, y): (usize, usize), cave: &Cave) -> Option<(usize, usize)> { // abusing scan here to simulate a stateful for-loop with a counter fn part1((cave, _): &Parsed) -> usize { - repeat(()).scan(cave.to_owned(), |cave, _| simulate(SAND_SOURCE, &cave).map(|(x, y)| cave[x][y] = true)).count() + repeat(()).scan(cave.to_owned(), |cave, _| simulate(SAND_SOURCE, cave).map(|(x, y)| cave[x][y] = true)).count() } fn part2((cave, max_y): &Parsed) -> usize { let mut cave = cave.to_owned(); cave.iter_mut().for_each(|row| row[max_y + 2] = true); 1 + repeat(()) - .scan(cave, |cave, _| simulate(SAND_SOURCE, &cave).map(|(x, y)| cave[x][y] = true).filter(|_| !cave[SAND_SOURCE.0][SAND_SOURCE.1])) + .scan(cave, |cave, _| simulate(SAND_SOURCE, cave).map(|(x, y)| cave[x][y] = true).filter(|_| !cave[SAND_SOURCE.0][SAND_SOURCE.1])) .count() } diff --git a/2022/src/common.rs b/2022/src/common.rs index 700a60a..1206d9a 100644 --- a/2022/src/common.rs +++ b/2022/src/common.rs @@ -1,5 +1,5 @@ pub fn read_file(day: usize) -> String { - std::fs::read_to_string(std::env::var("AOC_INPUT").unwrap_or_else(|_| format!("inputs/day{:0>2}", day))).unwrap() + std::fs::read_to_string(std::env::var("AOC_INPUT").unwrap_or_else(|_| format!("inputs/day{day:0>2}"))).unwrap() } pub fn parse_nums(l: &str) -> Vec {