-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution_2021_05.rs
133 lines (111 loc) · 3.16 KB
/
solution_2021_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
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
use std::str::FromStr;
use advent_of_code_common::coords2d::Coords2D;
use advent_of_code_common::parsing::Error;
use itertools::Itertools;
#[derive(Debug)]
struct Vent {
from: Coords2D<usize>,
to: Coords2D<usize>,
}
impl Vent {
fn from_to(from: usize, to: usize) -> Vec<usize> {
if from < to {
(from ..= to).collect()
} else {
(to ..= from).rev().collect()
}
}
fn all_points(&self) -> Vec<Coords2D<usize>> {
if self.from.x == self.to.x {
Self::from_to(self.from.y, self.to.y)
.iter()
.map(|y| Coords2D::new(self.from.x, *y))
.collect()
} else if self.from.y == self.to.y {
Self::from_to(self.from.x, self.to.x)
.iter()
.map(|x| Coords2D::new(*x, self.from.y))
.collect()
} else {
Self::from_to(self.from.x, self.to.x)
.iter()
.zip(Self::from_to(self.from.y, self.to.y).iter())
.map(|(x, y)| Coords2D::new(*x, *y))
.collect()
}
}
fn horizontal_or_vertical(&self) -> bool {
self.from.x == self.to.x || self.from.y == self.to.y
}
fn diagonal(&self) -> bool {
self.to.x.abs_diff(self.from.x) == self.to.y.abs_diff(self.from.y)
}
fn horizontal_or_vertical_or_diagonal(&self) -> bool {
self.horizontal_or_vertical() || self.diagonal()
}
}
impl FromStr for Vent {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let v: Vec<_> = s.split(" -> ").collect();
if v.len() == 2 {
let from = Coords2D::from_str(v[0])?;
let to = Coords2D::from_str(v[1])?;
Ok(Vent { from, to })
} else {
Err(format!("Invalid vent {s}").to_string())
}
}
}
fn parse(input: &str) -> Result<Vec<Vent>, Error> {
input.lines().map(Vent::from_str).collect()
}
fn solve<F>(vents: Vec<Vent>, valid: F) -> usize
where
F: Fn(&Vent) -> bool,
{
vents
.into_iter()
.filter(valid)
.flat_map(|x| x.all_points())
.counts()
.into_iter()
.filter(|(_coord, times_seen)| *times_seen > 1)
.count()
}
fn part_1(input: &str) -> Result<usize, Error> {
let vents = parse(input)?;
Ok(solve(vents, Vent::horizontal_or_vertical))
}
fn part_2(input: &str) -> Result<usize, Error> {
let vents = parse(input)?;
Ok(solve(vents, Vent::horizontal_or_vertical_or_diagonal))
}
const DATA: &str = include_str!("../../resources/05.txt");
fn main() {
let result_1 = part_1(DATA);
println!("Part 1: {result_1:?}");
let result_2 = part_2(DATA);
println!("Part 2: {result_2:?}");
}
#[cfg(test)]
mod tests {
use super::*;
const TEST: &str = include_str!("../../resources/05-test.txt");
#[test]
fn test_part_1() {
assert_eq!(part_1(TEST), Ok(5));
}
#[test]
fn real_part_1() {
assert_eq!(part_1(DATA), Ok(5147));
}
#[test]
fn test_part_2() {
assert_eq!(part_2(TEST), Ok(12));
}
#[test]
fn real_part_2() {
assert_eq!(part_2(DATA), Ok(16925));
}
}