-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04.rs
69 lines (59 loc) · 1.43 KB
/
04.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#![feature(test)]
use std::collections::VecDeque;
use rustc_hash::FxHashSet;
type Input = Vec<Card>;
#[derive(Debug)]
struct Card {
winning: FxHashSet<u32>,
mine: FxHashSet<u32>,
}
impl Card {
fn matches(&self) -> usize {
self.winning.intersection(&self.mine).count()
}
fn points(&self) -> usize {
let matches = self.matches();
if matches >= 1 {
2usize.pow(matches as u32 - 1)
} else {
0
}
}
}
fn setup(input: &str) -> Input {
input
.lines()
.map(|line| {
let mut line = line.split(':').nth(1).unwrap().split('|');
let mut nums = || {
line.next()
.unwrap()
.split_whitespace()
.map(|n| n.parse().unwrap())
.collect()
};
Card {
winning: nums(),
mine: nums(),
}
})
.collect()
}
fn part1(input: &Input) -> usize {
input.iter().map(Card::points).sum()
}
fn part2(input: &Input) -> usize {
let mut counts = VecDeque::from(vec![1; input.len()]);
input
.iter()
.map(|card| {
let n = counts.pop_front().unwrap();
counts
.iter_mut()
.take(card.matches())
.for_each(|cnt| *cnt += n);
n
})
.sum()
}
aoc::main!(2023, 4, ex: 1);