advent_of_code_2022

My (attempted) solutions to the 2022 Advent of Code
git clone https://git.eamoncaddigan.net/advent_of_code_2022.git
Log | Files | Refs | README

commit 01de598936366d6e7cc24cad17f4522704790462
parent 3124dadd09fe9eedf3d9eab85596ceb4dc99c74d
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date:   Thu,  8 Dec 2022 10:43:50 -0800

Just making my code cuter.

Ported over some stuff (specifically, Iterators.product() and mapreduce()) that
I learned about working part 2 to my part 1 code. I can't claim it's more
readable, and I haven't looked to see if it runs faster or anything.

Diffstat:
Msrc/day_8.jl | 16++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/day_8.jl b/src/day_8.jl @@ -32,18 +32,22 @@ function cumismax(x) result end -# Return the tallest trees +# Return the tallest trees when viewed from one direction function visible_here(trees, dims, doreverse) - trees = doreverse ? reverse(trees, dims = dims) : trees + if doreverse + trees = reverse(trees, dims = dims) + end istallest = mapslices(cumismax, trees, dims = dims) doreverse ? reverse(istallest, dims = dims) : istallest end +# Return the trees visible from any direction function visible_anywhere(trees) - visible_here(trees, 1, false) .| - visible_here(trees, 1, true) .| - visible_here(trees, 2, false) .| - visible_here(trees, 2, true) + mapreduce( + (x)->visible_here(trees, x[1], x[2]), + (x, y)->x .| y, + Iterators.product((1, 2), (false, true)) + ) end function part_1(input)