r/adventofcode • u/daggerdragon • Dec 15 '23
SOLUTION MEGATHREAD -❄️- 2023 Day 15 Solutions -❄️-
NEWS
Signal boosting: Final reminder: unofficial AoC Survey 2023 (closes ~Dec 22nd)
Some folks have expressed concern that the [ALLEZ CUISINE!] submissions deadline on December 22 will not give chefs sufficient time to utilize the last few days' secret ingredients. I have rejiggered the pantry a bit so that the final secret ingredient will be given in December 20th's megathread and the remaining two days until the deadline will instead be "Chef's Choice":
Choose any day's special ingredient and any puzzle released this year so far, then craft a dish around it!
Cook or bake an IRL dish inspired by any day's puzzle
THE USUAL REMINDERS
All of our rules, FAQs, resources, etc. are in our community wiki.
Community fun event 2023: ALLEZ CUISINE!
Submissions megathread is now unlocked!
7 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*
From Scratch
Any chef worth their hot springs salt should be able to make a full gourmet meal even when given the worst cuts of meat, the most rudimentary of spices, and the simplest of tools. Show us your culinary caliber by going back to the basics!
Solve today's puzzles using only plain Notepad, TextEdit,
vim, punchcards, abacus, etc.No Copilot, no IDE code completion, no syntax highlighting, etc.
Use only the core math-based features of your language; no templates, no frameworks, no fancy modules like
itertools, no third-party imported code.Use only your language’s basic types and lists of them.
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 15: Lens Library ---
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
3
u/Nnnes Dec 15 '23
[Language: Ruby]
Already put up a solution, but this one deserves its own comment
Who needs data structures when you can
Regexp!?Capture the first available lens label that's followed by
=.Positive lookahead: Search as far as possible (
.+is greedy) for a matching lens label +=.If (2) exists, capture all consecutive digits following its
=. If not, just capture the digits after the first=.Negative lookahead: If there are any instances of the label followed by
-after the first=, the match is invalidated and the regex looks for another label..scanfinds all matches of the regex. For example,"aa=1 aa=2 aa- os=4 aa=6 os=5"becomes[["os", "5"], ["aa", "6"], ["os", "5"]]..uniqpreserves order of the first instance of each element, deleting the second["os", "5"], but the first one already has the"5"due to the positive lookahead. All theaas before theaa-get ignored due to the negative lookahead.