From 1970b5b3472398ae6a96ad8720096e941f71a062 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Fri, 23 Dec 2022 15:55:39 +0200 Subject: Clippy-driven cleanup --- 2022/src/bin/day_23.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to '2022/src') diff --git a/2022/src/bin/day_23.rs b/2022/src/bin/day_23.rs index 1267431..44483a5 100644 --- a/2022/src/bin/day_23.rs +++ b/2022/src/bin/day_23.rs @@ -24,7 +24,7 @@ fn main() -> Result<(), Box> { } { - let mut elves_2 = elves.clone(); + let mut elves_2 = elves; for round in 1.. { if !elves_2.process_round() { dbg!(round); @@ -98,17 +98,23 @@ impl ElfMap { // phase 1: figure out where each elf wants to move for elf in &self.elves { - if let Some(destination) = self.find_elf_move(&elf) { - if elf_moves.contains_key(&destination) { - elf_moves.remove(&destination); - conflict_moves.insert(destination); - } else if !conflict_moves.contains(&destination) { - elf_moves.insert(destination, elf.clone()); + if let Some(destination) = self.find_elf_move(elf) { + use std::collections::btree_map::Entry; + match elf_moves.entry(destination) { + Entry::Occupied(elf_move_entry) => { + conflict_moves.insert(elf_move_entry.key().clone()); + elf_move_entry.remove_entry(); + } + Entry::Vacant(elf_move_entry) => { + if !conflict_moves.contains(elf_move_entry.key()) { + elf_move_entry.insert(elf.clone()); + } + } } } } - let any_elf_moved = elf_moves.len() > 0; + let any_elf_moved = !elf_moves.is_empty(); // phase 2: move the elves for (dest, src) in elf_moves { -- cgit v1.2.3