-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
98 lines (83 loc) · 2.33 KB
/
lib.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
use std::fs;
pub fn part1(input_file: &str) -> usize {
parse_input(input_file).iter().map(|x| {
x.get_num_winning()
}).product()
}
pub fn part2(input_file: &str) -> usize {
parse_input_single(input_file).get_num_winning()
}
#[derive(Debug)]
struct Race {
pub t: usize,
pub d: usize,
}
impl Race {
fn is_winning_charge_time(self: &Race, c: usize) -> bool {
(self.t-c)*c > self.d
}
pub fn get_num_winning(self: &Race) -> usize {
self.last_winning() - self.first_winning() + 1
}
pub fn first_winning(self: &Race) -> usize {
let mut c = 1;
while !self.is_winning_charge_time(c) { c += 1 };
c
}
pub fn last_winning(self: &Race) -> usize {
let mut c = self.t - 1;
while !self.is_winning_charge_time(c) { c -= 1 };
c
}
}
fn parse_input(input_file: &str) -> Vec<Race> {
let mut races: Vec<Race> = vec![];
let input = fs::read_to_string(input_file)
.expect("Something went wrong reading the file");
let mut input = input.split("\n");
let times = input.next().unwrap()
.strip_prefix("Time: ").unwrap().trim();
let distances = input.next().unwrap()
.strip_prefix("Distance: ").unwrap().trim();
let times = times.split_whitespace();
let mut distances = distances.split_whitespace();
for t in times {
races.push(
Race {
t: t.parse::<usize>().unwrap(),
d: distances.next().unwrap()
.parse::<usize>().unwrap(),
}
);
}
races
}
fn parse_input_single(input_file: &str) -> Race {
let input = fs::read_to_string(input_file)
.expect("Something went wrong reading the file");
let mut input = input.split("\n");
let t = input.next().unwrap()
.strip_prefix("Time: ").unwrap()
.split_whitespace()
.collect::<String>();
let d = input.next().unwrap()
.strip_prefix("Distance: ").unwrap()
.split_whitespace()
.collect::<String>();
Race {
t: t.parse::<usize>().unwrap(),
d: d.parse::<usize>().unwrap(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test1() {
assert_eq!(part1("data/test.txt"), 288);
}
#[test]
fn test2() {
assert_eq!(part2("data/test.txt"), 71503);
}
}