Skip to content

Instantly share code, notes, and snippets.

@neofight78
Last active December 5, 2021 10:31
Show Gist options
  • Select an option

  • Save neofight78/c3a63a6584c7c5dd63478fa3e7716104 to your computer and use it in GitHub Desktop.

Select an option

Save neofight78/c3a63a6584c7c5dd63478fa3e7716104 to your computer and use it in GitHub Desktop.
Advent of Code 2021 - Day 5: Hydrothermal Venture
struct Point {
x: i16,
y: i16,
}
struct Line {
start: Point,
end: Point,
}
fn find_points(lines: &[Line]) -> usize {
let mut map = [[0; 1000]; 1000];
lines.iter().filter(|l| l.start.x == l.end.x).for_each(|l| {
for y in i16::min(l.start.y, l.end.y)..=i16::max(l.start.y, l.end.y) {
map[l.start.x as usize][y as usize] += 1;
}
});
lines.iter().filter(|l| l.start.y == l.end.y).for_each(|l| {
for x in i16::min(l.start.x, l.end.x)..=i16::max(l.start.x, l.end.x) {
map[x as usize][l.start.y as usize] += 1;
}
});
map.iter()
.flat_map(|c| c.iter())
.filter(|&&p| p > 1)
.count()
}
fn find_points2(lines: &[Line]) -> usize {
let mut map = [[0; 1000]; 1000];
lines.iter().for_each(|l| {
let mut x = l.start.x;
let mut y = l.start.y;
let x_step = l.end.x.cmp(&l.start.x) as i16;
let y_step = l.end.y.cmp(&l.start.y) as i16;
loop {
map[x as usize][y as usize] += 1;
if x == l.end.x && y == l.end.y {
break;
}
x += x_step;
y += y_step;
}
});
map.iter()
.flat_map(|c| c.iter())
.filter(|&&p| p > 1)
.count()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment