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 e38ebb7fcfc7a00fba183c1d6b178a0da0bc1fc6
parent 01de598936366d6e7cc24cad17f4522704790462
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date:   Thu,  8 Dec 2022 20:14:50 -0800

Making the code a bit more compact and idiomatic.

Specifically I didn't realize (until I looked at the manual), that
nested loops could be combined without relying on Iterators.product, and
I'm relying on the short-circuiting behavior of && since that's
apparently (again, according to the manual) something that Julia
programmers do.

Diffstat:
Msrc/day_8.jl | 13++++---------
1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/src/day_8.jl b/src/day_8.jl @@ -34,9 +34,7 @@ end # Return the tallest trees when viewed from one direction function visible_here(trees, dims, doreverse) - if doreverse - trees = reverse(trees, dims = dims) - end + doreverse && (trees = reverse(trees, dims = dims)) istallest = mapslices(cumismax, trees, dims = dims) doreverse ? reverse(istallest, dims = dims) : istallest end @@ -59,18 +57,15 @@ end # Find the component of a scenic score looking along one direction function score_component(treeheight::Int, neighbortrees::Vector{Int})::Int trees_seen = findfirst(x->x>=treeheight, neighbortrees) - if trees_seen == nothing - trees_seen = length(neighbortrees) - end - trees_seen + trees_seen == nothing ? length(neighbortrees) : trees_seen end function part_2(input) trees = parse_input(input) mapheight, mapwidth = size(trees) - scenicscore = fill(0, (mapheight, mapwidth)) + scenicscore = zeros(Int, mapheight, mapwidth) - for (row, col) = Iterators.product(2:mapheight-1, 2:mapwidth-1) + for row = 2:mapheight-1, col = 2:mapwidth-1 scenicscore[row, col] = mapreduce( Base.Fix1(score_component, trees[row, col]), *,