aboutsummaryrefslogtreecommitdiff
path: root/2022/08/a.rs
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-06-26 09:47:23 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2025-06-26 23:37:23 +0200
commitfa26f694d8cf98272763952a698301ee9935c19e (patch)
tree5af7a788fb8e9fa12b2b2fbea64250ed496a23c0 /2022/08/a.rs
parentf0159b7d36e2c81182f2d047a34bf883394894db (diff)
downloadaoc-fa26f694d8cf98272763952a698301ee9935c19e.tar.gz
aoc-fa26f694d8cf98272763952a698301ee9935c19e.zip
Added 2022, first few problems
Diffstat (limited to '2022/08/a.rs')
-rw-r--r--2022/08/a.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/2022/08/a.rs b/2022/08/a.rs
new file mode 100644
index 0000000..b145486
--- /dev/null
+++ b/2022/08/a.rs
@@ -0,0 +1,42 @@
1use std::cmp::max;
2mod common;
3use common::*;
4
5fn mark_visible(grid: &mut Grid) {
6 let update = |elem: &mut (i8, bool), m: &mut i8| {
7 elem.1 = elem.1 || elem.0 > *m;
8 *m = max(*m, elem.0);
9 };
10
11 for row in &mut *grid {
12 // From left
13 let mut m = -1;
14 for t in &mut *row { update(t, &mut m); }
15
16 // From right
17 let mut m = -1;
18 for t in &mut row.iter_mut().rev() { update(t, &mut m); }
19 }
20
21 for j in 0..grid[0].len() {
22 // From top
23 let mut m = -1;
24 for i in 0..grid.len() { update(&mut grid[i][j], &mut m); }
25
26 // From bottom
27 let mut m = -1;
28 for i in (0..grid.len()).rev() { update(&mut grid[i][j], &mut m); }
29 }
30}
31
32fn count_visible(grid: &Grid) -> usize {
33 grid.iter()
34 .map(|row| row.iter().filter(|c| c.1).count())
35 .sum()
36}
37
38fn main() {
39 let mut grid = read_grid_from_stdin();
40 mark_visible(&mut grid);
41 println!("{}", count_visible(&grid));
42}

Generated with cgit - Back to sebastiano.tronto.net