Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ad1cfad

Browse files
authored
Merge pull request #81 from wtchangdm/feature/improve_day_4
improve day4 a bit
2 parents f251f36 + 2a3661f commit ad1cfad

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

‎2023/04/wtchangdm/day4.rs‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,21 @@ pub fn solve_part1(input: &[String]) -> u32 {
4646
}
4747

4848
pub fn solve_part2(input: &[String]) -> u32 {
49-
let mut count: HashMap<u32, u32> = HashMap::new();
49+
input
50+
.iter()
51+
.map(|line| Card::from(line))
52+
.fold(HashMap::new(), |mut count, card| {
53+
*count.entry(card.id).or_insert(0) += 1;
5054

51-
input.iter().map(|line| Card::from(line)).for_each(|card| {
52-
*count.entry(card.id).or_insert(0) += 1;
55+
// inspired by https://github.com/rust-tw/advent-of-code/tree/main/2023/04
56+
(1..=card.won_copies())
57+
.map(|i| i + card.id)
58+
.for_each(|id| *count.entry(id).or_insert(0) += *count.get(&card.id).unwrap());
5359

54-
// inspired by https://github.com/rust-tw/advent-of-code/tree/main/2023/04
55-
(1..=card.won_copies()).map(|i| i + card.id).for_each(|id| {
56-
*count.entry(id).or_insert(0) += *count.get(&card.id).unwrap();
60+
count
5761
})
58-
});
59-
60-
count.values().sum()
62+
.values()
63+
.sum()
6164
}
6265

6366
#[cfg(test)]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /