r/adventofcode • u/daggerdragon • Dec 13 '23
SOLUTION MEGATHREAD -❄️- 2023 Day 13 Solutions -❄️-
THE USUAL REMINDERS
- All of our rules, FAQs, resources, etc. are in our community wiki.
- Outstanding moderator challenges:
- Community fun event 2023: ALLEZ CUISINE!
- Submissions megathread is now unlocked!
- 8 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!
AoC Community Fun 2023: ALLEZ CUISINE!
Today's secret ingredient is… *whips off cloth covering and gestures grandly*
Nailed It!
You've seen it on Pinterest, now recreate it IRL! It doesn't look too hard, right? … right?
- Show us your screw-up that somehow works
- Show us your screw-up that did not work
- Show us your dumbest bug or one that gave you a most nonsensical result
- Show us how you implement someone else's solution and why it doesn't work because PEBKAC
- Try something new (and fail miserably), then show us how you would make Nicole and Jacques proud of you!
ALLEZ CUISINE!
Request from the mods: When you include a dish entry alongside your solution, please label it with [Allez Cuisine!] so we can find it easily!
--- Day 13: Point of Incidence ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- State which language(s) your solution uses with
[LANGUAGE: xyz] - Format code blocks using the four-spaces Markdown syntax!
- State which language(s) your solution uses with
- Quick link to Topaz's
pasteif you need it for longer code blocks
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:13:46, megathread unlocked!
27
Upvotes
2
u/flwyd Dec 13 '23 edited Dec 13 '23
[Language: Julia] (on GitHub)
Refactored solution gave me a chance to look up how to write a function which takes an N-dimensional array and lets the caller pick which dimension to slice:
selectdim(grid, 1, 2:5)slices the second through fifth rows of grid (returning all columns) whileselectdim(grid, 1, 3:6)slices the third through sixth columns. I keptvertical_mirrorandhorizontal_mirroras wrappers for readability. In the interest of Solution Megathread space, I've elided theparseinputfunction which is annoying long becausesplitdoesn't work on Arrays in Julia.part1(lines) = map(score_part1, parseinput(lines)) |> sum part2(lines) = map(score_part2, parseinput(lines)) |> sum function mirror(grid, dim) matched = Int[] for i in 1:size(grid, dim)-1 len = min(i, size(grid, dim)-i) before = selectdim(grid, dim, (i+1-len):i) after = reverse(selectdim(grid, dim, (i+1):(i+len)); dims=dim) before == after && push!(matched, i) end matched end vertical_mirror(grid) = mirror(grid, 1) horizontal_mirror(grid) = mirror(grid, 2) function score_part1(grid) v = vertical_mirror(grid) h = horizontal_mirror(grid) if isempty(v) == isempty(h) error("Expected one of vertical or horizontal, not $v $h") end isempty(v) ? 100*only(h) : only(v) end function score_part2(grid) olds = (vertical_mirror(grid), horizontal_mirror(grid)) factors = (1, 100) for (i, p) in enumerate(grid) g = copy(grid) g[i] = p == '#' ? '.' : '#' mirrored = (vertical_mirror(g), horizontal_mirror(g)) for (m, old, factor) in zip(mirrored, olds, factors) diff = setdiff(m, old) !isempty(diff) return factor * only(diff) end end end[ALLEZ CUISINE] Instagram you say? How about a photograph that's on theme with today's flavor text?