-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.rs
57 lines (50 loc) · 1.33 KB
/
day06.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
fn parse_num(input: &str) -> Vec<usize> {
input
.split_whitespace()
.skip(1)
.map(|num| num.parse().unwrap())
.collect()
}
fn parse_line(input: &str) -> usize {
input
.split_whitespace()
.skip(1)
.fold("".to_string(), |acc, s| acc + s)
.parse()
.unwrap()
}
#[aoc(day6, part1)]
pub fn part1(input: &str) -> usize {
let (times, dists) = input
.split_once("\n")
.map(|(first, second)| (parse_num(first), parse_num(second)))
.unwrap();
times
.iter()
.zip(&dists)
.map(|(&time, &dist)| {
(1..time)
.filter(|&t| (time - t) * t > dist)
.count()
})
.product()
}
#[aoc(day6, part2)]
pub fn part2(input: &str) -> usize {
let (time, dist) = input
.split_once("\n")
.map(|(first, second)| (parse_line(first), parse_line(second)))
.unwrap();
(1..time)
.filter(|t| (time - t) * t > dist)
.count()
}
// Apparently I can use a quadratic equation
#[aoc(day6, part2, quad)]
pub fn part2_quad(input: &str) -> usize {
let (time, dist) = input
.split_once("\n")
.map(|(first, second)| (parse_line(first), parse_line(second)))
.unwrap();
((time.pow(2) - 4 * dist) as f64).sqrt() as usize
}