-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.rs
148 lines (124 loc) · 4.4 KB
/
day4.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use std::collections::{HashMap, HashSet};
use regex::Regex;
use crate::input_reader::read_lines;
#[derive(Debug, Clone, Eq, PartialEq)]
struct Card {
index: usize,
winning_numbers: HashSet<usize>,
numbers: HashSet<usize>
}
impl Card {
fn build_from(raw_card: &str) -> Self {
let parts: Vec<_> = raw_card.split(": ").collect();
let card_with_index = parts[0];
let numbers: Vec<_> = parts[1].split(" | ").collect();
let index = parse_card_index(card_with_index);
let winning_numbers: HashSet<_> = parse_numbers(numbers[0]);
let numbers: HashSet<_> = parse_numbers(numbers[1]);
Card { index, winning_numbers, numbers }
}
fn score(&self) -> usize {
let number_of_overlapping_cards = self.number_of_overlapping_cards();
if number_of_overlapping_cards == 0 {
return 0;
}
1 << (number_of_overlapping_cards - 1)
}
fn number_of_overlapping_cards(&self) -> usize {
self.numbers.intersection(&self.winning_numbers).count()
}
}
fn parse_card_index(card_with_index: &str) -> usize {
Regex::new(r"(\d+)").unwrap()
.captures(card_with_index)
.unwrap()
.get(1)
.unwrap()
.as_str()
.parse::<usize>()
.unwrap()
}
fn parse_numbers(values: &str) -> HashSet<usize> {
Regex::new(r"(\d+)")
.unwrap()
.captures_iter(values)
.map(|c| c.extract())
.map(|(_, [value])| value.parse::<usize>().unwrap())
.collect()
}
fn deck_score(input: &str) -> usize {
cards_from(input)
.iter()
.map(|c| c.score())
.sum()
}
fn calculate_number_of_copies(input: &str) -> usize {
let mut copies: HashMap<usize, usize> = HashMap::new();
for card in cards_from(input) {
*copies.entry(card.index).or_insert(0)+=1;
let card_copies = copies.get(&card.index).unwrap().clone();
for card_index in 1..=card.number_of_overlapping_cards() {
*copies.entry(card.index + card_index).or_insert(0)+=card_copies;
}
}
copies.values().sum()
}
fn cards_from(input: &str) -> Vec<Card> {
read_lines(input)
.iter()
.map(|c| Card::build_from(c))
.collect()
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use indoc::indoc;
use crate::day4::*;
use crate::input_reader::read_input_file;
#[test]
fn it_solves_first_part() {
let input = read_input_file("input_day04.txt");
assert_eq!(24706, deck_score(&input));
}
#[test]
fn it_solves_second_part() {
let input = read_input_file("input_day04.txt");
assert_eq!(13114317, calculate_number_of_copies(&input));
}
#[test]
fn it_calculate_number_of_copies() {
let input = indoc! {"
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11"};
assert_eq!(30, calculate_number_of_copies(&input));
}
#[test]
fn it_calculates_deck_score() {
let input = indoc! {"
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11"};
assert_eq!(13, deck_score(input));
}
#[test]
fn it_calculates_card_score() {
assert_eq!(8, Card::build_from("Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53").score());
assert_eq!(2, Card::build_from("Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19").score());
assert_eq!(1, Card::build_from("Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83").score());
assert_eq!(0, Card::build_from("Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36").score());
}
#[test]
fn it_parses_a_card() {
let card = Card::build_from("Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53");
assert_eq!(1, card.index);
assert_eq!(HashSet::from([48, 41, 86, 83, 17]), card.winning_numbers);
assert_eq!(HashSet::from([83,86,6,31,17,9,48,53]), card.numbers);
}
}