Merge branch 'master' of github.com:RingOfStorms/advent_of_code_2023
This commit is contained in:
commit
71d8241b43
1 changed files with 14 additions and 11 deletions
|
@ -6,11 +6,12 @@ use std::{
|
||||||
time::Instant,
|
time::Instant,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
|
||||||
struct Card {
|
struct Card {
|
||||||
id: usize,
|
id: usize,
|
||||||
part_2_matches: usize,
|
|
||||||
part_1_score: usize,
|
part_1_score: usize,
|
||||||
|
part_2_matches: usize,
|
||||||
|
part_2_count: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for Card {
|
impl FromStr for Card {
|
||||||
|
@ -56,6 +57,7 @@ impl FromStr for Card {
|
||||||
id: id.ok_or("no id found")?,
|
id: id.ok_or("no id found")?,
|
||||||
part_1_score: score,
|
part_1_score: score,
|
||||||
part_2_matches: matches,
|
part_2_matches: matches,
|
||||||
|
part_2_count: 1,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,20 +89,21 @@ fn part2(input: String) -> Result<usize> {
|
||||||
|
|
||||||
fn part2_revised(input: String) -> Result<usize> {
|
fn part2_revised(input: String) -> Result<usize> {
|
||||||
let mut sum = 0;
|
let mut sum = 0;
|
||||||
let cards = input
|
let mut cards = input
|
||||||
.lines()
|
.lines()
|
||||||
.map(|line| line.parse::<Card>())
|
.map(|line| line.parse::<Card>())
|
||||||
.filter_map(|card| card.ok())
|
.filter_map(|card| card.ok())
|
||||||
.collect_vec();
|
.collect_vec();
|
||||||
let mut card_counts: HashMap<usize, usize> = HashMap::new();
|
for i in 0..cards.len() {
|
||||||
for card in cards {
|
let (count, matches) = {
|
||||||
let count = {
|
let card = cards.get(i).ok_or("card not found")?;
|
||||||
let card_count = card_counts.entry(card.id).or_insert(1);
|
sum += card.part_2_count;
|
||||||
sum += *card_count;
|
(card.part_2_count, card.part_2_matches)
|
||||||
*card_count
|
|
||||||
};
|
};
|
||||||
for card_index in 1..=card.part_2_matches {
|
for card_index in 1..=matches {
|
||||||
*card_counts.entry(card.id + card_index).or_insert(1) += count;
|
cards.get_mut(i + card_index).map(|card_below| {
|
||||||
|
card_below.part_2_count += count;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(sum)
|
Ok(sum)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue