-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
72 lines (61 loc) · 1.7 KB
/
main.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
// Problem: https://adventofcode.com/2023/day/6
type Result = usize;
type Input = (Vec<usize>, Vec<usize>);
fn parse_input(input: &str) -> Input {
let (line1, line2) = input.split_once('\n').unwrap();
let time_str = line1.split_once(':').unwrap().1;
let dist_str = line2.split_once(':').unwrap().1;
(
time_str
.split_whitespace()
.map(|s| s.parse::<usize>().unwrap())
.collect(),
dist_str
.split_whitespace()
.map(|s| s.parse::<usize>().unwrap())
.collect(),
)
}
fn race_dist(speed: usize, time: usize) -> usize {
let rem = time - speed;
rem * speed
}
fn count_winners(time: usize, dist: usize) -> usize {
(1..=time)
.filter(|speed| race_dist(*speed, time) > dist)
.count()
}
fn part1((times, dist): &Input) -> Result {
times
.iter()
.zip(dist.iter())
.map(|(time, dist)| count_winners(*time, *dist))
.product()
}
#[test]
fn test_part1() {
let input = parse_input(include_str!("test.txt"));
dbg!(&input);
assert_eq!(part1(&input), 288);
}
fn list_to_number(list: Vec<usize>) -> usize {
let s = list
.iter()
.map(|n| format!("{}", n))
.collect::<Vec<String>>()
.join("");
s.parse::<usize>().unwrap()
}
fn part2((times, dist): &Input) -> Result {
count_winners(list_to_number(times.clone()), list_to_number(dist.clone()))
}
#[test]
fn test_part2() {
let input = parse_input(include_str!("test.txt"));
assert_eq!(part2(&input), 71503);
}
fn main() {
let input = parse_input(include_str!("input.txt"));
println!("Part 1: {:?}", part1(&input));
println!("Part 2: {:?}", part2(&input));
}