-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.rs
134 lines (115 loc) · 3.13 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
type Int = i64;
#[derive(Debug)]
struct Race {
time: Int,
distance: Int,
}
fn parser_part1_line(line: &str) -> impl Iterator<Item = Int> + '_ {
return line
.split_once(':')
.unwrap()
.1
.trim()
.split_ascii_whitespace()
.map(str::parse)
.map(Result::unwrap);
}
#[aoc_generator(day6, part1)]
fn parser_part1(input: &str) -> Vec<Race> {
let (times, distances) = input.split_once('\n').unwrap();
return parser_part1_line(times)
.zip(parser_part1_line(distances))
.map(|(time, distance)| Race { time, distance })
.collect();
}
fn parser_part2_line(line: &str) -> Int {
line.chars().filter(char::is_ascii_digit).fold(0, |acc, c| {
acc.checked_mul(10).unwrap() + Int::from(c.to_digit(10).unwrap())
})
}
#[aoc_generator(day6, part2)]
fn parser_part2(input: &str) -> Race {
let (t, d) = input.split_once('\n').unwrap();
return Race {
time: parser_part2_line(t),
distance: parser_part2_line(d),
};
}
fn dist(time: Int, hold: Int) -> Int {
(time - hold) * hold
}
#[aoc(day6, part1, iterative)]
fn solver_part1(data: &[Race]) -> Int {
return data
.iter()
.map(|race| {
(1..race.time)
.filter(|&hold| dist(race.time, hold) > race.distance)
.count()
})
.reduce(|a, b| a * b)
.unwrap()
.try_into()
.unwrap();
}
#[aoc(day6, part2, iterative)]
fn solver_part2(race: &Race) -> Int {
let mut first: Option<Int> = Option::None;
let mut last: Option<Int> = Option::None;
for i in 1..race.time {
let b = dist(race.time, i) > race.distance;
if b {
first = Some(i);
break;
}
}
for i in (1..race.time).rev() {
let b = dist(race.time, i) > race.distance;
if b {
last = Some(i);
break;
}
}
return last.unwrap() - first.unwrap() + 1;
}
fn conv(int: i64) -> f64 {
let a = (int >> 32) as u32;
let b = int as u32;
return f64::from(a).mul_add(2.0_f64.powi(32), f64::from(b));
}
#[aoc(day6, part2, analytical)]
fn solver_part2_ana(race: &Race) -> Int {
let a: f64 = conv(race.time);
let b: f64 = conv(race.distance);
let sqrt_res = b.mul_add(-4.0, a.powi(2)).sqrt();
let f = 0.5 * (a - sqrt_res);
let s = 0.5 * (sqrt_res + a);
return (s.ceil() as Int) - (f.floor() as Int) - 1;
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE_1: &str = "Time: 7 15 30\nDistance: 9 40 200";
#[test]
fn test_parser_part1() {
let result = parser_part1(EXAMPLE_1);
println!("{:?}", result);
}
#[test]
fn test_parser_part2() {
let result = parser_part2(EXAMPLE_1);
println!("{:?}", result);
}
#[test]
fn test_solver_part1() {
assert_eq!(288, solver_part1(&parser_part1(EXAMPLE_1)));
}
#[test]
fn test_solver_part2() {
assert_eq!(71503, solver_part2(&parser_part2(EXAMPLE_1)));
}
#[test]
fn test_solver_part2_ana() {
assert_eq!(71503, solver_part2_ana(&parser_part2(EXAMPLE_1)));
}
}