2022: clippies

This commit is contained in:
ktims committed 2026-12-02 13:57:38 -07:00
1 parent b41c049a38
commit 966ac945ed
3 files changed
+15 -18

No files matched your search

+6 -6
View File
@@ -10,7 +10,7 @@ struct Instruction {
to: usize, to: usize,
} }
fn run_machine(stacks: &mut Vec<VecDeque<u8>>, instructions: &Vec<Instruction>) { fn run_machine(stacks: &mut [VecDeque<u8>], instructions: &[Instruction]) {
for inst in instructions { for inst in instructions {
for _ in 0..inst.n { for _ in 0..inst.n {
let moving = stacks[inst.from].pop_front().unwrap(); let moving = stacks[inst.from].pop_front().unwrap();
@@ -19,7 +19,7 @@ fn run_machine(stacks: &mut Vec<VecDeque<u8>>, instructions: &Vec<Instruction>)
} }
} }
fn run_machine2(stacks: &mut Vec<VecDeque<u8>>, instructions: &Vec<Instruction>) { fn run_machine2(stacks: &mut [VecDeque<u8>], instructions: &[Instruction]) {
for inst in instructions { for inst in instructions {
let mut moving = stacks[inst.from].split_off(inst.n); let mut moving = stacks[inst.from].split_off(inst.n);
// semantics of split_off are opposite of what we want // semantics of split_off are opposite of what we want
@@ -42,7 +42,7 @@ fn parse(input: &str) -> (Vec<VecDeque<u8>>, Vec<Instruction>) {
for line in input.lines() { for line in input.lines() {
match state { match state {
State::Stacks => { 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() { if top.is_ascii_digit() {
state = State::Ids state = State::Ids
} else if top != b' ' { } else if top != b' ' {
@@ -51,7 +51,7 @@ fn parse(input: &str) -> (Vec<VecDeque<u8>>, Vec<Instruction>) {
} }
} }
State::Ids => { State::Ids => {
if line == "" { if line.is_empty() {
state = State::Instructions state = State::Instructions
} }
} }
@@ -80,14 +80,14 @@ fn parse(input: &str) -> (Vec<VecDeque<u8>>, Vec<Instruction>) {
#[aoc(day5, part1)] #[aoc(day5, part1)]
fn part1((stacks, instructions): &(Vec<VecDeque<u8>>, Vec<Instruction>)) -> String { fn part1((stacks, instructions): &(Vec<VecDeque<u8>>, Vec<Instruction>)) -> String {
let mut stacks = stacks.clone(); 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("") stacks.iter().map(|s| *s.front().unwrap() as char).join("")
} }
#[aoc(day5, part2)] #[aoc(day5, part2)]
fn part2((stacks, instructions): &(Vec<VecDeque<u8>>, Vec<Instruction>)) -> String { fn part2((stacks, instructions): &(Vec<VecDeque<u8>>, Vec<Instruction>)) -> String {
let mut stacks = stacks.clone(); 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("") stacks.iter().map(|s| *s.front().unwrap() as char).join("")
} }
+6 -9
View File
@@ -42,16 +42,16 @@ fn dir_usage(fs: &Filesystem, path: &str) -> u64 {
fn parse(input: &str) -> Filesystem { fn parse(input: &str) -> Filesystem {
let mut cwd = "/".to_owned(); let mut cwd = "/".to_owned();
let mut fs = Filesystem::new(); 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(); let tokens = line.split_ascii_whitespace().collect_vec();
if tokens[0] == "$" { if tokens[0] == "$" {
// command // command
match tokens[1] { match tokens[1] {
"cd" => { "cd" => {
cwd = new_cwd(&cwd, tokens[2]); cwd = new_cwd(&cwd, tokens[2]);
fs.entry(cwd.to_owned()).or_insert(HashMap::new()); fs.entry(cwd.to_owned()).or_default();
} }
"ls" => {} "ls" => {}
s => { s => {
@@ -67,7 +67,7 @@ fn parse(input: &str) -> Filesystem {
} }
// dir entry // dir entry
else { 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| { fs.entry(cwd.to_owned()).and_modify(|e| {
e.insert(tokens[1].into(), DirEntry::Directory); e.insert(tokens[1].into(), DirEntry::Directory);
}); });
@@ -83,15 +83,12 @@ const MIN_FREE: u64 = 30000000;
#[aoc(day7, part1)] #[aoc(day7, part1)]
fn part1(fs: &Filesystem) -> u64 { fn part1(fs: &Filesystem) -> u64 {
fs.keys() fs.keys().map(|path| dir_usage(fs, path)).filter(|v| *v <= 100000).sum()
.map(|path| dir_usage(&fs, path))
.filter(|v| *v <= 100000)
.sum()
} }
#[aoc(day7, part2)] #[aoc(day7, part2)]
fn part2(fs: &Filesystem) -> u64 { 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 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); let need_to_free_at_least = MIN_FREE - (AVAIL_DISK - total_usage);
+3 -3
View File
@@ -1,9 +1,9 @@
mod day5;
mod day7;
mod day4;
mod day1; mod day1;
mod day2; mod day2;
mod day3; mod day3;
mod day4;
mod day5;
mod day7;
use aoc_runner_derive::aoc_lib; use aoc_runner_derive::aoc_lib;
mod day6; mod day6;