generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06.rs
97 lines (75 loc) · 2.54 KB
/
06.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
use regex::Regex;
advent_of_code::solution!(6);
pub fn part_one(input: &str) -> Option<u64> {
let races = parse_races(input);
let product_of_possible_wins: u64 = races.iter().map(get_amount_of_wins).product();
Some(product_of_possible_wins)
}
pub fn part_two(input: &str) -> Option<u64> {
let race = parse_race(input);
Some(get_amount_of_wins(&race))
}
fn parse_races(input: &str) -> Vec<(u64, u64)> {
let space_regex = Regex::new(r"\s+").unwrap();
let mut lines = input.lines();
let times_string = lines.next().unwrap().split(':').nth(1).unwrap().trim();
let distances_string = lines.next().unwrap().split(':').nth(1).unwrap().trim();
let times_split = space_regex.split(times_string);
let distances_split = space_regex.split(distances_string);
times_split
.zip(distances_split)
.map(|(time, distance)| {
let time = time.parse::<u64>().unwrap();
let record_distance = distance.parse::<u64>().unwrap();
(time, record_distance)
})
.collect()
}
fn get_amount_of_wins((time_ref, record_distance_ref): &(u64, u64)) -> u64 {
let time = *time_ref;
let record_distance = *record_distance_ref;
let mut charge_duration = 1;
let mut amount_of_wins = 0;
while charge_duration < time {
let moving_duration = time - charge_duration;
let distance = moving_duration * charge_duration;
if distance > record_distance {
amount_of_wins += 1;
} else if amount_of_wins != 0 {
break;
}
charge_duration += 1
}
amount_of_wins
}
fn parse_race(input: &str) -> (u64, u64) {
let space_regex = Regex::new(r"\s+").unwrap();
let mut lines = input.lines();
let time_string = lines.next().unwrap().split(':').nth(1).unwrap().trim();
let distance_string = lines.next().unwrap().split(':').nth(1).unwrap().trim();
let time = space_regex
.split(time_string)
.collect::<String>()
.parse::<u64>()
.unwrap();
let distance = space_regex
.split(distance_string)
.collect::<String>()
.parse::<u64>()
.unwrap();
(time, distance)
}
#[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(288));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(71503));
}
}