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
135
136
137
138
139
140
|
use {
anyhow::{
anyhow,
Context,
Result,
},
regex::{
Regex,
},
std::{
collections::HashMap,
env::args,
fs::File,
io::{
BufRead,
BufReader,
},
iter::repeat,
path::Path,
},
};
#[derive(Debug,Eq,Hash,PartialEq)]
struct Coord {
x: usize,
y: usize,
}
#[derive(Debug)]
struct Line {
start: Coord,
end: Coord,
}
impl Line {
fn coords(&self) -> Vec<Coord> {
let (iter_x, iter_y): (Box<dyn Iterator<Item=_>>, Box<dyn Iterator<Item=_>>) = (
match (self.start.x, self.end.x) {
(start, end) if start < end => Box::new(start..=end),
(start, end) if start > end => Box::new((end..=start).rev()),
(start, _end) /* equals */ => Box::new(repeat(start)),
},
match (self.start.y, self.end.y) {
(start, end) if start < end => Box::new(start..=end),
(start, end) if start > end => Box::new((end..=start).rev()),
(start, _end) /* equals */ => Box::new(repeat(start)),
},
);
iter_x.zip(iter_y).map(|(x, y)| Coord{x, y}).collect()
}
}
fn read_input<T: AsRef<Path>>(filename: T) -> Result<Vec<Line>> {
let reader = BufReader::new(File::open(filename)?);
let re = Regex::new(r#"(?x)
(?P<x1>[0-9]+),
(?P<y1>[0-9]+)[[:blank:]]*->[[:blank:]]*
(?P<x2>[0-9]+),
(?P<y2>[0-9]+)
"#)?;
reader.lines().map(|row| {
let string = row?;
let caps = re.captures(&string).ok_or_else(|| anyhow!("Could not parse: {}", string))?;
match (caps.name("x1"), caps.name("y1"), caps.name("x2"), caps.name("y2")) {
(Some(x1), Some(y1), Some(x2), Some(y2)) => {
let x = x1.as_str().parse()?;
let y = y1.as_str().parse()?;
let start = Coord{x, y};
let x = x2.as_str().parse()?;
let y = y2.as_str().parse()?;
let end = Coord {x, y};
Ok(Line{start, end})
},
_ => Err(anyhow!("Could not parse: {}", string)),
}
}).collect()
}
fn count_overlaps(points: &HashMap<Coord, usize>) -> Result<usize>{
let mut overlap = 0;
let min_x = points.keys().map(|coord| coord.x).min().ok_or(anyhow!("Problem finding min x"))?;
let max_x = points.keys().map(|coord| coord.x).max().ok_or(anyhow!("Problem finding max x"))?;
let min_y = points.keys().map(|coord| coord.y).min().ok_or(anyhow!("Problem finding min y"))?;
let max_y = points.keys().map(|coord| coord.y).max().ok_or(anyhow!("Problem finding max y"))?;
for y in min_y..=max_y {
for x in min_x..=max_x {
if let Some(v) = points.get(&Coord {x, y}) {
if *v >= 2 {
overlap += 1;
}
}
}
}
Ok(overlap)
}
fn part1<'a, I: IntoIterator<Item = &'a Line>>(input: I) -> Result<usize> {
let mut points = HashMap::new();
for line in input.into_iter().filter(|l| l.start.x == l.end.x || l.start.y == l.end.y) {
for coord in line.coords() {
let point = points.entry(coord).or_insert(0);
*point += 1;
}
}
count_overlaps(&points)
}
fn part2<'a, I: IntoIterator<Item = &'a Line>>(input: I) -> Result<usize> {
let mut points = HashMap::new();
for line in input.into_iter() {
for coord in line.coords() {
let point = points.entry(coord).or_insert(0);
*point += 1;
}
}
count_overlaps(&points)
}
fn main() -> Result<()> {
let ( do_part_1, do_part_2 ) = aoc::do_parts();
let filename = args().nth(1).ok_or(anyhow!("Missing input filename"))?;
let input = read_input(filename).context("Could not read input")?;
if do_part_1 {
let solution = part1(&input).context("No solution for part 1")?;
println!("Part1, solution found to be: {}", solution);
}
if do_part_2 {
let solution = part2(&input).context("No solution for part 2")?;
println!("Part2, solution found to be: {}", solution);
}
Ok(())
}
|