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
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:22:57, megathread unlocked!
47
Upvotes
13
u/kaa-the-wise Dec 12 '23 edited Dec 12 '23
[Language: Python] one-line/single-expression solutions
Nothing too fancy:
functools.cacheandre.match.https://github.com/kaathewise/aoc2023/blob/main/12.py
3
u/Singing-In-The-Storm Dec 18 '23 edited Dec 19 '23
[LANGUAGE: JavaScript]
Part 1 (40ms)
Part2 (75ms)
No recursion.
No caching.
code on github
My theory:
This is the basic/rough idea. You can learn more reading the code. It is written in a didactic style, part of my Blazingly Fast JavaScript solutions for AOC series.
4
u/Domy__ Dec 17 '23
[LANGUAGE: Python]
https://github.com/Domyy95/Challenges/blob/master/2023-12-Advent-of-code/12.py
2
u/scheurneus Dec 12 '23
[LANGUAGE: Haskell]
My solution
I went for the fairly simple recursive approach, and memoized it for part 2. Haskell memoization is always a little ugly (IMO) because you have to initialize a data structure right at the start (you can't really insert to it because of immutability, and have to preconstruct it with lazily evaluated thunks instead), and I decided to store Ints describing which sublist I'm using. This also gave me some hangs at first because I forgot to unmemoize a call that used a too similar sublist.
Parsing and unfolding are pretty trivial, luckily.
Also, what are the xxx/yyy numbers people post? Their position on the global leaderboard? If so, (855/519). Not bad considering I needed a bit of thought to convert to memoization!
2
u/Jadarma Dec 12 '23
[LANGUAGE: Kotlin]
Today's puzzle was very hard to think about a good way to start it, but it was clear from the get go I shouldn't try to brute force it. The solution (for me) was a "simple" recursive function with a state cache.
This is one of those algorithms that only make sense when you have it fresh in your mind, so I made an exception from my usual practice and added lots of comments to remind me why it works the way it does.
AocKt Y2023D12
2
u/Mabi19_ Dec 12 '23 edited Dec 12 '23
[LANGUAGE: Rust]
Most solutions that I have seen used dynamic programming or recursion. I'm terrible at thinking up dynamic programming solutions to problems, so I decided to dance around with propagating partial solutions forward.
I'm still learning how to Rust, so this may not be very idiomatic, but I still think it's neat.
Solution
2
u/FlipperBumperKickout Dec 17 '23 edited Jan 24 '24
[LANGUAGE: C#]
Aaargh, I'm getting farther and farther behind.
Anyway. To solve the problem I recursively counted the arrangements by solving the same problem again minus the spring which placement have been decided and the location that spring takes up.
To make Part2 work I cached all the results of the partial problems.
https://github.com/lumikrynd/adventofcode/blob/main/2023/Day12/ArrangementsCounter.cs22
u/Nathanfenner Dec 12 '23 edited Dec 12 '23
[LANGUAGE: TypeScript] solution + helpers (18/1)
This isn't actually the first time I've seen a nonogram/picross-solving problem in a coding contest, so it brought back some memories from ~6 years ago. I went straight for dynamic programming since it seemed simpler to me than brute force, and I also figured part 2 was going to do something that made brute force too slow.
As a result, I didn't have to change my code at all for part 2, besides 5xing the input strings! That made for a very speedy turnaround.
There are a few things that worked out conveniently:
if (line[run] === "#")works fine in the "exactly-the-right-length" caseline.length < sum(runs) + runs.length - 1early means checking for whether there's "enough room" to fit all of the runs - if not, a few annoying case go away so you don't have to think about them anymore (e.g. the string must have at least as many characters as the first run length number)JSON.stringify()s all of the arguments. That means it works fine for e.g.number[]arguments too, without having to think too hardThe solution ends up being O(n3) in the worst case, and usually much faster - if you're quite careful, you can write a worst-case O(n2) solution that follows the same basic structure but avoids copying things and caches some data about where long runs of
.and#are. I suspect there's a more-sophisticated algorithm that can get down to linearithmic time, but don't see an obvious way to come up with the details at the moment.17
u/Usually_Works_Fine89 Dec 12 '23 edited Jan 25 '24
[Language: Go]
I've been doing these for a bit, but this is the first time I've posted, mostly because I'm kinda proud of these:
12-1.go
12-2.go
There's no recursion, no backtracking, no memo-isation or any sort of fancy optimisation involved at all; and it runs in (FAICT; I was never very good at theory) O(n). [NOTE: part about runtime removed, see bottom] Not very hand-optimised yet, but meh. The heart of it is a ~35 line go function,
func countPossible(), which is basically a loop over a state machine that stores a list of parallel states and processes them step-by-step as a Non-Deterministic Finite Automata.The entire approach was inspired by a beautiful seed of an idea implanted in my head long ago by this excellent set of old articles describing a very similar situation:
https://swtch.com/~rsc/regexp/regexp1.html
https://research.swtch.com/glob
As the above article elaborates on with its examples, and I'd like to think I demonstrated with this code, "Good theory leads to good programs". The runtime complexity, far as I can tell unchallenged, is all thanks to the venerable Thompson NFA (by Ken Thompson himself).
I'm glad this was the first approach I had. Although I very quickly noticed that the original version for P1 (which did not deduplicate states) was quickly using up like 20gigs of memory and had like 50-100k copies of one state at the same time (yeah, unlike several other solutions my P2 blocker was memory, not runtime), I will admit it took me an embarrassing number of refactors to get to the syntactical elegance of using maps to deduplicate states, using Go's helpfully comparable arrays.
UPDATE: I was using hyperfine to measure it usinghyperfine -i 'cat input | ./12-2'~~. Turns out that that's a lotta overhead with shell spawning and process forking, and actually sending the input using hyperfine's~~--inputthe runtime is less than 4ms, on the order of ~140μs (microseconds) (and fluctuates a lot as expected on those miniscule values).ERRATA: Well this is embarassing. NVM, I'm an idiot, and didn't know how to use hyperfine. It couldn't even find the executable when I prepended its path with
./(I'm testing on windows so this is probably a weird cross-platform edge case), so it was measuring the startup time of launching a shell that errors out immediately 💀 The tool gave me no indication that this is so, and instead just told me about the exit status of the program being non-zero (in reality it was the shell whose exit status was non-zero because it couldn't find the program, which makes sense in hindsight). The actual runtime of my code on my pc currently is 60 milliseconds, 20ms for a trivially multithread version (just wrapping all work for a line intogo func()), which doesn't sound nearly as impressive as 4ms or μs. I should've realised something was off from the start because Go, while pretty fast, isn't a "pure speed" language to begin with for a variety of reasons including the GC overhead, especially for tiny short-lived programs. I also did no non-trivial optimisations, of which I'm sure a few can still shave off a couple ms more. Those numbers might've been more believable if it were, say, C or rust. Guess I learnt never to trust a too-good result, and triple-check everything for stupid mistakes like this.All that said, I still stand behind the theoretical stability of this solution. It's still linear in time complexity and <quadratic in space complexity. I do think I'll try reimplementing it in a more "zero-cost" language and see how much better it fares, just out of academic curiosity.
13
u/kaa-the-wise Dec 12 '23 edited Dec 13 '23
[Language: Uiua]
Continuing with Uiua today.
Link to pad
The general structure is as follows:
.##.?.#?#.into two masks, one where#could be, and one where.could be, i.e.0 1 1 0 1 0 1 1 1 0and1 0 0 1 1 1 0 1 0 1.#mask we calculate for each position, how long we can have a spring ending at this position, i.e.0 1 2 0 1 0 1 2 3 0..mask to generate a matrix where(i,j)is the truthfulness of the statement "there is a contiguous sequence of.?s fromitoj".Then we fold over the list of the springs, maintaining a vector, where position
ianswers the question "how many ways to fill the space up toiwith the previous springs and empty space, such thatiis empty space".1 0 0 0 0 0 0 0 0 0.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:pospositionsgroupsgroups of##having lengthlenThe transitions are:
#or?, we can continue the previous group or start a new group:f (pos + 1, groups + (len == 0), len + 1) += f (pos, groups, len).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:
11
u/4HbQ Dec 12 '23 edited Dec 14 '23
[LANGUAGE: Python] Code (16 lines)
Basic DP solution today. Thanks to /u/pred for their . trick!
Today's Python hack: use try/except so you don't have to manually check if index values are in range.
11
u/Diderikdm Dec 12 '23 edited Dec 12 '23
[LANGUAGE: Python]
9
u/yfilipov Dec 12 '23
[Language: C#]
Similar to another solution below, caching is awesome! :)
https://pastebin.com/djb8RJ85
8
u/AndreiaCo Dec 13 '23 edited Dec 14 '23
[Language: OCaml]
Not that many OCaml solutions around here, so this is mine. Not really functional, it runs in around 115 milliseconds (there are some potential simple optimisations, including memoization, but I can't be bothered :) ).
This smelled like dynamic programming from the get-go (anything with counting possible arrangements does), but it took me a while to understand how to formulate it as such.
Here is an example for
???.###. 1,1,3Each cell represents the number of possible arrangements by matching the groups on that row, with the substring starting at that column and continuing until the end. For example
dyn[2][1] := 2because there are 2 ways to match1,3with??.###.. The first row that matches against no groups might be a surprise, and it does not affect this particular example, but it is important for other strings. Also, the last column represents the empty string.As for how to compute the table, there are three cases for each cell:
where
current_groupis the first number from each group, in each row (e.g. on row 3 or 4,current_groupwould be 1). So, let's look atdyn[1][4]. Because we are under a'#'and the current group (i.e. 3) can match with the current substring (i.e."###."), we know this is a potential arrangement, and all we need to know now is how many potential arrangements there are with the previous groups using the remaining substring. So we look atdyn[0][4 + 3 + 1], and we get 1. If we are under a'.', we just copy the previous number of arrangements (dots don't really do anything, they just separate numbers). If we are under a'?', we compute the value as if the question mark is a period, then as if it's a pound symbol, and we add them up.The first row is computed manually, at the start, filling it with 1s, from right to left, until you reach the first
#. The last column is 0 for all but the first row.There are more details to my implementation, but this is the gist of the algorithm.
EDIT: correct table & add base case
8
u/blekpul Dec 14 '23
[Language: Python]
Solved using automata theory, I drew the NFA for one pattern and implemented it into code.
Part 2 runs in 145ms on my machine, no libraries imported.
https://github.com/clrfl/AdventOfCode2023/tree/master/12
8
u/koopatroopa125 Dec 25 '23 edited Dec 25 '23
[LANGUAGE: C++]
I feel like I solved day 12 in a somewhat unique way. No recursion or anything.
Basically I turned the broken pipe patter into a finite state machine. So `1,2,3` would turn into a state machine matching the regex `.*#.+##.+###.*` (the `.` represent the litteral `.` as opposed to the typical wild card)
Then as I traverse the blueprint, I keep track of a map between a given state, and how many paths are in the state. Then for each character, I use the state machine to generate the next map, while making sure to split whenever I see a `?`
At the end, I just get the number of paths that reached the final "accept" state of the state machine. This should represent how many different combinations are valid.
I have a full writeup for my solution here: https://alexoxorn.github.io/posts/aoc-day12-regular_languages/
Keep in mind, this writeup assumes no knowledge of AoC, so the first half is mostly just an introduction to the problem.
And for my code: https://github.com/AlexOxorn/AdventOfCode/blob/main/puzzles/2023/src/day12.cpp
7
u/morgoth1145 Dec 12 '23 edited Dec 13 '23
[LANGUAGE: Python 3] 433/104 Raw solution
This reminds me of Picross, and I once wrote a Picross solver. In fact, I did this exact same thing when generating candidate patterns for the Picross rows/columns. Fundamentally the generator isn't too complex a recursive function, but I definitely took way too long to write this. My primary issue is that in my base case (the remaining splits being empty) I was returning instead of yielding. Python allows this but there are some weird results, and it took me I'm not sure how long to find that bug...
Part 2 was not the twist I was expecting, I expected to need to do something with the generated patterns. Still though, not too bad, we just need to return the counts instead of patterns, as well as filtering the pattern options during generation. That said, I did goof by just repeating the template pattern/record 5 times instead of joining them with question marks. Lost ~25-30 ranks as a result as I found the bug...
Honestly my ranking is a little embarrassing having written a Picross solver before, but such is life.
Cleaned up code. I may make another optimization pass, we'll see...
Edit: Found and uploaded my picross solver for anybody interested. (Edit on the edit: Now that I think about it, a pretty hype part 2 would have been a twist where the input actually overlaid on top of itself somehow to generate an image that spelled out a number. If only...)
Edit 2: Optimized code. I was thinking that using bitwise operations instead of string operations might make it even faster than this, but not really. (And it's a lot more complicated!)
7
u/pred Dec 12 '23 edited Dec 12 '23
[LANGUAGE: Python] GitHub
Yeah, dynamic programming, counting solutions by looking at one character at a time, extending in all possible ways (a "?" can become both a "." or a "#", a "#" can only be used to extend the current group, and a "." can be used to close a group or as a noop).
It's possible to prune the search space a good deal by e.g. checking if there are groups enough left to make a full solution, but that turns out not to be necessary.
One small trick is to append a "." to each input to make it easier to check when we reach EOL.
13
u/jonathan_paulson Dec 12 '23
[LANGUAGE: Python 3] 21/13. Solution. Video.
I did brute force for part 1 and dynamic programming for part 2. The state space is (which character you're on, which block you're on, how long your current block is)
6
u/AllanTaylor314 Dec 12 '23
[LANGUAGE: Python] 258/1055
Code: main (a757427)
Part 1: First thought was "nonograms". Made a dumb recursive string generator and a separate validator. Generated all the strings and counted how many were valid. This obviously doesn't scale well and takes about 10 seconds to run.
Part 2: The dumb Part 1 solver didn't even solve the first line of unfolded input it scales that poorly. Tried just generating the valid strings and that also scaled poorly (and was really buggy) but it could easily be converted into a function that counts the number of valid combinations instead of generating them. It was still too slow, but changing
cluesto atuple(so it's hashable) and slappingfunctools.cacheon there solved that, bringing it down to 5 seconds (still slow, but faster than part 1). Withoutcache, it would have needed to make 7905412988817410 function calls. Instead, it hit the cache 125298 times and missed 2056548 times - over 3.6e9 times fewer calls (it would have taken over 500 years to finish).I thought about using the fact that the input is repeated to simplify the calculation but the potential for overlap reduced the chances of that working out, so I didn't.
I'll probably tidy up the code but the initial code will still be available via the commit hash link above.
6
u/marx38 Dec 12 '23 edited Dec 12 '23
[LANGUAGE: Rust]
Dynamic programming solution.
Part 1 runs in 568.4µs and Part 2 runs in 4.3ms.
dp[i][j] := Number of arrangements of the first j springs into the first i locations
To avoid using O( n2 ) memory, we keep only the last two rows of the dp table at a time.
6
u/ai_prof Dec 12 '23 edited Dec 12 '23
[LANGUAGE: Python] - Short, readable, commented.
Used DP for both - took ages to get it working - and now it looks so clean. It didn't earlier!
Needs the cache to speed things up (thanks 4hbQ) - many thousands (millions?) of times quicker (at least).
paste link
6
u/azzal07 Dec 12 '23
[LANGUAGE: Awk] First one over a second this year.
Gnu awk was exceptionally slow at ~18 s, other implementations that I tested took only ~2 s.
6
u/ka-splam Dec 14 '23
[LANGUAGE: Dyalog APL]
Part 1:
This takes ~35 seconds to run, it does a lot of string splitting and recursion (
∇). I haven't got a part 2 yet, I've had to read other people's explanations to even see how.5
u/SuperSmurfen Dec 12 '23
[Language: Rust]
Link to full solution
(1194/547)
Phew, today was a tough one. Really happy with my p2 placement. Most of my part 1 code worked for part two, just needed to implement caching. My solution is a memoized dynamic programming approach.
The bulk of the logic:
Runs in about
40mson my machine, not sure how to optimize it much further.5
u/encse Dec 12 '23
[Language: C#]
Code is commented
https://github.com/encse/adventofcode/blob/master/2023/Day12/Solution.cs
5
u/ash30342 Dec 12 '23
[Language: Java]
Code: Day12
Part 1 runs in ~100 ms, part 2 in ~500 ms.
I choose a recursive approach for part 1. This took me quite some time, figuring out all conditions which have to be met for damaged springs. Fun though! This also let to some heavily commented code ;-)
For part 2 luckily this approach meant that I only had to add some memoization.
5
u/danvk Dec 12 '23
[Langauge: Zig]
https://github.com/danvk/aoc2023/blob/main/src/day12.zig
Part 1 was brute force (try both possibilities for
?). Part 2 I tried going recursively character by character but this was too slow (it may have worked if I added memoization). Instead I split the list of numbers in half and tried each possibility for the split point in the pattern (by setting those two characters to.and#, as the pattern allowed). Then you can recur and multiply the left and right counts. The beauty of this is that you don't have to enumerate every possible solutions.Runs over my whole input in ~10s on my laptop in a debug build.
5
u/Derailed_Dash Dec 12 '23 edited Dec 15 '23
[LANGUAGE: Python]
Solution in Jupyter notebook
5
u/rjwut Dec 12 '23
[LANGUAGE: JavaScript]
I'm pleased with how my solution turned out, despite struggling with it for a while. Both parts together run in 138 ms on my machine.
GitHub
5
u/PhunkyBob Dec 12 '23
[Language: Python]
source
I initially solved part A with regex.
I was obviously forced to use another approach for part B.
I separated input in blocks:
becomes a tuple of
I used a recursive function to test if I can replace the first "?" of the first element with a "#" or remove it.
I'm kinda proud of the performance of the algorithm: it takes less than 4 seconds on my old computer, thanks to lru_cache.
5
u/RookBe Dec 13 '23
[Language: Rust] [Language: all] Blogpost explaining my solution in Rust, the explanation can be used for all languages: https://nickymeuleman.netlify.app/garden/aoc2023-day12
My part1 explained, and for part2 I used a slightly modified version of u/Crazytieguy 's code
5
u/Atijohn Dec 13 '23 edited Dec 13 '23
[LANGUAGE: Python (3.11)]
NFA solution (probably overkill). I've noticed fairly quickly that the problem can be reduced to a simple regular expression -- any number of dots or question marks (
(.|?)*) followed by N regular expressions of the form(#|.){i}(ibeing the size of the ith contiguous group), separated by N - 1 expressions of the form(.|?)+and ended with the same expression as the first one.So it's really just a matter of parsing the input string, while collecting the amount of "forks" the NFA takes, summing up the forks whenever a state would merge back, and taking the value from the final state. Unfortunately, I couldn't really find a library that would let me do that easily, so I had to implement them myself, and boy, is implementing an NFA bug-prone as hell. I first tried doing it in Haskell (a stateless language is not a good language to implement a state machine in), ended up doing a mess in Python, it took a few hours, 162 lines total
https://pastebin.com/4KStjBNv
5
u/emceewit Dec 13 '23 edited Dec 13 '23
[LANGUAGE: Haskell]
Wow, part 2 of this one kicked my butt. It took me a long time to give up on my initial intuition that there was some way to construct the solution to the 5x unfolded problem from the 1x and 2x unfolded solutions. Once I finally moved on from that, spent a bunch of time getting the recursion relations correct to generate the allowed arrangements with pruning on the fly; the final leap was adding memoization.
https://github.com/mcwitt/puzzles/blob/main/aoc/app/Y2023/D12/Main.hs
5
u/xavdid Dec 15 '23
[LANGUAGE: Python]
Step-by-step explanation | full code
This took a couple of extra days, but I got there! I was helped no small party by this great comment by /u/damaltor1 which set me on the right track for part 2.
Ended up brute forcing pt 1 and doing a simple(ish) cached recursive approach for part 2. The hardest bit for me was getting the conditions right when ensuring a
#at the start of the record was big enough (but not too big!) to fulfill a group. Not too bad when it was all said and done!5
u/maneatingape Dec 16 '23 edited Dec 16 '23
[LANGUAGE: Rust]
Using some spare time at the weekend figured out a dynamic programming approach calculating the possible arrangements for each entry in
O(n * w)complexity where:nNumber of springsw"Wiggle" is the amount of extra free space the springs can slide around in the pattern.We build a table for each entry with a row for each spring and a column for every character in the pattern. Adding a trailing
.character makes bounds checking easier without changing the number of arrangements. The result will be the bottom right value.Using the sample
?###???????? 3,2,1:This approach is much faster than my original recursive+memoization solution taking only 1.3ms (down from 4.2ms for the original).
Heavily commented solution.
5
u/atrocia6 Dec 18 '23
[Language: Python]
Part 1, Part 2 (the identical code, with just two additional lines in part 2 to do the unfolding)
I love AoC - for part 2, I was coming up with increasingly complicated pruning optimizations for my recursive solution, which still didn't complete in a reasonable time, until I realized the / a relatively simple solution, which finishes fairly quickly and took under 20 LOC in its final version ...
9
u/Nithramir Dec 12 '23 edited Dec 12 '23
[Language: agnostic]
Solved it with two-dimensions DP.
First thing I need to do is to "expand" the contiguous groups of damaged springs to a boolean array, with true being "1 damaged" and false being " a gap of operational" (1 or more operational).
This means that 1,1,3 is expanded to [T, F, T, F, T, T, T]
Then I just have to match the characters to the array of booleans, which is very similar to most algos to match two strings together, for eg. Longest Common Subsequence.
The tricky part now is the ends. Because you they can match either T or F (eg. ".#" and "#" match [T]). To handle the ends, I added '.' to the start and end of the chars and added 'F' to the springs. This way start and end always match.
Here is an implementation in Java:
Note: there is a way to solve it very similarly without expanding (keeping numbers), but I find it easier to resonate in the expanded way. I don't think the complexity is impacted anyways
8
u/JustinHuPrime Dec 12 '23
[LANGUAGE: x86_64 assembly with Linux syscalls]
Phew. That was tough. I really need to brush up on my DP - it's a bit of an algorithmic blind spot.
For part 1, perhaps a bit optimistically, I implemented it using a brute force search. I read in the list, then I treated the unknown positions as bits in an integer, and did ripple-carry addition in software.
For part 2, I was forced to implement the memoized recursive solution. Following first-year software engineering design principles, I designed the recursive solution. The base case was that if I had no more springs and no more runs and I was done my current run, then I had a solution. Otherwise, if I had no run of damaged springs going on, and I saw what could be an operational spring (either definitely operational or unknown), then I either declined to start a run and recursed, or I started a run and recursed, and otherwise (it's definitely damaged, but there's no run going on), I returned zero. If I had a run of damaged springs going on, and I saw what could be a damaged spring, I included it in the run and recursed, otherwise (it's definitely operational, but there's a run going on), I again returned zero. This required me to prepend a definitely operational spring to the springs, to avoid the case where we start a run at the start of the springs. And then I applied some third-year algorithms class knowledge and memoized the whole thing (although I really didn't pay attention in my third-year algorithms class...).
Surprisingly, I only had two bugs in part 2 - I forgot the base case (how embarrassing - I can hear my first year software engineering prof being exasperated at me), and I didn't properly append the unknown values when unfolding - I was writing them to the list, but forgetting to increment my list index.
A lot of the difficulty with assembly is the lack of a standard library - I have to write my own multi-dimensional arrays in place of a hash map, I have to implement my own arrays, and so on.
Part 1 runs in 48 ms, and part 2 runs in 143 ms. Part 1 is 9200 bytes long, and part 2 is 9400 bytes long (no, I don't know why they're such round numbers in decimal).
4
u/mental-chaos Dec 12 '23
LANGUAGE: Python] 736 / 91 github
Did a fairly straightforward memoization on the recurrence relation, and it runs in a little over a second even on part 2. The state I use for the traversal is a tuple of "current position, number of fully-matched # sequences, count of # in the current sequence".
4
u/Shemetz Dec 12 '23
[LANGUAGE: Python 3] code on github (cleaned up and documented)
This was nice! I'm glad for the existence of
@lru_cache, made part 2 a breeze. It took me a while to figure out what I was doing wrong with part 1 - for a long time my code didn't correctly ensure that a group of e.g.###&[3, ...]had to be followed by a period (###.is good,####is bad). So, it took me about 10-15 more minutes just to debug this and update the code to always expect a.or?after a sequence of#/?s of the group length (with an exception for the final group - although, in retrospect I could've removed that exception by just appending a.to my input).4
u/mendelmunkis Dec 12 '23 edited Dec 12 '23
[LANGUAGE: C]
Nonograms anyone?
I am unsure why the second memoization requires the memsets to be left in
74.585ms/8.282 s
4
u/_Vector4 Dec 12 '23
[LANGUAGE: python3]
Code
I used a recursive algorithm to find the number of valid configurations, (there weren't even that many cases to my surprise... ). I was most amazed at the speedup of functools.cache! It's a good tool to keep in my pocket, although I was required to use tuples now. This was the challenge I enjoyed most this year so far!
4
u/michaelgallagher Dec 12 '23
[Language: Python]
Solution
I bruteforced part one which obviously became a bad choice for part two. Figured top down memoized dp would be easiest, but took me way too long to figure out the recursive relation; felt like a lot to keep track of mentally. Cleaned it up the best I could. Takes ~2 seconds to run on my machine; there's probably a more optimal solution.
4
u/SopyG Dec 12 '23
[LANGUAGE: Rust]
Combined/Part 1/Part 2
4
u/Dr-Baggy Dec 12 '23
[LANGUAGE: Perl]
See more at:
https://github.com/drbaggy/adventofcode/blob/main/2023/day12.pl
4
u/Old_Smoke_3382 Dec 12 '23 edited Dec 12 '23
[LANGUAGE: C#]
Took me a while and memoisation would have been faster to get to a solution tbh, but this one runs pretty quick and uses very little memory thanks to no caching and binary chop through the sections so recursion depth is only O(log(section count))
Day12.cs (jmg48@GitHub) Part 2: 120ms
Here's an example of how my part 2 algo works:
?????????? 1,2,3I split the sections into a pivot and sections before / after the pivot: [1], 2, [3]
Given the slack, the possible positions of the "pivot" 2 are as as follows:
(
|denotes how the pattern is being broken into smaller sections that can be solved individually)? | .##. | ??????? | .##. | ??????? | .##. | ???Trivially, solutions of the sub-problems are as follows:
? 1 => 1????? 3 => 3?? 1 => 1???? 3 => 2??? 1 => 2??? 3 => 1So the result is
(1 * 3) + (1 * 2) + (2 * 1) = 74
u/xHyroM Dec 12 '23
[LANGUAGE: Python]
part 1
part 2
4
u/Ukhando Dec 12 '23
[LANGUAGE: PHP]
Part 1
Part 2
4
u/WhiteSparrow Dec 12 '23
[LANGUAGE: Uiua]
Runs in ~14sec. This is the third algorithm I tried, before it was able to solve part 2. This is getting serious!
4
u/jackysee Dec 12 '23
[LANGUAGE: Typescript]
link
The moral is : use
Mapinstead of{}for the cache. It takes forever for part 2 if I just use{}while 4s usingMap.4
u/mathsaey Dec 12 '23
[Language: Elixir] https://github.com/mathsaey/adventofcode/blob/master/lib/2023/12.ex
This was actually pretty okay. Took me some time to come up with how I’d handle the search process, but once I decided to go for recursion I had something working for part 1 pretty fast. It surprised me that this naive approach was already pretty fast out of the box. Tackled part 2 by tacking on memoization. I only use it when “splitting”, since that is the only point where you can really end up in a similar situation as before (I think). It proved to be plenty fast enough, so didn’t bother trying to optimize that further.
4
u/jinschoi Dec 12 '23
[LANGUAGE: Python]
I first did it in Rust, then decided to abuse Python's lookahead regexps for terseness. Look for spring-sized windows that are all non-'.' that are not followed by a '#'. Disallow any window that is preceded by a '#'. Add a '.' to the end of all the conditions to make this easier.
4
u/tcbrindle Dec 12 '23
[Language: C++]
I found this by far the hardest day so far.
I eventually got Part 1 working with brute force, generating all possible combinations and checking which ones were valid once they had no
?s left. This got me the first star but it clearly wasn't going to cut it for part 2.I figured that I probably needed to do two things: firstly, prune "bad" paths earlier, and secondly do some sort of caching... but for the life of me I just couldn't figure out the details. Eventually I gave up and came looking at this sub for some hints -- or perhaps better phrased, to improve my dynamic programming knowledge! (Honestly, I didn't even know that just caching past results had a fancy name. I'm still not sure what's "dynamic" about it... also, don't ever google "DP", kids.)
In the end I got it all working, and I think the final code is actually pretty nice. Thanks to the other solvers for pointing me in the right direction!
Github
4
u/xelf Dec 12 '23 edited Dec 12 '23
[Language: Python] with no imports
same method for both parts, just changing the size of the data for part2:
essentially:
4
u/Wayoshi Dec 12 '23 edited Dec 12 '23
[LANGUAGE: Python3] 4400s / 8400s
Needed some sleep and idle time at work to realize why I was getting infinite recursion in part 2.
The final solution really is quite elegant - one big if branch, with only single return statements on each branch (hence the possibility of one-liners already seen in this thread).
Per
cache_info, I had ~68k accesses to the cache when calculating both parts.paste (black-formatted after completion)
4
u/copperfield42 Dec 12 '23
[Language: Python]
code part 1
I could brute force part 1, but part 2 defeat me :( I would need more than a day to come up with something...
4
u/bo-tato Dec 13 '23
[LANGUAGE: Common Lisp]
Basic recursive solution with memoization, no clever tricks to prune or speed up the recursive search so it's slow taking around 20s for part2, but it works.
https://github.com/bo-tato/advent-of-code-2023/blob/main/day12/day12.lisp
4
u/fachammer Dec 14 '23
[LANGUAGE: Scala] code on github
I found the logic quite straightforward, but what tripped me up is that I tried to implement memoization to keep the running time in check, but failed to do it correctly. Therefore I discarded the memoization approach at first until after two days I tried it again (this time correctly)
4
u/thousandsongs Dec 14 '23
[LANGUAGE: Haskell]
Finally!
This is the magic nugget that I was running after:
It took me two days (thinking on and off in parallel with doing the other problems) to get to this, but it was worth it, got a big dopamine hit solving the problem without any hints.
The story goes how I imagine it must've gone for many others: I was able to quickly come up with a recursive enumeration for p1 - it enumerated all arrangements, and then filtered them. This obviously didn't work fast enough for p2. So then I added memoization, but that didn't help.
I understood why that didn't help -- my original recursive formulation was short but recursive in arbitrary ways, and to get the benefit of memoization I needed a solution that was tail recursive so to say -- it should only proceed linearly in the input, and only recurse to smaller inputs when needed.
This is fine to say, but I wasn't able to come up with that formulation quickly. I did manage a few variations of my recursion, but nothing that was easily memoizable.
Finally, today I started from scratch, and gave myself an hour of staring at the screen, and finally was able to come up with the formulation above. I understand what it does, but I can't give a short tldr of what it does (that's actually why I'm excited to finish this problem, so I can look at the simpler, easier to interpret, formulations other people would've come up with).
Of course, to get it to work on p2 I had to add memoization to this solution. I'd written a blog post earlier about doing this sort of a thing, so it was quite straightforward, but I'm not very happy about how my current approach to memoization using the State monad obscures the original recursive formulation a bit.
Here's the code (with memoization) in full. Runs in ~2s unoptimized, ~1s optimized on the full input.
4
u/manhuntos Dec 30 '23
[Language: Rust]
This is my first project in rust. I'm learning it by solving advent of code puzzles. So if you have any hints for me I would be happy to read it :)
https://github.com/manhunto/advent-of-code-rs/blob/master/src/solutions/day12.rs
1
u/glebm Dec 12 '23
[Language: Ruby]
Part 1: Brute force all possible `?` substitutions and count the valid ones.
Part 2: Recursion with memoization. Possibly overly complicated, took me several hours to get working. Finishes in 5 seconds for part 2.
https://github.com/glebm/advent-of-code/tree/main/2023/12
3
u/leijurv Dec 12 '23
[LANGUAGE: Python 3]
24th place on part 2! 🎉🎉 334th place on part 1
I'm somewhat shocked how fast this runs. I would never have imagined. I ran it on part 2 almost as a joke, thinking there's no way it would complete, but it takes less than three seconds even though there are 4443895258186 possibilities for my input.
paste
Screen recording: https://youtu.be/SpKi_0Ou2pg
3
u/kroppeb Dec 12 '23 edited Dec 12 '23
[Language: Kotlin] 197/20 Global: 41st
code and YouTube (once it's done processing)
I made a recursive function for part 1. I'm curious to see how other people solved it, I'm assuming brute forcing?
For part 2 I did the good old "dynamic programming" by memoizing my recursive function.
3
u/recursive Dec 12 '23
[LANGUAGE: typescript]
I built a solution in typescript in a code sandbox. https://mutraction.dev/link/k7 You can see the code and run it. Bring your own input.
In part 1, it enumerates all the valid arrangements and lists them for you to see. It performs this using an iterating bit-mask and regular expression. Part 2 uses memoization rather than a bottom-up dynamic approach. It ends up being a lot faster than the brute force part 1 as a result.
I built this sandbox for a front-end framework that I made as an art project.
3
u/bucketz76 Dec 12 '23
[Language: Python]
Made lots of silly mistakes, but in the end, was recursion for part 1, and memoized recursion for part 2, as was common.
paste
3
u/r_so9 Dec 12 '23
[LANGUAGE: F#]
Memoized recursive parsing of both the springs and the groups at the same time. Part 2 was very quick after part 1 was done (just add caching).
Interesting block- the core part of the recursive parsing function:
Where
remis the remaining characters in the spring,remGroupsis the remaining groups to process,groupis the number of remaining damaged springs in the current group (starts with 0), andneedGapis whether we just completed a group in the processing and need at least one undamaged spring.paste
3
u/Multipl Dec 12 '23
[LANGUAGE: golang]
Dp for today. I miss Python's @cache decorator haha.
link
3
u/xoronth Dec 12 '23
[LANGUAGE: Python]
paste
I see we're now in DP territory. Fun.
Brute forced part one, part two I had to rewrite to work with
@cache.3
u/1234abcdcba4321 Dec 12 '23
[Language: Javascript] 278/1488
paste
I forgot you could DP with memoizing a recursive function (which would've been easier to make... I spent forever cleaning up random edge cases which is why the code is so messy with c(-1,0) being special cased and such) and ended up going for a top-down tabulation solution instead.
3
u/FCBStar-of-the-South Dec 12 '23 edited Dec 12 '23
[language: python3]
Brute forced part 1 and then was stuck on part 2 for a while until I came on this thread and saw people say DP. Took forever to figure out all the base cases. Fumbled around implementing memoization for a while before remembering the clutch @cache. Lots of comments, hope it can help someone.
1.2s in total. Definitely room for improvement but that's tomorrow.
paste
Edit: An additional base case for more efficient pruning, provides a small speedup.
3
u/yieldtoben Dec 12 '23 edited Dec 12 '23
[LANGUAGE: PHP]
PHP 8.3.0 paste
Execution time: 0.1185 seconds
Peak memory: 0.6609 MiB
MacBook Pro (16-inch, 2023)
M2 Pro / 16GB unified memory
3
u/gyorokpeter Dec 12 '23
[LANGUAGE: q]
paste
3
u/sansskill Dec 12 '23
[LANGUAGE: Kotlin]
https://github.com/sansskill/adventofkode2023/blob/main/src/main/kotlin/days/D12.kt
My initial hunch to use DP was correct, but I figured brute force couldn't be that bad right. Should have gone with the gut feeling, would have saved half an hour of trying to optimize brute force to be 'good enough'.
3
u/hcs64 Dec 12 '23 edited Dec 12 '23
[Language: Python]
649/4133
https://gist.github.com/hcs64/917d79fef99b4248830baacac1cc7b1d
I got part 1 fairly quickly with brute force, but it took me a long time to dial in something for part 2. Ended up memoizing a
find_ways()function with arguments: working springs left to allocate, damaged ranges left to allocate, status string left, and a start flag. This will eat all possible sizes of working range, passing off to another functionfind_ways_dam()that just checks if the given damaged range will fit, if successful that recurses back tofind_ways().At least it runs pretty quickly, would be faster if the memoization was smarter (nothing meaningful is shared between cases). I wanted to post before I look over other solutions but I feel like I'll be kicking myself for missing a simpler path.
Edit: Realizing a potential bug that fortunately didn't bite: I don't reset the memo dict between cases, but I don't include the start flag (which allows there to be no working at start) in the memo state, so there could be some crosstalk between cases if one is a suffix of another. There's no point in persisting the dict between cases, and I ought just to be using the string index so there's not so much reference counting traffic from all the string slices.
3
u/pwnsforyou Dec 12 '23
[Language: rust]
simple backtracking solution for part 1 should work https://github.com/happyhacks/aoc2023-rs/blob/master/day12a/src/main.rs the result is in thousands
for part2 just add memoization on top - cache hit rate can be as high as 26% and the result is about 10e14 so the backtracking without memoization would not finish for a long long time. https://github.com/happyhacks/aoc2023-rs/blob/master/day12b/src/main.rs
my backtracking solution involves a state of tuple (current-spring-index, current-damage-group-index, count-of-damaged-springs-seen-contiguous) For each unknown cell - we traverse with states both damaged and operational
3
u/KayZGames Dec 12 '23 edited Dec 16 '23
[LANGUAGE: Dart]
Day 12
The important part of my algorithm is the
(group: 0, amount: 0, permutations: 1)record, thegroupis which group of broken springs is represented andamountis the amount of broken springs in the current group whilepermutationsis the amount of broken spring configurations that can be represented with the samegroup/amountvalues. After each character I combine those records if they have the samegroup/amountcombination and increase thepermutationscount accordingly. Impossible records are dropped as soon as they occur (only thing I don't check - that I am aware of - is whether or not there is still enough length to the string to create a possible configuration). That's sufficient to prevent a combinatorial explosion of possible values (for the final data 100 was the highest amount of uniquegroup/amountcombinations). Doing it this way allows me to check the same spring in all possible permutations one after the another without the need to backtrack or memoize.Runs in approximately half a second.
EDIT: updated and optimized version, twice as fast as before
3
u/Red__Pixel Dec 12 '23
[LANGUAGE: Python]
Fun! Some nice dynamic programming. I first made a slow version that edited the string, but that was way too slow, even when stopping when there were too many groups.
Then I settled for "eating" the string and use caching. Both parts in 20 lines, quite readable, concise and fast.
3
u/Szeweq Dec 12 '23
[LANGUAGE: Rust]
Used
Cowto solve this. No cows were harmed while coding. Runs in about 5 ms.Code: https://github.com/szeweq/aoc2023/blob/master/src/bin/12.rs
3
u/[deleted] Dec 12 '23
[LANGUAGE: python]
Part 1 I did with brute force.
Part 2 I built up a regular expression where each match represented a valid arrangement then tried using pythons built in regex engine. I couldn't find a way to get it to return all valid combinations for counting so I ended up implementing my own (terrible) backtracking regex parser over a subset of the python regex language. With memoization it's fast enough to solve the problem.
Code: https://github.com/ch33zer/aoc2023/blob/main/day12.py
3
u/aptacode Dec 12 '23
[LANGUAGE: Typescript]
First day using Typescript this year, using the same recursive method for both parts - I've added comments to hopefully make it a bit more readable.
Part 1
Part 2
3
u/jovani_lukino Dec 12 '23 edited Dec 12 '23
[LANGUAGE: MATHEMATICA]
PART I
(in 25 sec)
3
u/i_have_no_biscuits Dec 12 '23
[Language: Python]
Recursion, splitting the pattern by the largest 'block' each time, and then multiplying the number of ways of filling in the pattern on the left/right. Takes just over a second for both parts on my computer. I'm sure it could be sped up if I passed the start/end pattern indices rather than doing 5 million string slices, but it runs fast enough for me!
As with a lot of recursive problems on AoC, @cache is the vital ingredient.
I've tried to use relatively readable variable names rather than single letter ones.
Here's the code (~30 lines)
3
u/Symbroson Dec 12 '23 edited Dec 12 '23
[Language: Ruby]
It took me 4 very long hours of tryharding ways to optimize the recursion until it dawned me that all my efforts were wasted and I should maybe use caching to solve part 2.
I like the core part of my recursion though - simple 6 line 3-way condition of consuming individual characters
Part 2 takes 10 seconds to complete iteratively, 1 second when using parallel forked processes
I kept one recursion optimization though: Before the recursion I count how many
?can be skipped to still be able to fulfill the required total#. When the limit is reached the recursion returns early and saves a bit of timepaste
3
u/[deleted] Dec 12 '23 edited Dec 12 '23
[LANGUAGE: python] Did a recursive DP.
Runs in under 1 sec
Any optimisations I am missing?
3
u/juanplopes Dec 12 '23 edited Dec 12 '23
[LANGUAGE: Python]
Both parts in 19 lines. Runs in 0.3s on PyPy.
https://github.com/juanplopes/advent-of-code-2023/blob/main/day12.py
3
u/aurele Dec 12 '23
[LANGUAGE: Rust]
https://gist.github.com/samueltardieu/1295a8e8aa8ad31c0231b7b99dd68246
3
u/mcnadel Dec 12 '23
[LANGUAGE: Python]
Today I used recursion and also cached it for part 2. Runs in ~0.3s (for both parts). Not the fastest but great for this type of recursion and fairly simple :)
GitHub
3
u/tymscar Dec 12 '23 edited Dec 12 '23
[LANGUAGE: Rust]
Wow, today was a mess. I spent hours thinking of a smart way to do part1 because I was scared of what part2 would be.
In the end I just did a bruteforce for part1 which finishes in around a second, but the code is rather nice and easy to undestand.
Part2 on the other hand took me another couple of hours, and I ended up with a bunch of spaghetti code, and a lot of duplication, but I am too tired to refactor it and I dont really enjoy this problem, so I'll leave it.
Up to today, all days ran in under 150ms, most way under 1ms. Today runs in 2.5 seconds.
Edit: well I know I said I wont, but I couldnt stop thinking about it, and found HyperNeutrino's video, which made me want to refactor mine similarly to his. Now both my parts run in under 29ms together. The only difference is that in rust, to not fight with the lifetimes, I needed to do a lot of clones, which slowed it down to a crawl, so I ended up implementing the algorithm with usize offsets for both the characters and the numbers, and I used those as the key in the memo hashmap.
Part1 and Part2
3
u/Sp00ph Dec 12 '23 edited Dec 12 '23
[LANGUAGE: Rust] https://github.com/Sp00ph/aoc2023/blob/master/src/day12.rs
This one was kinda rough. I tried to keep all solutions sub 1ms, but today I only got to ~3ms for part 2, with my initial implementation taking ~40ms. Basically all of the optimization was about improving the speed of the cache. I started out with a simple hashmap based approach that used a pair of arrays as a key. Now, I'm using just the lengths of the arrays as the cache key, and squashed that length down to 12 bits. This makes the key space small enough that using a dense array is faster than a hashmap (3ms vs 4ms).
3
u/david3x3x3 Dec 12 '23
[LANGUAGE: Python]
This solves part 1 and 2 in about 12 seconds with pypy (36 seconds with python). I worked on this for a few hours when the puzzle was released. I had the right idea but was chasing bugs, so I went to bed and quickly had the solution in the morning.
https://github.com/david3x3x3/advent-of-code/blob/main/2023/12/day12.py
3
u/schveiguy Dec 12 '23
[LANGUAGE: D]
https://github.com/schveiguy/adventofcode/blob/master/2023/day12/springs.d
Haven't done a good DP program in a while, great one! Cost way too much time by forgetting to change int to long in some places lol.
Algorithm explained in the top comment.
3
u/codeunveiled Dec 12 '23
[Language: Python] 🐍🐍🐍
Source code
Video explanation: https://youtu.be/WiNz_zBhGIM
Time: 0,293s
3
u/mnkyman Dec 12 '23
[LANGUAGE: Kotlin/JVM]
Solution.
I used dynamic programming like many others, but I did not break subproblems up character-by-character, but rather by contiguous groups of broken springs. The idea is that if you choose to mark the current spring as broken, then you know that the next few springs must also be broken in order to satisfy the broken group lengths requirement. This lets you make fewer recursive calls as there are fewer subproblems, and query/update the cache less often, leading to a faster solution.
On my machine, it runs in 62ms, at least 25ms of which is unavoidable overhead from the JVM. A reimplementation in C or rust would likely be much faster.
3
u/DrunkHacker Dec 12 '23 edited Dec 12 '23
[LANGUAGE: Python]
Recursive memoized solution. Same as everyone else. I feel like the arrangement count method could be more elegant but I went for the straightforward one-char-at-a-time approach. Solves both in under a second.
3
u/POGtastic Dec 12 '23 edited Dec 12 '23
[LANGUAGE: F#]
https://github.com/mbottini/AOC2023/blob/master/Day12/Program.fs
Neat opportunity to look at memoization in a functional language! There's a big performance hit when you use linked lists to represent the springs in this problem because you have to traverse the list every time you look it up in the cache, but pattern matching is a lot easier, and the program still finishes Part 2 in less than 3 seconds on my machine.
Edit: Adding
PSeqand running in parallel drops the runtime into less than 1 second. Very easy performance improvement.3
u/Maravedis Dec 12 '23
[Language: Clojure]
Man, fuck memoization in clojure. Making it work with recursion is a pain.
The core function is like this, just map over lines and groups:
Once I figured it out, it runs decently fast for a dfs.
Github day 12
3
u/sgxxx Dec 12 '23
[LANGUAGE: Python]
10 lines solution https://github.com/sayan01/advent-of-code-2023/blob/master/12/p.py
Used regex and %-string formatting along with walrus operator. Inspired from u/hrunt
3
u/optimistic-thylacine Dec 12 '23 edited Dec 12 '23
[LANGUAGE: Rust] 🦀
Started trying to find a DP solution, but found it easier to go with a recursive approach with memoization. Below is the recursive routine that counts the number of ways the springs can be grouped.
Full code
dmgis the list of numbers represeting continguous damaged springs.unkis the ruined map with many springs in unknown condition.3
u/fullmetalalch Dec 12 '23
[Language: Python] Gitlab
@cacheDefinitely enjoyed this one! Good to get more hands on experience with recursion and non-trivial memoization.
3
u/vbe-elvis Dec 12 '23 edited Dec 12 '23
[LANGUAGE: Kotlin]
Made a recursive solution that works for both solutions. Needed to add a cache for Part2 or it would run for hours.
https://pastebin.com/GWL1tqLD
The cache key is based on the hint-index and the start-index.
Part 1: 23ms Part 2: 143ms
3
u/Steinrikur Dec 12 '23 edited Dec 13 '23
[Language: bash]
No recursion used. eval is horribly slow, but one-liners are always fun. There is surely a faster way to force bash to do brace expansion, but this works...
Convert the symbols to greppable chars and then convert the list into a regex. Then use brace expansion to print every single permutation of the springs, and grep them. Added "dots" on both ends so the springs can be on the start/end positions.
This does not scale for part 2, since it would need to print up to 290 lines of symobls.
3
u/sanraith Dec 12 '23
[LANGUAGE: Scala]
Code is available on my github: Day12.scala
If there are n continous broken segments, then there are n-1..n+1 continous operational segments. I generate the ranges for each operational segment ((0..m), (1..m), ... (1..m), (0..m)) where m is the amount of operational units, and recursively iterate over their combinations. I only use values where the operational segment and the next broken segment matches the records. When there are many ???-s after each other the checked segments overlap and blow up the problem space, so I added memoization for part 2.
3
u/CCC_037 Dec 12 '23
[Language: Rockstar]
Gyargh.
Did another recursive solution. Took a while, so I had to put in a few optimisations. You know, just to take the edge off the time requirements.
Part 2, I just expanded the inputs to my function. I then had to add yet more optimisations, including a dictionary to see if my recursive function had been called with those inputs before.
3
u/clbrri Dec 12 '23
[LANGUAGE: C-style C++]
Here is a 73-line C/C++ solution based on standard recursive memoization.
Uses a global lookahead cut-off optimization (prunes ~33% of the recursive calls)
Runs in 9 minutes 14.6 seconds on a Commodore 64, or in 2.58 milliseconds on an AMD Ryzen 5950X. (5950X is 214,961.24x faster than the Commodore 64)
3
u/Alkyonios Dec 12 '23
[LANGUAGE: Ruby]
Kind of surprised of how "quickly" I got part 1 (20-25 minutes), to a problem that seemed quite complex initially.
I counted the wildcards in the string ("?") and then generated every binary string of that length and replaced every wildcard with the character from the binary string in that position.
Time complexity so bad that I'll have to work on something else for part 2. Perhaps BFS could work (since you can ditch a permutation as soon as you get the first group, if it's not the correct length).
Any kind of feedback is highly appreciated
Solution (@input is just an array of every line in the input)
3
u/rabuf Dec 12 '23
[LANGUAGE: Python]
Finally done with Part 2. My initial solution was quite dumb but fun to write. Took around 8 seconds for p1, though. It generated all (unique) assignments to the
?characters and then checked if they met the requirements of the group count. Obviously generating that many permutations that are discarded takes way too long.For part 2 I totally rewrote it. After dealing with some off-by-one errors (yay having unit tests to verify my work) it finishes both parts in less than half a second on my 6 year old laptop.
The core algorithm is this bit:
3
u/icub3d Dec 12 '23
[LANGUAGE: Rust]
Solution: https://gist.github.com/icub3d/7aa45ca96ccb88ebf95b91d6a28eba74
Analysis: https://youtu.be/GJq_Hza8nSk
I like the dynamic programming problems because I always like the practice. It wasn't easy to get there but getting the practice in is helpful.
3
u/maitre_lld Dec 12 '23
[Language: Python]
I could not code from 6am to 3pm. This was actually good because during this time I thought about a lot of solutions that would be unwritable... By 3pm I was convinced that the easiest would be to do recursion on the first character. The recursion formula is not bad at all. With memoization, it goes down to 300ms for Part 2 on a crappy i5 proc.
https://github.com/MeisterLLD/aoc2023/blob/main/12.py
3
u/crnkofe Dec 12 '23
[Language: Zig]
I'm really happy about how straightforward part 2 turned out to be. It took me rusted gears a while to get what actually needs to be cached for this to work. It also runs pretty fast within a few seconds without me doing any optimizations.
https://github.com/crnkofe/aoc2023/blob/master/src/day12.zig
3
u/Fyvaproldje Dec 12 '23
[LANGUAGE: Raku]
https://github.com/DarthGandalf/advent-of-code/blob/master/2023/Day12.rakumod
Why do I ever attempt DP? I was debugging it for many hours until I decided to write a simple recursive function with memoize (using vim 9 and tmux 3.3a, because [Allez Cuisine!]).
This is salted(!) md5 hash of my kernel config: f268335c6dc4719adab1f3e79b3cdd37 and the version of it is 6.1.60-gentoo
3
u/Naturage Dec 12 '23
[Language: R]
Solution here. Today took every semi-cheat that it could: googling the theory (which I knew from a few years back but utterly forgot), browsing reddit for P1 edge case examples, and taking a long lunchbreak off work as well as hours in the eve. But it runs, and runs in 20s for both parts.
In the end, I have a dynamic programming set up, which uses matrix where row defines # of entries left in LHS of spring grid, and col defines # of entries left in RHS of the spring records. Then we can initialise the edge (e.g. if we have no records left, if grid has any # on it we have 0 ways to make it happen, if it only has . and ? we have 1 way), and then finally express each other entry through ones to the left/above. I tried recursion, memoisation and direct approaches, neither was even remotely close to quick enough. In particular, I was displeased with memoisation; from looking through here, it should have done the trick, but I likely implemented it poorly.
3
u/sikief Dec 12 '23
[LANGUAGE: C++]
[PLATFORM: Nintendo DS (Lite)]
Solution - Part 1 and 2
3
u/Totherex Dec 13 '23
[Language: C#]
https://github.com/rtrinh3/AdventOfCode/blob/2139838074181b65b0ab98a2ce4c53107e3e6b69/Aoc2023/Day12.cs
Whew, what a day! I did part 1 with a brute force search which ran in 40 seconds. Clearly, that won't be good enough for part 2. During lunch break, I wrote a backtracking search and let it run over the afternoon. After supper, I came back to write the this memoized version while the backtracking version was still running. Judging by the outputs, the old version was maybe 50% done when the new version got the answer.
3
u/dyatelok Dec 13 '23
[Language: Rust]https://github.com/dyatelok/aoc2023/blob/main/day12/src/bin/part2.rsNo recursion or hash involved. Just an iterative solution exploring and collapsing all similar branches. Takes ~0.07 seconds for part 2 on my cpu. Should be easy to execute in parallel.
3
u/j_ault Dec 13 '23
[Language: Swift]
Code
I admit I had no idea how to tackle this other than by either brute force or maybe some kind of binary search, and while either of those might have worked in part 1 without taking an eternity I had the sense that neither one was going to be good enough for part 2. So I just bailed on this last night and picked it up again this afternoon after reading some of the solutions. I found this post to be particularly helpful & ended up implementing that in Swift.
3
u/stevie-o-read-it Dec 13 '23
[LANGUAGE: C#] (LINQPad)
Okay, how about this one:
github
3
u/onrustigescheikundig Dec 13 '23 edited Dec 13 '23
[LANGUAGE: OCaml]
github
This time with the right link.
Typical recursive dynamic programming solution, memoized for Part 2.
Bonus prolog solution for Part 1 that I've run out of time on tonight.
3
u/CrAzYmEtAlHeAd1 Dec 13 '23
[LANGUAGE: Python]
GitHub link to my solution
Thanks to u/pred because I was spinning trying to figure out a reasonable solution. I was stuck on thinking there had to be some math solution, and I got completely lost in it. I followed what u/pred did almost to a tee but it was such a good solution so ehh!
My execution in the script took at least 2 seconds, but my statistics script only took 35ms so I'm going with that :D
3
u/jsgrosman77 Dec 13 '23
[Language: Typescript]
For part 1, I did the opposite as most people, and iterated on the numbers, creating a set of every combination that fit, and then checking that against the pattern. I knew part 2 was going to make that approach unworkable, but I tried. I switched from recursion to using a stack when I exceed the call stack, and I realized I could just count the matches instead of returning them. I ran it all night, and only did 125/1000, so no go. Then I did what a lot of other people did and iterated through the pattern, using recursion to check for either '#' or '.' when I hit a question mark, memoizing as I went. I got stuck on some off-by-one errors and poorly thought out end conditions, but finally powered through.
My code is a mess, but it's a monument to my persistence.
https://github.com/jsgrosman/advent-code-challenges/blob/main/advent2023/advent12.ts
3
u/DownhillOneWheeler Dec 13 '23
[LANGUAGE: C++]
https://github.com/UnicycleBloke/aoc2023/blob/main/day12/day12.cpp
My first attempt was a brute force recursive thing involving a generated regex expression. That was fine for Part 1... After much head scratching and farting around, I eventually remembered a thing called memoisation just before going to bed.
It was very satisfying to wake up a little before 5AM, add the cache I needed, and watch it work first time. Squeezed the solution for Part 2 in just before the 24 hour mark - a very nice way to prep for Day 13.
3
u/RB5009 Dec 13 '23
[Language: 🦀 RUST 🦀]
Just a regular top-down DP
🦀Times
The solution can be sped up by using a 3D array as cache instead of a HashMap
3
u/weeble_wobble_wobble Dec 13 '23
[LANGUAGE: Python]
GitHub (31 and 46 lines with a focus on readability)
Part 1 brute-forces using
itertools.combinations, part 2 uses dynamic programming with adictas cache (rather than usingfunctools.cache) just for fun. Took a bit to to fix all the edge cases, hope this solutions helps some of you having difficulties.3
u/CutOnBumInBandHere9 Dec 13 '23
[Language: Python]
I'm a day behind on the puzzles right now, since I didn't have time to solve over the weekend.
This one was fun, and
functools.cachemade part 2 a breeze.The core of my solution is the
countfunction, which takes a tuple of ints representing the three states (off, ambiguous and on), as well as a tuple of block lengths, and returns the number of assignments of the ambiguous values that work.It's recursive, with the following base cases; the third is checked last:
Otherwise, if the first character is off, then the count is the same as the count ignoring that assignment and we can recurse.
If the first character is on, we can check whether the first
lcharacters would fit the first block, and thel+1'th character is either the end of the string or compatible with an off state. If it is, the count is the same as the count for the remainder of the string on the remainder of the blocks and we can recurse.Finally, if the first character is ambiguous, the count is the sum of the counts for the two possible assignments of the character, and we can recurse.
Link
3
u/aexl Dec 13 '23
[LANGUAGE: Julia]
This was the hardest challenge for me so far!
I've first tried a combinatorics approach (we know how many '#' need to be set and we know the places where they can be set). This worked great and instantly for part 1, but for part 2 it had no chance...
So I rewrote my code and chose a recursive approach with memoization.
Solution on GitHub: https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day12.jl
Repository: https://github.com/goggle/AdventOfCode2023.jl
3
u/fridi_s Dec 13 '23
[LANGUAGE: Fuzion]
https://github.com/fridis/fuzion_aoc/blob/main/2023/12/part2.fz
Using combinatorics only: This seems to be one of only very few solutions that does not use a cache / memoization or dynamic programming, but purely functional combinatorics using binomial coefficients to place groups into stretches of repeated ????.
The Fuzion Language is under development, so everything is very unstable and changing rapidly.
3
u/Dullstar Dec 13 '23
[LANGUAGE: D]
Reposted with revised description (I deleted the old post earlier, so it can no longer be edited).
https://github.com/Dullstar/Advent_Of_Code/blob/main/D/source/year2023/day12.d
It's a little slow in Part 2. The caching had to be handrolled because the function arguments aren't set up for doing it the easy way. I don't think that's why it's slow though, at least after taking a look at some step-by-step guides -- I believe the problem is the branch culling, which could be improved a bit by keeping better track of the current position in the array of run_length and being smarter about handling runs: if we know the next one needs to be, for example, 10 long, we need to branch everywhere it can potentially fit, but we don't even need to generate branches on the positions that don't have room for it and we also don't need to branch on each # in that sequence because all the . branches are going to be obviously incorrect.
3
u/e_blake Dec 14 '23
[LANGUAGE: m4] [Allez Cuisine!]
Finally posting now, after having (successfully!) resisted the urge to even open the megathread or even read any of the main reddit for the past 2 days, so as to be sure I did it all on my own (definitely took me way too long to even try streaming). And I think u/topaz2078/ COMPLETELY missed out on the obvious pun in part 2: instead of stating that the input strings were folded, the problem should state they were compressed!
m4 -Dfile=day12.input day12.m4
Depends on common.m4 and math64.m4 (in my repo), and takes ~3.9s to run (definitely the slowest runtime so far, I may be able to optimize it some, but now I have to catch up on two more days of puzzles). Although I eventually reached the correct answer for part 1, it took me more than 24 hours and at least 5 failed attempts (you know it's bad when the website won't even tell you if the answer is too high or low, but merely that it is wrong and you must wait 5 minutes, because of so many earlier wrong tries). The part 2 solution then took me less than an hour of refactoring: POSIX m4 is limited to parameters $1-$9, and I was using all of them in part 1, so I had to rewrite my algorithm to pass in "arg1, (arg2, ..., argN)" as just two arguments instead of 30. Then I immediately noticed negative numbers in my trace output, which was an obvious clue that I had to do another round of refactoring to add up numbers using arbitrary-precision rather than 32-bit signed integers.
I immediately recognized the problem as being similar to puzzles I've done by hand loads of time (sometimes named Tsunami, Hanjie, or Nonogram). And I seriously considered cranking out 1000 answers by hand for part1, but got only as far as 10 rows before I was able to come up with a rough algorithm to match what has already been so ingrained in my head. That is what made this SOOO tedious: as a human, I am very good at pattern recognition, and have done these puzzles so many times that my brain has picked up intuitive shortcuts such as "pack the numbers as far left as possible; pack the numbers as far right as possible, and see what MUST become # in the middle because of overlaps", but which I could not adequately transform into code without becoming a mess of spaghetti. In particular, m4 makes it easy to get at the first argument of a list, but O(N) work to get to the last argument, so optimizing on the right side is MUCH harder than optimizing on the left side. You can see the various commits in my git history where I checked in partial progress, first with lots of special casing (does the string start with .# or end with #.?), and where the later versions ripped that all out to go with the much simpler brute force divide and conquer with memoization (in turn, memoization requires that I use 012 instead of .?#, to form valid macro names).
My development cycle (using emacs as my IDE and a terminal for invoking m4 - fairly low-level stuff) involved a LOT of the following command lines:
m4 -l20 -daeqt -Dfile=tmp.12 day12.m4 2>&1 |lessfollowed by scrolling around in less for patterns such as
- (_?score|lone), which let me find where a particular macro was being expanded to then check if the logic was doing what I wanted. For example, at one point, my code for lone(5, 11012, 2) was producing an answer of 2, where I wanted an answer of 1; reading the trace like this let me figure out I was not accounting for the end of the string, fixed by replacing eval($3-$5+1) with min($4, $3-$5)+1.It took me several tries to get check() and lone() to do the right thing, with an interactive m4 session where I did things like unit-testing by hand:
Another tool in my toolbag was temporarily rewriting things for easy tracing, such as tweaking the code:
So I could then run
m4 --trace=Define -da day12.m4 2>&1and see which values were being calculated for each row of part 2.3
u/mothibault Dec 14 '23 edited Dec 17 '23
[LANGUAGE: JavaScript]
Boy oh boy did I waste my time on this one!
Got stumped for an hour trying to figure out how to address it. Came up with the idea of finding all potential grouping of operational springs's lengths, intertwine them with groupings of damaged springs, build the strings, evaluate the strings, count results.
Coding it was a breeze. Worked A1. Then came P2. I tried to memoize and optimize it for over 24hrs, but then realized that while I only had to evaluate 45 distinct grouping scenarios out of 1000 rows, some of these grouping scenarios had 7e+18 possible variations.
Finally abandonned the idea, went back to the drawing board, watched a bunch of videos about memoization use cases, and then I figured what the approach was supposed to be.
Coding it was a breeze. It's not perfect (takes like 10s to run), but it works, and I can move on :)
with lots of in-line comments:
https://github.com/yolocheezwhiz/adventofcode/blob/main/2023/day12.js
3
u/silt_lover Dec 14 '23
[LANGUAGE: rust]
Hard mode rust - #![no_std], no std collections or deallocation. I ended up implementing a generic LRU cache, which handily can be stack allocated (thanks const generics!) After that, it was just some bit twiddling to check whether a pattern fits.
github
3
u/linnaea___borealis Dec 14 '23
[LANGUAGE: R]
https://github.com/lauraschild/AOC2023/blob/main/day12.R
https://github.com/lauraschild/AOC2023/blob/main/part2_day12.R
Part 1 was fine. Part 2 was horrendous. I learned about recursion and caching and after trying to implement it still ended up translating a python solution into R. However the caching still doesn't seem to speed it up as much as it should do (finishes in roughly 5 minutes now and should be much faster). So if anyone has more experience with caching in R , please let me know where I can optimize.
3
u/ThreadsOfCode Dec 15 '23
[LANGUAGE: Python]
Posting because I finished and now I get to read the reddit for the last 2 days.
My brute force solution for part 1 generated all the possibilities and then counted up the one that were correct. For part 2, I tried many things, but basically started over when, for some reason, I could not even get python to tell me if a list was empty.
I knew from the brute force approach that there were some strings that were repetitive. For example:
You don’t need to figure out the rest of both of these. You can just do this:
And count it twice. I put off writing that solution. It seemed like an indexing and debugging nightmare. But finally I did.
My solution does 5 substitutions at a time, trims off the front ends, and then accumulates the matching strings. Eventually all the ?s are gone and you can just total up counts for the strings that are left.
150 lines of code in the end. Maybe not the most elegant code, and one of the loops is unrolled (and I’m not going to fix it), but it runs in 25 seconds on my Mac desktop.
Best part is that once part 1 ran, I changed the factor to 5 and it ran first try. So glad I didn't need to debug!
code
3
u/Paxtian Dec 15 '23
[Language: C++]
github
Huge thanks to /u/mpyne for the assist concerning memoization.
When I first saw Part 2 and what it was adding, I was like, "Hmm, better change my int's to long's." I did that, for all except the variable that stored the final value.
After playing around for about 3 hours (!!) this morning trying to figure out where I was missing patterns, I finally started going through inputs one by one and was like, "Wow, that single line is just a few digits off of my final... oh no...." Yup, forgot to change that ONE variable to a long.
3
u/NeilNjae Dec 15 '23
[Language: Haskell]
Brute force for part 1, dynamic programming for part 2.
Full writeup on my blog, and code on Gitlab.
3
u/prafster Dec 15 '23 edited Dec 16 '23
[LANGUAGE: Python]
After using brute in part 1, I finally got round to tackling part 2. I was pleasantly surprised at the eventual simplicity of the solution, which uses recursion and memoization, and ~1s for both parts.
Here's the relevant extract from my solution. Full code on GitHub.
3
u/ImpossibleSav Dec 18 '23
[LANGUAGE: Python]
My one-liners are here! Part 1 on Line 49 and Part 2 on Line 81. Part 1 isn't too long today!
I'm trying to solve as many days as I can in one line. I've combined them all into a single line I like to call the Basilisk — check out the code here, and my most recent visualization of Days 1 through 16. Feel free to follow along on my GitHub as well! :)
3
u/835246 Dec 20 '23
[LANGUAGE: C]
I learned about memoization today.
Part 1: https://github.com/efox4335/advent_of_code/blob/main/advent_of_code_2023/day12springpt1.c
Part 2: https://github.com/efox4335/advent_of_code/blob/main/advent_of_code_2023/day12springpt2.c
2
u/silxikys Dec 12 '23
[LANGUAGE: C++] cleaned up sol (71/107)
I recognized it was a DP problem, but I figured coding up the brute force would be faster for part 1. Looking at the time spent, I possibly would have earned slightly more points from gunning for the DP solution initially, but it's a hard call to make (plus I could've misjudged what part 2 was going to be).
Otherwise, I felt it was pretty standard. The bounds are pretty small, so any reasonable DP setup should pass easily.
2
u/simonlydell Dec 12 '23
[LANGUAGE: Python] paste
Went all in on Python’s
matchstatement today, it turned out really nice.For part 2 I slapped
@cacheon it and it worked.2
u/Imaginary_Age_4072 Dec 12 '23
[Language: Common Lisp]
Day 12
Fun with recursion today. I checked through the input and saw that each string didn't have that many ?'s in it, so built a solution that just tried all the possible combinations. This worked great for part 1, but as soon as I saw part 2 I knew it would fail badly.
I rewrote it to be a recursive function and then cached it on the three parameters.
Still not entirely happy with performance, since it took about 20s and I normally want to get them less than 15. It is using full lists of springs as hash keys which will be taking a while, and using lots of memory, so if I get time I'll have a look at speeding that up.
2
u/Akari_Takai Dec 12 '23 edited Dec 13 '23
[LANGUAGE: Rust]
4084/1118 (Solution)
Got a late start to days 8-12, but maybe after today I can turn that around. :)
A reasonably straight-forward DP. I chose to solve it iteratively rather than recursively.
EDIT: Some small optimizations and parallelization got the runtime for part 2 down to ~10 ms on my machine which I'm happy with.
2
u/__Juris__ Dec 12 '23
[LANGUAGE: Scala]
https://github.com/jurisk/advent-of-code/blob/main/scala2/src/main/scala/jurisk/adventofcode/y2023/Advent12.scala
Has a generous amount of comments in the code.
2
u/Abomm Dec 12 '23
[Language: Python] 505 / 2597
paste
Brute forced part 1 by testing every possible string. Happy that I did that to at least get something working because it helped a lot to test edge cases in part 2.
I'm very happy with my final part 2 solution, it still runs a little slow because of what I assume is the regex but I think the regex itself in combination with dynamic programming is rather clean. Good thing I knew to use @functools.lru_cache because otherwise this would take forever to solve. I did have an inefficiency where I was creating a new string in my recursive calls rather than truncating the existing string, this was a huge bottleneck that saved a lot of time after fixing.
Going off the basic dynamic programming principle of 'use this character group to match a number' or 'skip this group and try to match this number in a following group' lead to a lot of edge-cases that I slowly debugged. For instance not using a '#' at the very end of a string when there are a lot of '?' early on. Using a '#' group larger than the number we're trying to match i.e. (1 matches with ### by splitting #|##). And then the regex almost had me go through my input and change '?' and '.' to some other characters because forgetting to escape those was causing some fun bugs. You can see most of these edge-cases handled by a specific branch in the 'f' function.
2
u/gnudalf Dec 12 '23
[LANGUAGE: clojure]
part-one - simple brute force, knowing it would kick me in part-two
part-two - recursively checking the line, using
memoizeto store intermediate results for a better performancecode
2
u/janek37 Dec 12 '23
[LANGUAGE: Python]
GitHub
2
u/NimbyDagda Dec 12 '23
[LANGUAGE: Python]
The algorithm I had a pretty good idea on, as I play a lot of nonograms, but I kept screwing up with fencepost errors.
Day 12
2
u/chickenthechicken Dec 12 '23
[LANGUAGE: C] Part 1 Part 2 it's kind of crazy how much of a difference memoization makes.
2
u/Mats56 Dec 12 '23 edited Dec 12 '23
[LANGUAGE: Kotlin]
Part1 only, got to look at part2 after work (my guess is this approach won't work :D ), trying to avoid spoilers for it in the meantime.
Again I got good use for my "combinations" util. Just brute force, find indexes of all ?, then figure out how many #'s are missing to get the right total amount, and then draw all combination of indexes of that length. Then replace those indexes with # and see if it's a valid solution.
2
u/nitekat1124 Dec 12 '23
[LANGUAGE: Python]
GitHub
I got a bit stuck on part 2, even though I thought my algorithm should be correct, thanks to some hints from reddit I managed to move forward. I believe the approach is still akin to brute-force but enhanced by the use of caching
2
u/Comfortable_Key_7654 Dec 12 '23
[Language: Rust]
Code
It was a fun DP problem. Took
38msfor both parts in release mode. To solve this, I just maintained two states: theindex of the characterI'm currently processing andthe index of the groupI'm working on.2
u/vamp1rebl00d Dec 12 '23 edited Dec 12 '23
[LANGUAGE: C++]
GitHub
DP with O(strlen ^ 2) time and space complexity.
Took a while to implement but runs pretty quick (< 50ms with g++ -O3)
2
u/Okashu Dec 12 '23
[Language: Rust]
https://github.com/micadam/aoc/blob/master/src/pack/aoc23/day12.rs
I used to be quite bad at Dynamic Programming, but this went surprisingly smoothly. Takes 2s for part 2.
2
u/vkasra Dec 12 '23
[language: rust]
https://github.com/dhconnelly/advent-of-code-2023/blob/master/src/day12.rs
8ms for part 1, 12ms for part 2
2
u/Hunky524 Dec 12 '23
[Language: Rust] GitHub
My brain broke with this one, DP does not work well in my head. Have to use memoization otherwise this takes forever on part 2.
With memoization, and rayon parallel iterator, part 1, and 2 computes in around 50ms on an
AMD Ryzen 9 3900x 12-Corewhich I am quite happy with.2
u/yawnick Dec 12 '23
[LANGUAGE: Python]
First time using @cache
paste
2
u/IronForce_ Dec 12 '23
[LANGUAGE: Python]
Part 1 only
It took me forever to figure out how to generate all the possible configurations for each record, and eventually settled on recursion
A second function checks the variations generated for each record, and outputs a list of valid variations
Part 1
2
u/ScorixEar Dec 12 '23
[LANGUAGE: Python]
Part 1: brute forced
O(N*S)where N is the length of the string and S is the number of unknown springs.O(N*S)same as abovePart 2: slapped DP array on to it
O(N*A*B)where N is the length of the string, A the number of groups and B the maximum number of those number groups.O(N*A*B)same as aboveGithub
2
u/mschaap Dec 12 '23 edited Dec 12 '23
[LANGUAGE: Raku]
I really like this one!
For part 1, I checked all combinations of m out of n unknown springs (were n is the number of
?s and m is the number of needed extra damaged springs).I was afraid this wouldn't be efficient enough for the real input but it was – just. (Took about half a minute.)
Of course, that means that this solution is much too slow for part 2, and I'll need to rethink it. I might do that later today if I have time. Hopefully I can come up with something myself before ‘cheating’ and looking here what other people did.
Code for part 1 in GitHub.
Edit: I've finally got a (quite messy) recursive solution that works, and was still way too slow for part two on the actual input until I added caching. But now it runs part 1 and 2 in about 8 seconds.
Code for both parts in GitHub.
2
u/Nyctef Dec 12 '23 edited Dec 12 '23
[LANGUAGE: Rust]
https://github.com/nyctef/advent-of-code/blob/main/2023-rust/src/day12.rs
For the first part, the easiest way seemed to be to generate all possible strings and then just filter for which ones matched the pattern. I thought it might be slow, but I looked at the puzzle input and figured that each line was pretty short, so it should be fine, right?
Then part 2 came along! I immediately got flashbacks to last year's day 16, where I first tried dynamic programming and then had to switch to DFS with lots of state space trimming to find the solution. So of course this year I started with DFS first and had to switch back to DP when that didn't work 😅 Once I figured that all out, though, it didn't take too long to get a solution that was nice and fast (~630ms)
2
u/pem4224 Dec 12 '23 edited Dec 12 '23
[LANGUAGE: Go]
https://github.com/pemoreau/advent-of-code/blob/main/go/2023/12/day12.go
Using recursion and cache. Part1:
157μs1.8ms , Part2:330μs 30msI have used a trick: input numbers are smaller than 255 such that
[]uint8can be used. Therefore, the conversion into string (for cache use) can be done efficiently.2
u/rumkuhgel Dec 12 '23 edited Dec 12 '23
[LANGUAGE: Golang]
Only part 1 for now, gonna do part 2 later.
https://github.com/rumkugel13/AdventOfCode2023/blob/main/day12.go
2
u/Constant_Hedgehog_31 Dec 12 '23
[Language: C++]
Source code in GitHub.
It became a hard, interactive lecture on making recursion efficient :-3
2
u/ranikola Dec 12 '23
[Language: JavaScript] Code
Use an object as a cache where key consists of ci (processed condition record element index), gi (processed group index) and ggi (processed group element index).
On last element of condition return 1 if groups are spent (2 cases) otherwise return 0.
Otherwise explore (there are 3 distinct cases) and then cache the results.
2
u/MarvelousShade Dec 12 '23 edited Dec 12 '23
[Language: C#]
Today I had a huge struggle to to find the error op part 1. I had a solution in no time that worked for the whole test set, but it didn't work for the whole input, there was one particular situation that I didn't cover.
I only discovered it after rewriting my code twice.
Part 2 was quite easy.
My solution is on: https://github.com/messcheg/advent-of-code/blob/main/AdventOfCode2023/Day12/Program.cs
edit: forgot the headertext.
2
u/damnian Dec 12 '23 edited Dec 12 '23
[LANGUAGE: C#]
Not the prettiest, but pretty concise (~20 lines of logic) and fast (~75ms) thanks to memoization (key is effectively (index into s) * 32 + (index into lens)) and sliding windows (k is the number of non-dots among the most recent lens[0] characters).
GitHub
Edit: Increased multiplier to 32 (although 16 gave me the correct answer, it was too small).
2
u/davidkn Dec 12 '23 edited Dec 12 '23
[Language: Rust]
Solution
I brute-forced the solution for part 1, but for part 2 I started to add merging of equivalent states via a
hashbtree map. It uses the stage (currentconsecutive_damagedstep) and the seen number of damaged tiles as the primary key, mapped to how many solutions are in this equivalent state.2
u/sim642 Dec 12 '23
[LANGUAGE: Scala]
On GitHub.
In part 1 I realized that both the mask and the lengths can be converted to a regular expression. Then we have to count the number of strings that match both, i.e. strings in the intersection language. Doing so entirely by brute force by enumerating all strings and matching regexes didn't seem very efficient, so I thought I'd be clever and use an automaton library.
Using the automaton library I could get the automata for both regexes and compute the automaton for their intersection. The number of arrangements can then be counted from the automaton graph directly without enumerating all the strings. This gave a quite neat solution but unfortunately that didn't scale to part 2.
Therefore, I ended up writing the mask and lengths matching recursively directly. To make it fast enough, I had to memoize the recursive function. My part 2 takes ~700ms.
2
u/hextree Dec 12 '23
[LANGUAGE: Python]
Code: https://gist.github.com/HexTree/08bbc28d5fb0d771ac164e5193c16874
Video: https://youtu.be/WbwN40hdK3s
Got there in about 3 hours. Dynamic Programming with memoization, which each subroutine restricts to the first i characters of the string, and the first j nums.
2
u/stanimirov Dec 12 '23 edited Dec 12 '23
[LANGUAGE: C++]
https://pastebin.com/1F1cEahc
Takes about 1 ms for part 1 and 9.5 ms for part 2. Can be trivially parallelized
I guess the only interesting part is that my memoization is:
[remaining-pattern-len][number-of-remaining damaged]. Something that I haven't seen in other solutions2
u/[deleted] Dec 12 '23
[LANGUAGE: Golang] [Code]
Part 1 only. I gave up on part 2, for now. Some things I've tried:
2
u/Kintelligence Dec 12 '23 edited Dec 12 '23
[Language: rust]
https://github.com/Kintelligence/advent-of-code-2023/blob/master/day-12/src/lib.rs
Solved with some recursion and memoization. Can definitely be cleaner, and really hope it can be faster. Hope to revisit and refactor this one, it's taking around 80ms in total. I wonder if you can simplify it due to the cyclic nature.
Edit: Improved the algorithm and got it down to ~35ms. I slapped Rayon on top and am now down to <5ms.
Benchmarks Part 1 Part 2
2
u/vipul0092 Dec 12 '23
[LANGUAGE: Go]
https://github.com/vipul0092/advent-of-code-2023/blob/main/day12/day12.go
Standard DP capturing 3 states - the position that you're on, the group that is being considered and the length of the current group
I'm learning Go with this year's Advent of code, and today I learnt how 2-D, 3-D array declaration can be a pain, esp in a problem like this.
Hoping some Go masters here can help me improve my Golang skills.
2
u/royvanrijn Dec 12 '23
[Language: Java]
Today was a big struggle, I got stuck thinking about representing the groups and filling the blanks. In the end everything works, but I still believe I got it "wrong" somehow.
Here is my code for part 2 (to do part 1, just change the repeating to '1 ').
https://gist.github.com/royvanrijn/0dd74b9da3e9234a972abeceae5e0ca4
How did you all tackle this? How did you represent the data?
I thought about it as follows, if you have a target
1,1,4and we know the final solution is length 10, we know that there are at least character amounts:. # . # . # .0,1,1,1,1,4,0And the even groups (0,2,4,6) need 2 more, at any spot. I just try to add them and see if the pattern is still correct.
For some reason though this still feels odd/wrong somehow, but it was easiest to think about this morning.
2
u/wheresmylart Dec 12 '23
[LANGUAGE: Python]
After far too long being stupid it hit it with the DP/memoization hammer, taking possible matches (with a leading .) from the right hand end until there's only one match left to make, for which we find all possible configurations.
Runs in under 2 seconds, so I'll take that.
day12.py
2
u/mit_verlaub Dec 12 '23
[LANGUAGE: go]
https://github.com/erkannt/go2023aoc/blob/main/12/solution.go
part1: two step process: find candidate positions for each group of springs, count which of their combinations is possible
Stumbled on two things during part1:
part2: solved using memoization
2
u/BeamMeUpBiscotti Dec 12 '23
[LANGUAGE: Rust]
Link
Recursion with a stack + caching for part 2
2
u/oatmeal_andy Dec 12 '23
[LANGUAGE: OCaml]
paste
Pretty fun one today. The solution is the same as everyone else has been doing, but it is implemented using two mutually recursive functions, one (memoized) that traverses the list of springs and looks for a place to place a group, and another that tries to place a group and resumes the traversal.
2
u/Solidifor Dec 12 '23 edited Dec 12 '23
[LANGUAGE: Java]
https://github.com/dirk527/aoc2021/blob/main/src/aoc2023/Day12.java
149 lines, kind of a functional approach using an immutable State class plus a cache to avoid doing double computations. Runs in <0.1 sec.
Did part 1 in a way that adding the cache was easy. Fun problem for me today.
Once more fell into the trap of doing integer sums – for AoC, I should default to long!
2
u/After-Leadership2183 Dec 12 '23
[LANGUAGE: Golang]
https://github.com/macos-fuse-t/aoc/blob/main/2023/12/main.go
2
u/TheZigerionScammer Dec 12 '23
[LANGUAGE: Python]
An interesting and tricky problem, and considering that even though I had a delta of over two hours I dropped by 8000 out of 16000 spots in Part 2 I'm guessing everyone else thought it was tricky too.
Part 1 is a dumb brute force approach, but it's interesting how it works. For each line I counted each type of punctuation, then calculated how many broken strings need to go into the amount of question marks and used iteratortools combinations function to return all the possible combinations of where the broken springs would go. I then created a new string using this information, then split it along the periods, then counted the length of each section and if it matched the lengths of the count at the end of each line, then it found a valid configuration and added it to the total. This takes a couple seconds to run on the input but it still works.
But it definitely wouldn't work for Part 2, oh no. So I decided to change tactics, instead of trying to generate permutations like in Part 1, I decided it was easier to visualize it like a 1 dimensional slide puzzle and could more easily detect all the valid permuations that way instead of checking for any invalid ones. I realized I could only do that recursively, and I think most other people had the same idea. But this took a while to program and I had to find and add every optimization individually. First it took time to figure out how many spaces the slide can move, then it took time to realize a slide is invalid if the immediate next space is a "#", then it took time to realize that if a slide slides past a "#" then the entire chain from that point is invalid. But it was still overcounting, and then I realized that you can't count a slide's position as valid if there are more "#"s past its position than there are the number of springs left to find. I had to program that in as well in both parts of the recursive function, but it finally worked.
Code
I realize I have been vague in this post, part of me typing in a stream of consciousness format. If anyone has any questions I can clarify them.
2
u/egel-lang Dec 12 '23
[Language: Egel]
2
u/deividragon Dec 12 '23
[LANGUAGE: Python]
Another solution recursing on substrings with functools cache. Runs both parts in about 0.33 seconds on my computer.
https://github.com/debbie-drg/advent-of-code-2023/blob/main/Day12/day12.py
2
u/jeis93 Dec 12 '23 edited Dec 12 '23
[LANGUAGE: TypeScript]
There's no way I would've been able to get this one without the help of the AoC subreddit and HyperNeutrino's video. Uses recursion for finding the amount of possible arrangements for a given record, and memoization to speed things up. Let me know what you think! Happy hacking!
Average times:
275.35 µs/iter5.38 ms/iter1.87 ms/iter278.96 ms/iterTypeScript x Bun Solutions for Day 12 (Parts 1 & 2)
Edit: I've updated the benchmarking for day 12 to clear the cache (memoization) during each iteration in order to gather more accurate performance metrics. My average times have been updated accordingly.
2
u/H9419 Dec 12 '23
[Language: Python]
After thinking too much and studied other people's solution, I made my simple divide and conquer that finishes part 2 in 5.5 minutes without caching. Just recursively passing slices around. I am quite proud of myself about this one since 6 minutes is better than the time it would take me to add memorization
Code
2
u/cbh66 Dec 12 '23
[LANGUAGE: Pickcode]
I completely knew that brute force was not going to work for part 2, but I still implemented it for part 1 because it was easier to code. I kept that code around for posterity.
My first optimization attempt was to be smarter about using logic to narrow down cases where you can be certain what a ? has to be. This sped things up a lot, but still not nearly enough to work for part 2. I was a bit stumped, but found a hint on here about dynamic programming, and added a map to my code to cache values. It still takes about 30 seconds to run part 2, but that's fast enough for me.
https://app.pickcode.io/project/clq2ek73bbi4wop01p1030r3a