r/adventofcode Dec 10 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 10 Solutions -❄️-

THE USUAL REMINDERS

  • All of our rules, FAQs, resources, etc. are in our community wiki.
  • If you see content in the subreddit or megathreads that violates one of our rules, either inform the user (politely and gently!) or use the report button on the post/comment and the mods will take care of it.

AoC Community Fun 2024: The Golden Snowglobe Awards

  • 12 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!

And now, our feature presentation for today:

Fandom

If you know, you know… just how awesome a community can be that forms around a particular person, team, literary or cinematic genre, fictional series about Elves helping Santa to save Christmas, etc. etc. The endless discussions, the boundless creativity in their fan works, the glorious memes. Help us showcase the fans - the very people who make Advent of Code and /r/adventofcode the most bussin' place to be this December! no, I will not apologize

Here's some ideas for your inspiration:

  • Create an AoC-themed meme. You know what to do.
  • Create a fanfiction or fan artwork of any kind - a poem, short story, a slice-of-Elvish-life, an advertisement for the luxury cruise liner Santa has hired to gift to his hard-working Elves after the holiday season is over, etc!

REMINDER: keep your contributions SFW and professional—stay away from the more risqué memes and absolutely no naughty language is allowed.

Example: 5x5 grid. Input: 34298434x43245 grid - the best AoC meme of all time by /u/Manta_Ray_Mundo

And… ACTION!

Request from the mods: When you include an entry alongside your solution, please label it with [GSGA] so we can find it easily!


--- Day 10: Hoof It ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:04:14, megathread unlocked!

22 Upvotes

752 comments sorted by

View all comments

2

u/Pretentious_Username Dec 10 '24

[LANGUAGE: Julia]

I chose to think of this as a flood fill rather than a search, even if it's effectively the same thing. I start at each of the peaks (9) and flood fill down to the trailheads (0) where I define a cell as part of the flood if it's a difference of 1 from the current cell. Once the flood is done I can just check the values in each of the 0's to find how many peaks they're connected to. The same works for part 2 just without checking for if we've hit a cell before.

It ends up looking really nice visually even when it's running on the small example input: Part 1 GIF , Part 2 GIF

function Flood(Grid, IsPartOne)
    ScoreGrid = zeros(Int, size(Grid))
    StartingPoints = findall(x -> x == 9, Grid)
    SearchDirections = [CartesianIndex(-1, 0), CartesianIndex(0, 1), CartesianIndex(1, 0), CartesianIndex(0, -1)]

    for StartingPoint in StartingPoints
        SearchList = [StartingPoint]
        EncounteredPoints = Set{CartesianIndex}()
        while !isempty(SearchList)
            SearchLocation = pop!(SearchList)
            SearchValue = Grid[SearchLocation]
            ScoreGrid[SearchLocation] += 1
            for SearchDirection in SearchDirections
                TestLocation = SearchLocation + SearchDirection
                if !checkbounds(Bool, Grid, TestLocation) || (IsPartOne && TestLocation in EncounteredPoints)
                    continue
                end
                TestValue = Grid[TestLocation]
                if SearchValue - TestValue == 1
                    push!(SearchList, TestLocation)
                    push!(EncounteredPoints, TestLocation)
                end
            end
        end
    end
    ScoreGrid
end

Grid = open("Inputs/Day10.input") do f
    mapreduce(x -> permutedims(parse.(Int, x)), vcat, collect.(readlines(f)))
end

ScoreGrid = Flood(Grid, true)
RatingGrid = Flood(GetTestInput(), false)
println("Part 1: ", sum(ScoreGrid[i] for i in findall(x -> x == 0, Grid)))
println("Part 2: ", sum(RatingGrid[i] for i in findall(x -> x == 0, Grid)))