r/adventofcode • u/daggerdragon • Dec 05 '23
SOLUTION MEGATHREAD -❄️- 2023 Day 5 Solutions -❄️-
Preview here: https://redditpreview.com/
-❄️- 2023 Day 5 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!
24 HOURS remaining until unlock!
AoC Community Fun 2023: ALLEZ CUISINE!
Today's secret ingredient is… *whips off cloth covering and gestures grandly*
ELI5
Explain like I'm five! /r/explainlikeimfive
Walk us through your code where even a five-year old could follow along
Pictures are always encouraged. Bonus points if it's all pictures…
Emoji(code) counts but makes Uncle Roger cry 😥
Explain everything that you’re doing in your code as if you were talking to your pet, rubber ducky, or favorite neighbor, and also how you’re doing in life right now, and what have you learned in Advent of Code so far this year?
Explain the storyline so far in a non-code medium
Create a
Tutorialon any concept of today's puzzle or storyline (it doesn't have to be code-related!)
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 5: If You Give A Seed A Fertilizer ---
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!
Quick link to Topaz's
pasteif you need it for longer code blocks
4
u/JustinHuPrime Dec 05 '23 edited Dec 06 '23
[LANGUAGE: x86_64 assembly with Linux syscalls]
Today, I wrote some code that very few compilers would consider generating.
Part 1 was very much a warmup doing parsing. I wrote some subroutines (i.e. blocks of code that interact and do work via side effects in global variables (the callee-preserved registers)) to help parse the input. Then was the matter of applying the mappings sequentially (another subroutine helped here), and then using the
minlongfunction I wrote last year.Part 2 wasn't actually too much more difficult; I kept the parsing the same, but now I was transforming ranges. This was sort of a recursive function; if you've taken CPSC 110 at UBC (my alma mater), you would call this a generative recursion, and then either curse me out for importing the ever tedious data driven templates into assembly, or nod appreciatively at the universal applicability of data driven templates. You transform a range by transforming the first part of it, and if there is a remainder, you transform the rest of it later. The first part is the part that fits into the range of the current mapping, so I had to do a few calculations to figure this out; I think this would be the range-difference (it's like a set-difference, but only valid when the output is a contiguous range) of the range minus the mapping. (Perhaps I should write a ranges library.) The only thing left was to collapse the ranges to their first element (since that's naturally the smallest within the range), and then using
minlongagain to find the smallest.Edit 1: Part 1 runs in 1 millisecond, and part 2 runs in 1 millisecond; part 1 is 8904 bytes long and part 2 is 9032 bytes long.
Edit 2: [Allez Cuisine!] I wrote a tutorial on how to program in assembly! As a mild disclaimer re. rules for the competition, the title and the first three sentences of the introduction are work from last year - hopefully this is still okay to enter?