commit cdeecb07c7c3a9194aafef271d63047008f23d13
parent 52dd2774d7008095a7e7c39048e874e694ff6153
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date: Thu, 8 Dec 2022 07:31:04 -0800
Solution to day 8, part 1.
Diffstat:
A | src/day_8.jl | | | 58 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 58 insertions(+), 0 deletions(-)
diff --git a/src/day_8.jl b/src/day_8.jl
@@ -0,0 +1,58 @@
+#!/usr/bin/env julia
+# https://adventofcode.com/2022/day/8
+using AdventOfCode
+
+example = readlines(IOBuffer("""
+ 30373
+ 25512
+ 65332
+ 33549
+ 35390"""))
+input = readlines("data/day_8.txt")
+
+# Just a helper to turn an array of strings to a 2D int matrix
+function parse_input(input)
+ parse.(Int, hcat(split.(input, "")...))
+end
+
+# Returns an array of the same length as x that's true for every element in x
+# that's greater than the elements visited so far. Sad I couldn't figure out
+# how to do this cleanly with accumulate()
+function cumismax(x)
+ currmax = -1
+ result = similar(x, Bool)
+ for (i, v) = enumerate(x)
+ if v > currmax
+ currmax = v
+ result[i] = true
+ else
+ result[i] = false
+ end
+ end
+ result
+end
+
+# Return the tallest trees
+function visible_here(trees, dims, doreverse)
+ trees = doreverse ? reverse(trees, dims = dims) : trees
+ istallest = mapslices(cumismax, trees, dims = dims)
+ doreverse ? reverse(istallest, dims = dims) : istallest
+end
+
+function visible_anywhere(trees)
+ visible_here(trees, 1, false) .|
+ visible_here(trees, 1, true) .|
+ visible_here(trees, 2, false) .|
+ visible_here(trees, 2, true)
+end
+
+function part_1(input)
+ sum(visible_anywhere(parse_input(input)))
+end
+@assert part_1(example) == 21
+@info part_1(input)
+
+function part_2(input)
+ nothing
+end
+@info part_2(input)