From 64623b044623fdcea7ba021a74a3d0afbea06c34 Mon Sep 17 00:00:00 2001 From: "RingOfStorms (Joshua Bell)" Date: Mon, 4 Dec 2023 00:53:17 -0600 Subject: [PATCH] remove unused map --- src/bin/day4.rs | 69 +++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 39 deletions(-) diff --git a/src/bin/day4.rs b/src/bin/day4.rs index e286c34..214307c 100644 --- a/src/bin/day4.rs +++ b/src/bin/day4.rs @@ -1,7 +1,10 @@ use aoc23::prelude::*; use itertools::Itertools; use std::{ - collections::{HashMap, VecDeque}, + collections::{ + HashMap, + VecDeque, + }, str::FromStr, }; @@ -26,33 +29,26 @@ impl FromStr for Card { for part in s.split(':') { if id.is_some() { for (num_mode, wins_or_nums) in part.split('|').enumerate() { - wins_or_nums - .trim() - .split_whitespace() - .into_iter() - .map(|num| { - num.parse::() - .expect(&format!("could not parse number: {}", num)) - }) - .for_each(|num| { - if num_mode == 0 { - winning_numbers.push(num); - } else { - numbers.push(num); - if winning_numbers.iter().any(|winner| winner == &num) { - matches += 1; - score = if score == 0 { 1 } else { score * 2 }; - } + wins_or_nums.trim().split_whitespace().into_iter().map(|num| { + num.parse::().expect(&format!("could not parse number: {}", num)) + }).for_each(|num| { + if num_mode == 0 { + winning_numbers.push(num); + } else { + numbers.push(num); + if winning_numbers.iter().any(|winner| winner == &num) { + matches += 1; + score = if score == 0 { + 1 + } else { + score * 2 + }; } - }); + } + }); } } else { - id = Some( - part.split_whitespace() - .last() - .ok_or("Failed to get last item")? - .parse()?, - ) + id = Some(part.split_whitespace().last().ok_or("Failed to get last item")?.parse()?) } } Ok(Card { @@ -66,27 +62,22 @@ impl FromStr for Card { } fn part1(input: String) -> Result { - Ok(input - .lines() - .map(|line| line.parse::()) - .filter_map(|card| card.ok()) - .fold(0, |sum, card| sum + card.score)) + Ok( + input + .lines() + .map(|line| line.parse::()) + .filter_map(|card| card.ok()) + .fold(0, |sum, card| sum + card.score), + ) } fn part2(input: String) -> Result { let mut sum = 0; - let cards = input - .lines() - .map(|line| line.parse::()) - .filter_map(|card| card.ok()) - .collect_vec(); + let cards = input.lines().map(|line| line.parse::()).filter_map(|card| card.ok()).collect_vec(); let mut queue: VecDeque<&Card> = cards.iter().collect(); - let mut counts: HashMap = HashMap::new(); while let Some(card) = queue.pop_front() { - let counter = counts.entry(card.id).or_insert(0); - *counter += 1; sum += 1; - for card_index in card.id..card.id + card.matches { + for card_index in card.id .. card.id + card.matches { cards.get(card_index).map(|c| queue.push_back(&c)); } }