generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06.rs
68 lines (58 loc) · 1.79 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
use itertools::Itertools;
advent_of_code::solution!(6);
pub fn part_one(input: &str) -> Option<usize> {
// Parse input into tuples of (time, speed) in usize based on
// Time: 7 15 30
// Distance: 9 40 200
let mut times_distances = input
.lines()
.map(|s| s.split_whitespace().filter_map(|s| s.parse::<usize>().ok()));
let times = times_distances.next().unwrap();
let distances = times_distances.next().unwrap();
let rounds = times.zip(distances);
let score = rounds
.map(|(time, record_distance)| {
(0..time)
.map(|press| {
let remaining = time - press;
let distance = press * remaining;
distance
})
.filter(|&distance| distance > record_distance)
.count()
})
.reduce(|a, b| a * b);
score
}
pub fn part_two(input: &str) -> Option<usize> {
let [time, speed]: [usize; 2] = input
.lines()
.map(|s| s.chars().filter(|c| c.is_numeric()).collect::<String>())
.filter_map(|s| s.parse::<usize>().ok())
.collect_vec()
.try_into()
.unwrap();
(0..time)
.map(|press| {
let remaining = time - press;
let distance = press * remaining;
distance
})
.filter(|&distance| distance > speed)
.count()
.into()
}
#[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));
}
}