This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
04.rs
82 lines (60 loc) · 2.05 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
use itertools::Itertools;
advent_of_code::solution!(4);
/* -------------------------------------------------------------------------- */
#[derive(Clone, Copy)]
struct Card {
match_count: usize,
}
fn parse_input(input: &str) -> Vec<Card> {
input
.lines()
.map(|line| {
let (_head, rest) = line.split_once(':').unwrap();
let (win_numbers, numbers) = rest.split_once('|').unwrap();
let win_numbers = win_numbers
.split_ascii_whitespace()
.map(|x| x.parse::<u32>().unwrap())
.collect_vec();
let numbers = numbers.split_ascii_whitespace().map(|x| x.parse().unwrap());
let match_count = numbers.filter(|nb| win_numbers.contains(nb)).count();
Card { match_count }
})
.collect()
}
/* -------------------------------------------------------------------------- */
pub fn part_one(input: &str) -> Option<u32> {
let cards = parse_input(input);
let result = cards
.into_iter()
.map(|card| card.match_count.checked_sub(1).map(|x| 1 << x).unwrap_or(0))
.sum();
Some(result)
}
/* -------------------------------------------------------------------------- */
pub fn part_two(input: &str) -> Option<u32> {
let cards = parse_input(input);
let mut card_count = vec![1; cards.len()];
for (i, card) in cards.into_iter().enumerate() {
let count = card_count[i];
for j in 0..card.match_count {
card_count[i + 1 + j] += count;
}
}
let result = card_count.into_iter().sum();
Some(result)
}
/* -------------------------------------------------------------------------- */
#[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));
}
}