-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6.rs
66 lines (59 loc) · 1.52 KB
/
day6.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
use std::num::ParseIntError;
fn win_count(time: u64, distance: u64) -> u64 {
let b = time as f64 / 2.0;
let d = (b * b - distance as f64).sqrt();
((b + d - 1.0).ceil() - (b - d + 1.0).floor() + 1.0) as u64
}
pub fn part1(data: &str) -> u64 {
let mut lines = data.lines();
lines
.next()
.unwrap_or("")
.split(|c: char| !c.is_ascii_digit())
.filter_map(|s| s.parse().ok())
.zip(
lines
.next()
.unwrap_or("")
.split(|c: char| !c.is_ascii_digit())
.filter_map(|s| s.parse().ok()),
)
.map(|(time, distance)| win_count(time, distance))
.product()
}
pub fn part2(data: &str) -> Result<u64, ParseIntError> {
let mut lines = data.lines();
let time = lines
.next()
.unwrap_or("")
.chars()
.filter(char::is_ascii_digit)
.collect::<String>()
.parse()?;
let distance = lines
.next()
.unwrap_or("")
.chars()
.filter(char::is_ascii_digit)
.collect::<String>()
.parse()?;
Ok(win_count(time, distance))
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
use pretty_assertions::assert_eq;
static EXAMPLE: &str = indoc! {"
Time: 7 15 30
Distance: 9 40 200
"};
#[test]
fn part1_examples() {
assert_eq!(288, part1(EXAMPLE));
}
#[test]
fn part2_examples() {
assert_eq!(Ok(71503), part2(EXAMPLE));
}
}