remove unused map
This commit is contained in:
parent
d470daf83d
commit
64623b0446
1 changed files with 30 additions and 39 deletions
|
@ -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::<usize>()
|
||||
.expect(&format!("could not parse number: {}", num))
|
||||
})
|
||||
.for_each(|num| {
|
||||
wins_or_nums.trim().split_whitespace().into_iter().map(|num| {
|
||||
num.parse::<usize>().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 };
|
||||
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,25 +62,20 @@ impl FromStr for Card {
|
|||
}
|
||||
|
||||
fn part1(input: String) -> Result<usize> {
|
||||
Ok(input
|
||||
Ok(
|
||||
input
|
||||
.lines()
|
||||
.map(|line| line.parse::<Card>())
|
||||
.filter_map(|card| card.ok())
|
||||
.fold(0, |sum, card| sum + card.score))
|
||||
.fold(0, |sum, card| sum + card.score),
|
||||
)
|
||||
}
|
||||
|
||||
fn part2(input: String) -> Result<usize> {
|
||||
let mut sum = 0;
|
||||
let cards = input
|
||||
.lines()
|
||||
.map(|line| line.parse::<Card>())
|
||||
.filter_map(|card| card.ok())
|
||||
.collect_vec();
|
||||
let cards = input.lines().map(|line| line.parse::<Card>()).filter_map(|card| card.ok()).collect_vec();
|
||||
let mut queue: VecDeque<&Card> = cards.iter().collect();
|
||||
let mut counts: HashMap<usize, usize> = 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 {
|
||||
cards.get(card_index).map(|c| queue.push_back(&c));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue