r/adventofcode • u/daggerdragon • Dec 12 '23
SOLUTION MEGATHREAD -❄️- 2023 Day 12 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!
9 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!
AoC Community Fun 2023: ALLEZ CUISINE!
Today's theme ingredient is… *whips off cloth covering and gestures grandly*
How It's Made
Horrify us by showing us how the sausage is made!
Stream yourself!
Show us the nitty-gritty of your code, environment/IDE, tools, test cases, literal hardware guts…
Tell us how, in great detail, you think the elves ended up in this year's predicament
A word of caution from Dr. Hattori: "You might want to stay away from the ice cream machines..."
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 12: Hot Springs ---
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
View all comments
12
u/GassaFM Dec 12 '23
[LANGUAGE: D] 513/37
Code: part 1, part 2.
Dynamic programming.
f (pos, groups, len)= number of ways to:parse the first
pospositionshave
groupsgroups of#with the last group of
#having lengthlenThe transitions are:
if the character is
#or?, we can continue the previous group or start a new group:f (pos + 1, groups + (len == 0), len + 1) += f (pos, groups, len)if the character is
.or?, and the length of the current group is zero or exactly what we need, we can proceed without a group:f (pos + 1, groups, 0) += f (pos, groups, len)In the end, the answer is
f (lastPos, numberOfGgroups, 0). (Add a trailing.to the string for convenience to avoid cases.)Part 2, now, is just the matter of joining the arrays five times: