generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
04.rs
83 lines (77 loc) · 2.26 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use std::collections::HashSet;
advent_of_code::solution!(4);
pub fn part_one(input: &str) -> Option<u32> {
let cards: Vec<u32> = input
.lines()
.map(|line| {
let split: Vec<HashSet<u32>> = line
.split(':')
.last()
.expect("Should be a string afer :")
.split('|')
.map(|s| {
s.split_whitespace()
.map(|ss| ss.parse::<u32>().expect("Should all be numbers"))
.collect()
})
.collect();
split[0].intersection(&split[1]).count().try_into().unwrap()
})
.collect();
let sum: u32 = cards
.iter()
.filter_map(|&count| {
if count < 1 {
None
} else {
Some(u32::pow(2, count - 1))
}
})
.sum();
Some(sum)
}
pub fn part_two(input: &str) -> Option<u32> {
let cards: Vec<u32> = input
.lines()
.map(|line| {
let split: Vec<HashSet<u32>> = line
.split(':')
.last()
.expect("Should be a string afer :")
.split('|')
.map(|s| {
s.split_whitespace()
.map(|ss| ss.parse::<u32>().expect("Should all be numbers"))
.collect()
})
.collect();
split[0].intersection(&split[1]).count().try_into().unwrap()
})
.collect();
let mut cards_count = vec![1_u32; cards.len()];
cards.iter().enumerate().for_each(|(index, &num)| {
if num < 1 {
return;
}
for _ in 0..cards_count[index] {
for i in 1..=num {
cards_count[index + (i as usize)] += 1;
}
}
});
Some(cards_count.iter().sum())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(13));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(30));
}
}