-
Notifications
You must be signed in to change notification settings - Fork 0
/
05.rs
96 lines (87 loc) · 2.57 KB
/
05.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
use std::collections::HashMap;
fn main() {
let input = parse_input(include_str!("../../input/05.txt"));
println!("Part 1: {}", part_1(input.clone()));
println!("Part 2: {}", part_2(input));
}
fn parse_input(input: &str) -> Vec<((i64, i64), (i64, i64))> {
input
.lines()
.map(|line| {
let (one, two) = line.split_once("->").unwrap();
let (x1, y1) = one.trim().split_once(',').unwrap();
let (x2, y2) = two.trim().split_once(',').unwrap();
let from = (x1.parse::<i64>().unwrap(), y1.parse::<i64>().unwrap());
let to = (x2.parse::<i64>().unwrap(), y2.parse::<i64>().unwrap());
if to < from {
(to, from)
} else {
(from, to)
}
})
.collect()
}
fn part_1(lines: Vec<((i64, i64), (i64, i64))>) -> usize {
count_points_covered(
lines
.iter()
.filter(|((x1, y1), (x2, y2))| y1 == y2 || x1 == x2)
.copied()
.collect(),
)
}
fn part_2(lines: Vec<((i64, i64), (i64, i64))>) -> usize {
count_points_covered(lines)
}
fn count_points_covered(lines: Vec<((i64, i64), (i64, i64))>) -> usize {
let mut points_covered = HashMap::<_, usize>::new();
for ((x1, y1), (x2, y2)) in lines {
if y1 == y2 {
for x in x1..=x2 {
*points_covered.entry((x, y1)).or_default() += 1;
}
} else if x1 == x2 {
for y in y1..=y2 {
*points_covered.entry((x1, y)).or_default() += 1;
}
} else if y1 < y2 {
for delta in 0..=(x2 - x1) {
*points_covered.entry((x1 + delta, y1 + delta)).or_default() += 1;
}
} else {
for delta in 0..=(x2 - x1) {
*points_covered.entry((x1 + delta, y1 - delta)).or_default() += 1;
}
}
}
points_covered
.into_iter()
.filter_map(|(_, num_lines)| Some(num_lines).filter(|&x| x >= 2))
.count()
}
#[cfg(test)]
mod day_5_tests {
use super::*;
static INPUT: &str = "0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2";
#[test]
fn test_small_input() {
let input = parse_input(INPUT);
assert_eq!(part_1(input.clone()), 5);
assert_eq!(part_2(input), 12);
}
#[test]
fn test_large_input() {
let input = parse_input(include_str!("../../input/05.txt"));
assert_eq!(part_1(input.clone()), 7297);
assert_eq!(part_2(input), 21038);
}
}