summaryrefslogtreecommitdiff
path: root/aoc22/day8/src
diff options
context:
space:
mode:
authormathiasmagnusson <mathias@magnusson.space>2022-12-11 23:04:39 +0100
committermathiasmagnusson <mathias@magnusson.space>2022-12-11 23:04:39 +0100
commitb7e2fe6160280472a625a8867ea5177b0155737c (patch)
treef8b10bd9a20adbd568b3a0d75fc0367a0523107f /aoc22/day8/src
parentac1f723d39b6d25903237af86a7319209373731b (diff)
downloadprogramming-problem-solving-b7e2fe6160280472a625a8867ea5177b0155737c.tar.gz
Aoc day 6, 7 & 8
Diffstat (limited to 'aoc22/day8/src')
-rw-r--r--aoc22/day8/src/main.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/aoc22/day8/src/main.rs b/aoc22/day8/src/main.rs
new file mode 100644
index 0000000..f73543d
--- /dev/null
+++ b/aoc22/day8/src/main.rs
@@ -0,0 +1,46 @@
+fn main() {
+ let input = lib::read_input(8);
+
+ part1(&input);
+ part2(&input);
+}
+
+fn part1(input: &str) {
+ let input = input.split_whitespace().map(|x| x.bytes().collect::<Vec<_>>()).collect::<Vec<_>>();
+
+ let mut ans = 0;
+ for (y, line) in input.iter().enumerate() {
+ for (x, c) in line.iter().enumerate() {
+ if visible(&input, x, y) {
+ ans += 1;
+ }
+ }
+ }
+ println!("{}", ans);
+}
+
+fn part2(input: &str) {
+}
+
+fn visible(heights: &[Vec<u8>], x: usize, y: usize) -> bool {
+ for (dy, dx) in &[(0, 1), (0, -1), (1, 0), (-1, 0)] {
+ let height = heights[y][x];
+
+ let mut sx = x as i32;
+ let mut sy = y as i32;
+
+ loop {
+ sx += dx;
+ sy += dy;
+
+ if sx < 0 || sy < 0 || sx >= heights[0].len() as i32 || sy >= heights.len() as i32 {
+ return true;
+ }
+
+ if heights[sy as usize][sx as usize] >= height {
+ break;
+ }
+ }
+ }
+ return false;
+}