From 966ac945ed437d9ed64d7f6e95ce242058502c0e Mon Sep 17 00:00:00 2001 From: Keenan Tims Date: Wed, 2 Dec 2026 13:57:37 -0700 Subject: [PATCH] 2022: clippies --- 2022/src/day5.rs | 12 ++++++------ 2022/src/day7.rs | 15 ++++++--------- 2022/src/lib.rs | 6 +++--- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/2022/src/day5.rs b/2022/src/day5.rs index eec6cb5..121d026 100644 --- a/2022/src/day5.rs +++ b/2022/src/day5.rs @@ -10,7 +10,7 @@ struct Instruction { to: usize, } -fn run_machine(stacks: &mut Vec>, instructions: &Vec) { +fn run_machine(stacks: &mut [VecDeque], instructions: &[Instruction]) { for inst in instructions { for _ in 0..inst.n { let moving = stacks[inst.from].pop_front().unwrap(); @@ -19,7 +19,7 @@ fn run_machine(stacks: &mut Vec>, instructions: &Vec) } } -fn run_machine2(stacks: &mut Vec>, instructions: &Vec) { +fn run_machine2(stacks: &mut [VecDeque], instructions: &[Instruction]) { for inst in instructions { let mut moving = stacks[inst.from].split_off(inst.n); // semantics of split_off are opposite of what we want @@ -42,7 +42,7 @@ fn parse(input: &str) -> (Vec>, Vec) { for line in input.lines() { match state { State::Stacks => { - for (stack, top) in line.as_bytes().chunks(4).into_iter().map(|s| s[1]).enumerate() { + for (stack, top) in line.as_bytes().chunks(4).map(|s| s[1]).enumerate() { if top.is_ascii_digit() { state = State::Ids } else if top != b' ' { @@ -51,7 +51,7 @@ fn parse(input: &str) -> (Vec>, Vec) { } } State::Ids => { - if line == "" { + if line.is_empty() { state = State::Instructions } } @@ -80,14 +80,14 @@ fn parse(input: &str) -> (Vec>, Vec) { #[aoc(day5, part1)] fn part1((stacks, instructions): &(Vec>, Vec)) -> String { let mut stacks = stacks.clone(); - run_machine(&mut stacks, &instructions); + run_machine(&mut stacks, instructions); stacks.iter().map(|s| *s.front().unwrap() as char).join("") } #[aoc(day5, part2)] fn part2((stacks, instructions): &(Vec>, Vec)) -> String { let mut stacks = stacks.clone(); - run_machine2(&mut stacks, &instructions); + run_machine2(&mut stacks, instructions); stacks.iter().map(|s| *s.front().unwrap() as char).join("") } diff --git a/2022/src/day7.rs b/2022/src/day7.rs index 2a55797..4276b29 100644 --- a/2022/src/day7.rs +++ b/2022/src/day7.rs @@ -42,16 +42,16 @@ fn dir_usage(fs: &Filesystem, path: &str) -> u64 { fn parse(input: &str) -> Filesystem { let mut cwd = "/".to_owned(); let mut fs = Filesystem::new(); - let mut lines = input.lines(); + let lines = input.lines(); - while let Some(line) = lines.next() { + for line in lines { let tokens = line.split_ascii_whitespace().collect_vec(); if tokens[0] == "$" { // command match tokens[1] { "cd" => { cwd = new_cwd(&cwd, tokens[2]); - fs.entry(cwd.to_owned()).or_insert(HashMap::new()); + fs.entry(cwd.to_owned()).or_default(); } "ls" => {} s => { @@ -67,7 +67,7 @@ fn parse(input: &str) -> Filesystem { } // dir entry else { - fs.entry(cwd.to_owned() + tokens[1] + "/").or_insert(HashMap::new()); + fs.entry(cwd.to_owned() + tokens[1] + "/").or_default(); fs.entry(cwd.to_owned()).and_modify(|e| { e.insert(tokens[1].into(), DirEntry::Directory); }); @@ -83,15 +83,12 @@ const MIN_FREE: u64 = 30000000; #[aoc(day7, part1)] fn part1(fs: &Filesystem) -> u64 { - fs.keys() - .map(|path| dir_usage(&fs, path)) - .filter(|v| *v <= 100000) - .sum() + fs.keys().map(|path| dir_usage(fs, path)).filter(|v| *v <= 100000).sum() } #[aoc(day7, part2)] fn part2(fs: &Filesystem) -> u64 { - let mut usages = fs.keys().map(|path| dir_usage(&fs, path)).sorted().collect_vec(); + let mut usages = fs.keys().map(|path| dir_usage(fs, path)).sorted().collect_vec(); let total_usage = usages.pop().unwrap(); // sorted list, largest dir will always be the root which can't be deleted let need_to_free_at_least = MIN_FREE - (AVAIL_DISK - total_usage); diff --git a/2022/src/lib.rs b/2022/src/lib.rs index a71d377..3dc247d 100644 --- a/2022/src/lib.rs +++ b/2022/src/lib.rs @@ -1,9 +1,9 @@ -mod day5; -mod day7; -mod day4; mod day1; mod day2; mod day3; +mod day4; +mod day5; +mod day7; use aoc_runner_derive::aoc_lib; mod day6;