Rules: no spoilers.
The other rules are made up as we go along.
Share code by link to a forge, home page, pastebin (Eric Wastl has one here) or code section in a comment.
Rules: no spoilers.
The other rules are made up as we go along.
Share code by link to a forge, home page, pastebin (Eric Wastl has one here) or code section in a comment.
121 Comments
200fifty@awful.systems · 7 pts · 2y
day 1
part 1
::: spoiler perl
:::
part 2
::: spoiler perl
Part 2 gave me a surprising amount of trouble. I resolved it by looking at longer and longer substrings from the end of the line in order to find the very last word even if it overlapped, which you can't do with normal regex split. I doubt this is the most efficient possible solution. :::
Also Lemmy is eating my < characters inside code blocks, which seems wrong. Pretend the "<>" part says "<>", lol
200fifty@awful.systems · 4 pts · 2y
day 2
::: spoiler perl
:::
Found this much easier than day 1 honestly...
froztbyte@awful.systems · 4 pts · 2y
lemme is an incredibly hungry little shit, it eats so much
sailor_sega_saturn@awful.systems · 7 pts · 2y
Just when I think I'm out, awful systems pulls me right back in.
C++, Horrifying C++ for day 1b check it out ;)
Day 1: https://www.animeprincess.net/blog/?p=55
One day is enough for me though, writing deliberately off-the-wall code is surprisingly mentally taxing, and normally I make sure to never program outside of work.
self@awful.systems · 4 pts · 2y
fuck yes
froztbyte@awful.systems · 4 pts · 2y
honestly I considered writing a parser-based approach
then I was too tired and thought "hmm, doing this with grok would be funny", but I didn't have logstash handy and fuck dealing with containers at midnight
I will, however, do that today
froztbyte@awful.systems · 4 pts · 2y
Status report: grok allows for non-greedy matching but still captures greedily for term assignment. I think I have a workaround that might work (recursively pipe data back to itself, gated on length for action), need to test later
This particular flavour of parsecrime is near guaranteed to be of interest to very few, but I want to see if I can make it work nonetheless. That’s just how my brainworms work.
datarama@awful.systems · 5 pts · 2y
datarama@awful.systems · 5 pts · 2y
datarama@awful.systems · 5 pts · 2y
gerikson@awful.systems · 4 pts · 2y
Let's do this.
Day 1: Trebuchet?!
https://adventofcode.com/2023/day/1
gerikson@awful.systems · 4 pts · 2y
Perl: https://github.com/gustafe/aoc2023/blob/main/d01-Trebuchet.pl
::: spoiler thoughts
I found this really tough for a day 1 problem. Because I don't really know regex, I did not know the secret to finding overlapping patterns. I quickly figured out that "twone" was a possibility, because it was in the example, but then I had to find other examples and hardcode them into my split pattern.
This isn't general, my input doesn't have 'sevenine' for example.
:::
datarama@awful.systems · 4 pts · 2y
gerikson@awful.systems · 4 pts · 2y
Abso-fucking-lutly. I just find sharing a link easier than wrangling code blocks.
There are also hipster "forges" like Sourcehut, if that's your jam.
self@awful.systems · 3 pts · 2y
absolutely — removing my dependency on GitHub has actually been a blocker on me releasing code lately, and it’s something I want to tackle when we launch that open source community. if it helps collaboration, I can provide some ultra-janky git hosting on awful.systems with the same service that hosts our infrastructure code, though this’d be just basic git and gitweb with ssh public key auth
datarama@awful.systems · 4 pts · 2y
gerikson@awful.systems · 4 pts · 2y
Re Perl findall, I used this regex in my "clean" solution which I didn't post because I figure it's more honest to submit what worked, not the smart stuff you found out later.
::: spoiler regex
# find all numerals and number in English from the string $line and put them into the array @spelled my @spelled = ( $line =~ (m/(?=(\d{1}|one|two|three|four|five|six|seven|eight|nine))/g );:::
datarama@awful.systems · 4 pts · 2y
self@awful.systems · 4 pts · 2y
fuck yes scheme. you might have just inspired me to write some Lisp Machine Lisp solutions, since I might need a Lisp Machine codebase to test one of my side projects
datarama@awful.systems · 4 pts · 2y
froztbyte@awful.systems · 3 pts · 2y
I like your condensed solve method, yay for concise code
datarama@awful.systems · 1 pts · 2y
swlabr@awful.systems · 4 pts · 2y
I am using dart to develop my dart chops. DM me for code.
froztbyte@awful.systems · 4 pts · 2y
Nice, I’ll be reading :D
(Possibly have some dart coming up on a thing soon)
swlabr@awful.systems · 4 pts · 2y
I'll be honest: so far, Dart is pretty rubbish for this kind of exercise for the simple reason that their Strings aren't just arrays of chars. There's no native isDigit, for example. Otherwise, I've been using it with Flutter and have been happy with my experience.
I'm only posting code when I think I've done something interesting or if there's a notable language feature to show off, but so far, no dice.
swlabr@awful.systems · 4 pts · 2y
::: spoiler 1 a,b: as a professional software engineer, I know that googling regex syntax and getting it right will take longer than manually writing string comparisons, so... here's all you need to see of my final solution.
:::
swlabr@awful.systems · 3 pts · 2y
::: spoiler 4 a Not so bad today. I bit the bullet and tried to see if dart has tuples or similar. It does, by the name of "records". Now instead of pretending I'm writing in C/C++, I can pretend I'm writing in python.
Anyway, a) is a pretty straightforward job-interview style question, except AoC doesn't care about efficiency. Still, we all have our own versions of pride, so I did it with a set (Though whether or not dart's native Set is tree or hash based is not known to me right now).
:::
::: spoiler code
:::
::: spoiler 4b b) was a little harder, and definitely a possible trap for overthinking. I think the easiest way to think about solving this is as if it is a dynamic programming problem (though it kinda isn't).
So the general approach is to formulate it like this:
T_n(total cards won by card n) = M_n(total matches for card n) + CW_n(total cards won by the copies that card n wins).
and
CW_n =
Caching T_n is the DP trick to making this performant (though once again, it does not need to be)
Anyway, the above approach is the top-down version of the DP; the bottom-up version is what I actually started with in my head. I gave up on that approach because I felt like the logic was too hard for me to figure out. ::: ::: spoiler code:
:::
swlabr@awful.systems · 3 pts · 2y
::: spoiler 3 a I read this wrong initially and thought that you only needed to check adjacency on the same line. Whoops! Then I wrote a bad algorithm that finds numbers THEN searches for symbols. That alg isn't inherently bad, except... :::
::: spoiler 3 b If I had chosen a symbol first approach, I would not have had as much pain as I did here. Also, I probably under and overthought this one. I went with my first idea, which was guaranteed to work.
The approach this time was:
A simpler approach would consider that you only have two numbers on the same line for the same gear if the character in the gear column is a non-digit; otherwise, if a number is adjacent to a gear, there is only one on that row. Union-find is completely overkill, but I like using it even when I don't need to.
Anyway, upon reflecting on this, while the general approach is fine, I didn't think too hard about the implementation and just ended up with globs of overly memoized spaghetti. I probably should check if Dart has a python-like tuple object or similar. Whatever. Behold!
:::
swlabr@awful.systems · 3 pts · 2y
::: spoiler 3b redux I took out the union find, the code is simpler and more readable now. I also leaned in to using null values, which is gross but whatever, it works.
:::
swlabr@awful.systems · 3 pts · 2y
::: spoiler 2 a,b: This one is just a test of whether or not your language lets you split strings.
Only novel thing I did here was recognise that red, green and blue end in different letters, so no need to string match the whole world, just check the last letter of the colour. :::
swlabr@awful.systems · 2 pts · 2y
7 Camel Cards
::: spoiler a, b I decided to write some classes and enums for this, which paid off a little in part b) when I could reuse most of my code. :::
swlabr@awful.systems · 2 pts · 2y
::: spoiler 6 a,b Very easy today. You don't need any programming to solve this; it's a series of inequalities. That said, the brute force calculation is fast enough if you don't want to think and is quite honestly faster than solving this mathematically.
So that being said, here's the mathematic solution:
The inequality to solve is just: x = time holding the button
x^2 - tx + d < 0
which occurs when x is between (t/2) +/- (t^2 - 4d). The total ways are the total integers between those two numbers, exclusive.
Writing that all in code is a little tricky thanks to int/double rounding/truncation intricacies, so the brute force solution is "better". :::
swlabr@awful.systems · 2 pts · 2y
::: spoiler General I used a class this time because it looked like it might be helpful. I don't think it turned out to be that useful. Still, you can see Dart's interesting constructor and getter syntax on display. :::
::: spoiler a. Pretty straightforward, though I didn't read the format correctly and had the destination/source data reversed. Oops! Luckily, my performance here will in no way affect my future career. :::
::: spoiler b. I didn't read the prompt correctly, which tripped me up. Also, while my solution is correct, it assumes that the input could be trickier than what the problem threw at me. Specifically, the edge case I had in mind was a range of values split into many subintervals and needing to track those mappings. I threw in some print statements to discover that intervals were never split beyond two subintervals, which was disappointing. Oh well- being correct is the best feeling you can have if you are otherwise empty inside.
Other than processing the input in the form of intervals, I don't think there were any notable tricks at play here, so this was more of an exercise in implementing code cleanly, which I struggle with. :::
swlabr@awful.systems · 1 pts · 2y
8
::: spoiler Hint for b The brute solution will take ~100 hours on my machine. You will need (to fudge) some very basic number theory to make it faster. :::
::: spoiler a A straightforward implementation of the traversal was sufficient for performant code. :::
::: spoiler b As suggested by the hint, I tried running the brute force solution. The pseudocode is something like this:
I put a timestamp for every 100mil iterations, which ticked once every two seconds.
So, how do we solve it? As mentioned in my hint, some fudged number theory is required.
Long story short, each ghost will eventually reach an end node. They could reach K endpoints, where K is the total number of end nodes available. They will follow the same path in a loop if they traverse infinitely.
The fudge is this: assume each ghost only hits one end node repeatedly. In this case, the solution is just the LCM of the number of steps it takes for each ghost to reach its end node from the initial state.
If given a case where a ghost hits multiple unique endpoints, you can probably find the configuration of counts and LCMs to yield the smallest one, but that proof is left to the reader.
The answer that I got was in the magnitude of 10 trillion, close to 20 trillion. :::
swlabr@awful.systems · 1 pts · 2y
9
::: spoiler a,b This was another implementation exercise, i.e., can you implement this algorithm as specified? So pretty easy. a) took me about 30 minutes or so to code up, b) took like a minute after that since the problem is so similar.
Interestingly, the problem itself is based on a method for finding closed-form general formulae for progressions of integers. You can use it to develop a formula for sums of squares, cubes, quartics etc. or any progression that doesn't seem to have an obvious closed form.
Anyway, there is probably a language where this problem can be solved with a one-liner, I got kinda close, see 9.dart. Check the commits if it's unreadable. :::
gerikson@awful.systems · 4 pts · 2y
Day 2: Cube Conundrum
https://adventofcode.com/2023/day/2
Parsing the puzzle (both instructions and input) was the hardest this time for me.
gerikson@awful.systems · 4 pts · 2y
Perl solution, nothing special: https://github.com/gustafe/aoc2023/blob/main/d02-Cube-Conundrum.pl
zogwarg@awful.systems · 4 pts · 2y
Have been mostly using jq for fun.
Day 1
::: spoiler Part 1
::: First part was easy, and very suited to jq
::: spoiler Part 2
::: Second part was harder than expected, i had to resort to regex.
Day 2
::: spoiler Part 1
::: Not too much trickery in this example.
::: spoiler Part 2
::: Satisifyingly straightfoward edit form part one.
Day 3
::: spoiler Part 1
::: Took More time than i expected, glad i had the idea early to search by the indices of the symbols and not the digits. Not super well suited to jq, unless I'm missing a better solution.
::: spoiler Part 2
::: Not too far of an edit from part one.
zogwarg@awful.systems · 4 pts · 2y
Back to a more straightfoward day, do they make them harder on the weekends?
Day 4 Scratchcards
::: spoiler Part 1
Very suited to JQ, extra trick learned using:
[ match("\\d+"; "g").string | tonumber ]as a parse all ints in line. :::::: spoiler Part 2
Not too much of an edit compared to part one, being able to easily do operations on range of indices is convenient. :::
gerikson@awful.systems · 4 pts · 2y
Historically problems on Sat/Sun have been more challenging than weekdays. However given that the first 7 days are usually “warmup” problems, I’d say this years edition of AoC is more challenging than at least since 2019.
gerikson@awful.systems · 4 pts · 2y
I liked today's puzzle. It was meaty but not frustrating.
gerikson@awful.systems · 4 pts · 2y
Day 11: Cosmic Expansion
https://adventofcode.com/2023/day/11
::: spoiler discussion
After yesterday' fiddle-fest we are back with a straight-forward puzzle. Today we get the return of Manhattan distance, an AoC fav, but this time not spelled out to fool the crafty LLMs.
I made the initial decision not to "move" the galaxies in the initial map, but instead to store an offset that was increased whenever an empty row or column preceding the object was detected. This turned out to make part 2 really easy once I figured out the off-by-one error.
:::
zogwarg@awful.systems · 2 pts · 2y
::: spoiler discussion In retrospect that would have been far better for runtime, my dist function ended up being a tad expensive.
I substituted the rows/columns, with multiplication by the expansion rate if they were all numbers. And then for each galaxy pair do a running sum by going “down” the “right” and adding the distance for each row and column crossed.
https://github.com/zogwarg/advent-of-code/blob/main/2023/jq/11-b.jq
transposeis nice to have in that approach. :::gerikson@awful.systems · 4 pts · 2y
Day 14: Parabolic Reflector Dish
I only managed part 1 today. My enthusiasm for index fiddling is waning rapidly.
zogwarg@awful.systems · 4 pts · 2y
How about not fiddling with indices?
::: spoiler JQ Notfiddlingwithindexification https://github.com/zogwarg/advent-of-code/blob/main/2023/jq/14-a.jq
:::
Similarly tired with index fiddling, I was pretty happy with my approach, which led to satisfying
transposecancelling in part 2. Not the fastest code out there, but it works. Day 14 was actually my favorite one so far ^^.Architeuthis@awful.systems · 4 pts · 2y
Day 4: Scratchcards
Late to the party and never done advents before, I liked how this problem reminded me that tree traversal is thing, almost as much as I don't that so much of my career involves powershell now.
I'm putting everything up at https://github.com/SpaceAntelope/advent-of-code-2023 except the input files.
Using command abbreviations like % and ? to keep the horizontal length friendly to lemmy post areas, they are expanded in git. ::: spoiler Part 2 in Powershell
:::
swlabr@awful.systems · 3 pts · 2y
Happy Holidays everyone. I’ve decided I am going to take a break from aoc to properly rest and recover from my mystery illness. Perhaps I will attempt solves again in the new year.
zogwarg@awful.systems · 3 pts · 2y
Happy holidays!
gerikson@awful.systems · 3 pts · 2y
Happy holidays to you too! I decided this morning that I'm not gonna work myself up missing days, so they are on hold until after xmas for me!
Get well soon!
swlabr@awful.systems · 2 pts · 2y
Thanks dawg. AOC has occupied the working part of my brain but now that it requires more brain wrinklies than usual I’m gonna go back to writing sneers.
sailor_sega_saturn@awful.systems · 3 pts · 2y
I have come up with a more horrifying way to solve Day 1 Part 1 involving C++ implementation defined behavior, but it requires launching two subprocesses for every test case so I'm not sure I have the motivation right now.
Proof of concept: https://www.animeprincess.net/blog/?p=60
swlabr@awful.systems · 1 pts · 2y
I believe you've posted in the wrong thread. This is the awful systems thread, not the awesome systems thread.
zogwarg@awful.systems · 3 pts · 2y
Day 8: Haunted Wasteland
https://adventofcode.com/2023/day/8
Not so easy at least for part two.
::: spoiler spoiler Do you remember high school math, like lowest common multiple, part 2 electric boogaloo. :::
zogwarg@awful.systems · 2 pts · 2y
Cleaned up version of code used to solve part 2 in jq.
::: spoiler Spoiler code section
:::
swlabr@awful.systems · 1 pts · 2y
Seeing you implement gcd/lcm makes me think about the people who are gunning for the AoC leaderboards. What do they have that I don’t?
Asking out of general interest and not any sort of feelings of inadequacy (I swear, behind a face of gritted teeth and obvious seethe).
Like, do they have cron scripts to scrape the prompt and input as soon as it comes out? Libraries of util functions accrued from years of AoC participation? That’s all I’ve thought of and honestly it doesn’t sound implausible if you are hypercompetitive. Like I imagine they just have a raiders of the lost ark warehouse of boilerplate indexed in their memory palace to draw from. And I don’t have that and I am totally not envious at all.
gerikson@awful.systems · 3 pts · 2y
Re: LCM, I figured my favorite Perl library
ntheoryhad it, and I was right! This a godsend for Project Euler, too.(The first year of AoC leaned heavily on these kinds of problems, and Python itertools utterly destroyed the puzzles.)
Re: leaderboard participants - I believe many of them are involved in programming contests, generally, and if you do enough of these, you recognize patterns, and you have routines for a lot of stuff. Also there are tools to download the puzzle inputs automatically.
My personal take on how to do AoC: https://gerikson.com/blog/comp/adventofcode/Howto-AoC.html (maybe already posted, I don't care)
swlabr@awful.systems · 3 pts · 2y
Thanks for the insight. I haven’t done much AoC, and this largely confirms the vibes I’ve been getting this time around.
gerikson@awful.systems · 4 pts · 2y
Much like a high IQ score doesn't show intelligence, but rather the aptitude to take IQ tests, being good at AoC does not show you are a good programmer, but rather that you are good at programming challenges.
zogwarg@awful.systems · 3 pts · 2y
Ah! Thanks for making my notice the GCM -> GCD typo. I'm not gunning for the leaderboards myself, it's pretty hopeless ^^. Yes i am assuming based off of experience and utility tools.
I myself have tools to automatically get the inputs, and submit outputs, but that's more because it pleases me than to actually be fast: https://github.com/zogwarg/advent-of-code/blob/main/functions.sh
(Also completely pointlessly have a functions to extract the session cookie from chrome storage from the CLI, despite being long-lived, and therefore much simpler to simply copy-paste from debugger window)
swlabr@awful.systems · 3 pts · 2y
hey dawg, doing pointless scripting to automate things you don’t need to automate is how we grow as programmers/how the basilisk gets made
gerikson@awful.systems · 3 pts · 2y
Day 9: Mirage Maintenance
My solution: https://github.com/gustafe/aoc2023/blob/main/d09-Mirage-Maintenance.pl
::: spoiler discussion What can I say. Shockingly simple.
I just literally followed the instructions, and got a solution in 20ms. This despite literally creating each intermediate array yet only using the ends. I'm sure I used way to much memory but you know? I'm using a $5/mo VPS for everything and unless I'm barking totally up the wrong tree I've never exceeded its memory limits.
On the subreddit I see people discussing recursion and "dynamic programming" (which is an empty signifier imho) but I really don't see the need, unless you wanna be "elegant"
:::
swlabr@awful.systems · 3 pts · 2y
::: spoiler spoiler DP to me is when you use memoisation and sometimes recursion and you want to feel smarter about what you did.
I also struggle to think of the need for DP, even in a more “elegant” approach. Maybe if you wanted to do an O(n) memory solution instead of n^2, or something. Not saying this out of derision. I do like looking at elegant code, sometimes you learn something.
I feel like there’s an unreadable Perl one line solution to this problem, wanna give that a go, @gerikson? :::
zogwarg@awful.systems · 3 pts · 2y
::: spoiler spoiler Part 2 only, but Part 1 is very similar.
I'm pretty sure you could make this one line and unreadable ^^. :::
swlabr@awful.systems · 3 pts · 2y
Here's where I landed in dart ::: spoiler no comments
:::
swlabr@awful.systems · 3 pts · 2y
Now this is content
froztbyte@awful.systems · 3 pts · 2y
rule 5: one code enters, 7 codes leave
froztbyte@awful.systems · 3 pts · 2y
okay yeah so
p1 part 1 submitted, runs fine. part 2 says "wrong answer for you but right for someone else". doing a debug-print-everything run on the intermediate stages of the data after my regex all seems correct, but it's also 23h50 and it's been a looooong week. so I guess I'll take a look a fresh look at that one in the morning over coffee
part1, badly:
::: spoiler spoiler
:::
(I really wasn't kidding about the badly)
part2:
::: spoiler spoiler missed the
eightwocasechanges:
:::
froztbyte@awful.systems · 1 pts · 2y
swlabr@awful.systems · 3 pts · 2y
21 Step Counter
Starting this thread having only solved a.
::: spoiler A Pretty straightforward. Could probably be done in a few lines with the right syntactic sugar. :::
::: spoiler B This is some game of life thing that I've never implemented or seen an implementation of, so I am altogether lost.
My current code (https://github.com/Fluxward/aoc2023/blob/main/21.dart) has a memoisation based approach but my current ailments are preventing me from really thinking about this properly so I am bowing out until I have the wherewithal. :::
gerikson@awful.systems · 3 pts · 2y
This is the hardest problem of the year so far, based on leaderboard completion times. I'm busy wrapping up work for this year, and looking for a new job, so this will have to be put on the TODO pile
swlabr@awful.systems · 2 pts · 2y
At this point I have officially given up and started looking at other people’s code. I’ll work on it after Imm fully better, it’s too much for me right now.
zogwarg@awful.systems · 3 pts · 2y
Only solved by receving heavy hints from other's solution, and it still took me forever. By far the hardest this year.
swlabr@awful.systems · 2 pts · 2y
Update on B:
::: spoiler still no solve, however Through glancing at someone else's code, I was inspired to just try simulating the A problem beyond 64 steps and seeing the result.
Essentially it reaches a (bi stable?) steady state between two numbers, which makes sense- if you can only make single rook steps, then the reachable squares will alternate every cycle.
Don't know if I'll try solve this again tonight but mentally I have now understood the solution. :::
swlabr@awful.systems · 1 pts · 2y
Update to the update: now fully recovered, I am now trying to finish the last problems.
Solved 21 B!
::: spoiler I spent way too much time on this but it’s fine So my approach to AOC has always been to write a pure coding solution, which finally broke down here.
First, the solve:
I call the unrepeated garden map the “plot”. Each repetition of the plot I call a “grid”. Hope that isn’t confusing.
To see why that last point is true, consider that in order for another grid A to influence an adjacent grid B beyond the moment the adjacent grid is entered, there must be a reachable point further from the midpoint of the edge on the edge of A. However, because the middle row and column are free from rocks, this is never the case. Any influence from A reaches B too late, i.e. reachable squares on B from A will be reachable sooner from just travelling from the entry point on B.
So putting all this together, the way I got the answer was like this:
So I guess the answer I arrived at was what I’d been thinking I should be doing this whole time: a mix of simulating some of the problem and a decent amount of pen and paper work to get the solution out, rather than just pure coding. Fun! :::
swlabr@awful.systems · 3 pts · 2y
Starting a new comment thread for my solutions to 10-19. Double digits, baby! DM for link to code.
swlabr@awful.systems · 4 pts · 2y
::: spoiler a,b a: Just copied what I did for 10 b. and applied it to work here. Had to fiddle with it to make it work, but it worked. I implemented a line-crossing counting algorithm. If you slice up the area into rows, you can tell if you are inside the area if you cross an odd number of lines that vertically cross that row.
b. Pretty much just changed the input parsing and ran the same algorithm. Thought that it might be too slow, but I put in some stopwatch ticks and found out that it should take about 5 minutes to run my a) algorithm to get the answer.
The actual time elapsed was 5m50s.
I miiiiight try make a faster version but my head is too fucked up to care right now, hence me letting the slow algorithm run. :::
swlabr@awful.systems · 4 pts · 2y
Update on 18.
::: spoiler It's not cheating if it's something you learned and forgot from a university course So, I have made my code a million times faster. My original algorithm counted every square of area individually, plus a bunch of other inefficient things.
My new algorithm now uses some swanky computational geometry to calculate the enclosed area. It took a bit of scribbling on a paper pad to get this right. Some notes:
So, here's what I did. I looked at some old lecture notes I had on programming competition computation geometry implementation details. I copied and pasted some code that implemented a counter-clockwise check and one that calculated the area of a simple polygon. That's pretty much all I needed.
Now my code runs in a few milliseconds.
There is almost definitely a more elegant way to do what I did for this iteration but I'm happy to leave that to someone else to figure out (or copy paste from reddit or something). :::
zogwarg@awful.systems · 3 pts · 2y
::: spoiler 18 The beauty is you don't need to keep track of the corners at all: ultimately the area contributed by the perimeter is ( 1/2 * perimeter ) + 1. The short justification is that is if was just ( 1/2 * perimeter ), for every inside corners you overcount by 1/4 and for every outside corner you undercount. And there is exactly 4 more outside corners that inside ones, always. You can justify that by having an arrow follow the eddges, utlmately the arrow must make 1 full turn, each outside corner adds 1/4 turn. each inside corner removes 1/4 turn. :::
swlabr@awful.systems · 2 pts · 2y
I knew there was a better way! Thanks!
::: spoiler perhaps A more elegant proof might show that starting with a rectangle, you have 4 corners contributing 1/4. You can push out parts of the edges of the rectangle to generate more corners, but they will always be in pairs of opposite types. :::
swlabr@awful.systems · 3 pts · 2y
11 ::: spoiler a,b a: So, I've been in the habit of skipping the flavour text and glossing over the prompt. This time, it hurt me badly.
I read the problem as follows: for N galaxies, find N/2 pairings such that the sum of distances is minimal.
At this point, I was like, wow, the difficulty has ramped up. A DP? That will probably run out of memory with most approaches, requiring a BitSet. I dove in, eager to implement a janky data structure to solve this humdinger.
I wrote the whole damn thing. The bitset, the DP, everything. I ran the code, and WOAH, that number was much smaller than the sample answer. I reread the prompt and realised I had it all wrong.
It wasn't all for naught, though. A lot of the convenience stuff I'd written was fine. Also, I implemented a sparse parser, which helped for b.
b: I was hoping they were asking for what I had accidentally implemented for a. My hopes were squandered.
Anyway, this was pretty trivial with a sparse representation of the galaxies. :::
gerikson@awful.systems · 3 pts · 2y
Love ur premature optimization
swlabr@awful.systems · 2 pts · 2y
Watch me as I solve P=NP and then find out I was only supposed to solve fizzbuzz
swlabr@awful.systems · 3 pts · 2y
::: spoiler a,b, not much to say The hardest part has finding the right dart ascii library to use (by default dart treats everything as UTF-16, which is horrible for this sort of thing) and the right data structure (linked hash map, which is a map that remembers insertion order.) :::
gerikson@awful.systems · 4 pts · 2y
::: spoiler spoiler
"you have linked hash maps? LUXURY!"
In my code I had to resort to this sorting
Perl at its best!
:::
swlabr@awful.systems · 2 pts · 2y
I am so mad that I don’t know Perl, honestly.
gerikson@awful.systems · 3 pts · 2y
It's a nice language! And I believe it's what AoC is made in. Certainly a lot of stuff seems tailor-made for the language.
swlabr@awful.systems · 3 pts · 2y
::: spoiler a,b So, like many other problems from this year, this is one of those direct solution problems where there isn't much of a neat trick to getting the answer. You just have to implement the algorithm they specify and hope you can do it correctly.
a) I used a regex to do some parsing because I haven't looked at dart regex much and wanted to dip my toes a little.
I considered doing this "properly" with OO classes and subclasses for the different rules. I felt that it would be too difficult and just wrote something janky instead. In hindsight, this was probably the wrong choice, especially since grappling with all the nullable types I had in my single rule class became a little too complex for my melting brain (it is HOT in Australia right now; also my conjunctivae are infected from my sinus infection. So my current IQ is like down 40 IQ points from its normal value of probably -12)
b) There may have been a trick here to simplify the programming (not the processing). Again, I felt that directly implementing the specified algorithm was the only real way forward. In brief:
Because this is AOC, I assumed that the input would be nice and wouldn't have anything problematic like overlapping ranges, and I was right. I had a very stupid off by one error that took me a while to find as well. :::
The code I have up as of this comment is pretty long and boring, I might try clean it up later.
zogwarg@awful.systems · 3 pts · 2y
Replying in OP: Yeah, Lemmy punishes old threads/posts a bit too much for my taste ^^.
swlabr@awful.systems · 3 pts · 2y
Good note for next year!
swlabr@awful.systems · 3 pts · 2y
update: have cleaned up the code.
swlabr@awful.systems · 3 pts · 2y
::: spoiler a,b part a: nothing to say here.
part b: Before diving into the discussion, I timed how long 1000 cycles takes to compute, and apparently, it would take 1643175 seconds or just over 19 days to compute 1 billion cycles naively. How fun!
So, how do you cut down that number? First, that number includes a sparse map representation, so you can tick that box off.
Second is intuiting that the result of performing a cycle is cyclical after a certain point. You can confirm this after you've debugged whatever implementation you have for performing cycles- run it a few times on the sample input and you'll find that the result has a cycle length of 7 after the second interaction.
Once you've got that figured out, it's a matter of implementing some kind of cycle detection and modular arithmetic to get the right answer without having to run 1000000000 cycles. For the record, mine took about 400 cycles to find the loop. :::
zogwarg@awful.systems · 2 pts · 2y
::: spoiler a,b I took a very similar approach to parts a and b, with the difference that i was too lazy to do titling in each direction, and wanted to abuse regex so Instead i always titled up and rotated, which given my method of tilting up and rotating had some satisfying cancelling of transpose operations: https://github.com/zogwarg/advent-of-code/blob/main/2023/jq/14-b.jq
JQ does allow some nice sortcuts sometimes, again
transposeis nice to have. :::swlabr@awful.systems · 2 pts · 2y
::: spoiler neat! I need like 25 more IQ points before I think of using a transpose in any context :::
swlabr@awful.systems · 2 pts · 2y
17, We’re in the back third now, folks! ::: spoiler a, b A and B were roughly the same difficulty.
So in my first year in university, in my intro DSA class, we learned A*. Have I learned any other ways to search since? Not really. So I used A* here.
It took way longer than it should have to solve, which I blame on my ongoing illness. The main sticking point was that I implemented a class to represent the search state, and since I was going to use it as a key to a map, I implemented a hashcode for it. The rest of the A* code freely flowed from my brain (really the wikipedia pseudocode).
Cue like 40 mins plus of wondering how the test input was searching over millions of states, wondering if I’d fucked up the A* implementation, wondering if the problem was too big for A*, and wondering if it was finally time to take a days break from all the aoc nonsense.
Anyway at some point I realised I forgot to implement the corresponding equals method to my hashcode. Once I had that my code ran in seconds and everything was fine. This sickness is the worst!!! :::
swlabr@awful.systems · 2 pts · 2y
12
::: spoiler a,b Finally! a Dee Pee!!!
This problem was mainly testing:
...which is true of all DP problems, honestly. So given you know and understand how to approach DP problems, this would be more an engineering issue than anything. :::
swlabr@awful.systems · 2 pts · 2y
::: spoiler 16 So, as I've been doing the AoC things, I've been creating small libraries of convenience functions to reuse, hopefully. This time, I reused some things I wrote for problem 10, which was vindicating.
a. was a fun coding exercise. Not much more to say.
b. I lucked out by making a recursive traversal function for a), which let me specify the entry and direction of where my traversal would start. Besides that, similar to a., this was a fun coding exercise. I was surprised that my code (which just ran the function from a) on every edge tile) It only took 2s to run; I thought I might need to memoize some of the results. :::
zogwarg@awful.systems · 2 pts · 2y
::: spoiler 16 a,b Neat!
In my case it was a lot more of headbanging, the traverse function i wrote for part a was way to slow, since JQ isn't happy with loop-heavy assignments (if not buried within C-implemented builtins). Part a completed in ~2seconds, which was never going to do (in hindsight it would have taken me less time to simply let it run slowly), I had to optimize it so that the beams don't step one square at a time, but shoot straight to any obstacle.
It took me waaaay too long to troubleshoot it into something that actually worked. I'm sure there's a compact implementation out there, but my part b ended up looking very meaty (and still took ~30s to run): https://github.com/zogwarg/advent-of-code/blob/main/2023/jq/16-b.jq :::
swlabr@awful.systems · 3 pts · 2y
That’s such a different programming paradigm than I’m used to!
swlabr@awful.systems · 2 pts · 2y
10! ::: spoiler a, b a was relatively straightforward - again, an implementation test.
b was interesting- luckily, I remembered how to test if a point lives inside a curve. The line-crossing count algorithm was a little tricky to write from scratch, but I finally got there. Also, I screwed up for like an hour when I didn't realise you were supposed to count junk pipes as part of the territory. Code wise it got messy, and I've thrown something up on my github, but might try clean it up a little later. :::
swlabr@awful.systems · 3 pts · 2y
Update: I've cleaned up my code and made it "dartier" i.e. I use more neat dart syntactic sugar at the cost of readability.
swlabr@awful.systems · 2 pts · 2y
::: spoiler a,b a. while you can brute force this one in time, one simple trick to make it faster is to treat the symbols as bits and interpret the grid as numbers. It then becomes a matter of locating the reflection point.
b. It's not much of a difference to solve b. The trick here is that if you did the bit stuff I suggested above, you'd quickly realise that a smudge interpreted as a binary number is a power of two. Finding a smudge is equivalent to if the bitwise XOR of two numbers is a power of 2, which can be done with some bitwise magic. :::
swlabr@awful.systems · 3 pts · 2y
Day 20: Pulse Propagation
It feels weird to kick one of these threads off, but hey, here we go.
DM me for access to the code.
::: spoiler a,b A
So following from yesterday where I was punished by not going full OO, I decided, hey, this is a problem in which I can do some OOP, so I did. This took very long to do but I regret nothing. If you look at my code, feel free to click your tongue and shake your head at every opportunity I missed to use a design pattern.
Anyway, after a slight snafu with misunderstanding the FF logic and not spotting that some modules can be dummy modules, it all just worked, and I got my answer.
B
This was a bit of a headscratcher, but the answer was surprisingly simple.
First, the answer. Here's how to do it:
Getting here was a bit weird. I hoped that I could just run the code from A and spit out the answer when rx went low, but as of time of writing I've been running it now on a separate machine for about an hour and still no result.
My next instinct was to simply work it out from pen and paper. I thought it might be possible (it probably is) but decided to at least experimentally see if the states of the modules connected to rx were cyclic first. I did, and that was enough for me to get to the answer.
My answer was about 230 trillion BPs, which, extrapolating on how long execution is taking on my other machine, might take just under 137 years to calculate naively. Fun! :::
gerikson@awful.systems · 4 pts · 2y
I'm having a hard time modeling the network at the moment, there's too much shit to keep track of. Objects might be the solution!
swlabr@awful.systems · 4 pts · 2y
Yeah, I mean it’s always possible to do without all the OO stuff, I just didn’t want to mentally keep tabs on what each variable or method did what. That’s what the code is for!
zogwarg@awful.systems · 3 pts · 2y
::: spoiler [Language: jq] https://github.com/zogwarg/advent-of-code/blob/main/2023/jq/20-b.jq
Completed when waiting for the second leg of my Christmas holidays flight. (It was a long wait, can I blame jet-lag?).
Have a more compact implementation of LCM/GCD, something tells me it will come in handy In future editions. (I’ve also progressively been doing past years)
:::
gerikson@awful.systems · 3 pts · 2y
Day 16: The Floor Will Be Lava
::: spoiler [Language: Perl]
https://github.com/gustafe/aoc2023/blob/main/d16-The-Floor-Will-Be-Lava.pl
:::
datarama@awful.systems · 3 pts · 2y
gerikson@awful.systems · 2 pts · 2y
No-one should begrudge or judge you for taking a day off.
gerikson@awful.systems · 3 pts · 2y
Day 5: If You Give A Seed A Fertilizer
https://adventofcode.com/2023/day/5
Leaderboard completion time: 26m37s, so it's the toughest problem this year so far.
zogwarg@awful.systems · 4 pts · 2y
I liked the slight trickiness of part 2, that the naive implementation would never complete in time.
As always doing a JQ implementation:
::: spoiler Part 1
Some comments:
inputfirst to get the seeds, theninputsto get remaining lines. :::::: spoiler Part 2
Some comments:
[1,2,3] | [ .[] | if . == 2 then . * 10 + 1 , . * 10 + 2 else . end ]->[1, 21, 22, 3]Replaced less-than (and greater-than for symmetry) symbols with full-width version, since lemmy apparently doesn't handle them well within a code block: replacing less than with <
gerikson@awful.systems · 3 pts · 2y
Part 2 is a classic AoC move, where suddenly the problem space becomes much larger.
swlabr@awful.systems · 2 pts · 2y
JQ looks like magic. So short! So clean! What's the catch?
zogwarg@awful.systems · 3 pts · 2y
The main catch is it would often be faster to use a "real" programming langage ^^, both in writing the code, and in execution time for some loop heavy examples: equivalent code that completes say in 1 second in python, completing in 1 minute in jq. Also missing a way to call native libraries, to do stuff like say "md5" (relevant) in past years advents-of-code.
That being said i like the general "pipe", map-reduce feel of the language. Like bash one-liners It can make for very terse implementations. I like to add comments, and indentation to make it readable though.
swlabr@awful.systems · 1 pts · 2y
Thanks for the insight!
gerikson@awful.systems · 1 pts · 2y
I have to punt on part 2 for now, generally shitty day precludes fun coding.
Update done, nothing special
https://github.com/gustafe/aoc2023/blob/main/d05-If-You-Give-A-Seed-A-Fertilizer.pl
gerikson@awful.systems · 3 pts · 2y
Day 3: Gear Ratios
https://adventofcode.com/2023/day/3
Writeup of my solution: https://github.com/gustafe/aoc2023#day-3-gear-ratios
swlabr@awful.systems · 3 pts · 2y
Sorry for the necropost: I have completed all the problems! One of them completely stumped me and I had to cheat. Not going to do a writeup unless requested :)
gerikson@awful.systems · 3 pts · 2y
congrats! I have officially checked out of the competition for the time being. Maybe if I get some spare energy later.
What problem had you stumped?
swlabr@awful.systems · 3 pts · 2y
24b, sort of. The problem came down to “hey do you remember how to do linear algebra?” and the answer was: dawg, I barely know normal algebra. I had solved the vibes of the problem but none of the details, though.
gerikson@awful.systems · 3 pts · 2y
Day 7: Camel Cards
https://adventofcode.com/2023/day/7
So far, my favorite puzzle. Challenging but fair. Also plays to Perl's strengths.
Leaderboard completion time: 16 minutes flat, so not a pushover.
gerikson@awful.systems · 3 pts · 2y
Comments and code: https://github.com/gustafe/aoc2023/tree/main#day-7-camel-cards
zogwarg@awful.systems · 3 pts · 2y
Day 6: Wait For It
https://adventofcode.com/2023/day/6
::: spoiler Alternate spoiler name - for part 2
Do you remember highschool algebra?Can you (or your compiler) remember highschool algebra fast enough to beat out a naïve implementation? :::gerikson@awful.systems · 3 pts · 2y
nice cleanser after yesterday
::: spoiler spoiler it would have taken me longer to figure out the algebra than to just mush the inputs together and get the solution that way (16s runtime) :::
zogwarg@awful.systems · 2 pts · 2y
Day 18: Lavaduct Lagoon
::: spoiler [Language: jq] https://github.com/zogwarg/advent-of-code/blob/main/2023/jq/18-b.jq
Satisfyingly short (in lines, not in time writing) some of the longer part is hexadecimal parsing, that doesn't come natively in JQ, I started doing polygon math from part 1, and what took me the longest was properly handling the area contributed by the perimeter. (I toyed with trying very annoying things like computing the outmost vertex at each turn, which is complicated by the fact that you don't initially know which way the digger is turning, and needing previous and next point to disambiguate).
:::
Day 19: Aplenty
::: spoiler [Language: jq] https://github.com/zogwarg/advent-of-code/blob/main/2023/jq/19-b.jq
Satisfyingly very well suited to JQ once you are used to the
stream,foreach(init; mod; extract)andrecurse(exp)[where every output item of exp as a stream is fed back into recurse] operators. It's a different way of coding but has a certain elegance IMO. This was actually quick to implement, along with re-using the treating a range as a primitive approach of the seeds-to-soil day.EDIT: Less-thans and greater-thans replaced by fullwidth version, because lemmy is a hungry little goblin. :::
swlabr@awful.systems · 3 pts · 2y
::: spoiler Nice! Also, kudos for working with polygon area from the start. I was too invested in reusing my code as discussed elsewhere, but I came around in the end. :::
swlabr@awful.systems · 3 pts · 2y
19 was a real pain in dart.
zogwarg@awful.systems · 2 pts · 2y
Day 17: Clumsy Crucible
Intimidating at first, so I went shopping for christmas presents first, which engaged my brain juices.
::: spoiler [Language: jq] https://github.com/zogwarg/advent-of-code/blob/main/2023/jq/17-b.jq
In hindsight this is is searching for the shortest path in a graph, making sure that each square is actually two nodes, for when approached vertically or horizontally. My shcool days are distant now, and i wonder how far from optimal my implementation is ^^.
Part two was only a small edit from part one for me, and the code actually ran faster! from ~45s -> ~20s. :::
swlabr@awful.systems · 2 pts · 2y
My part b code ran slower :(
zogwarg@awful.systems · 2 pts · 2y
Day 12: Hot springs
https://adventofcode.com/2023/day/12
Where a curse the fact I decided to use JQ and not a "real" programming language.
::: spoiler spoiler Had to resort to memoization, but sadly JQ isn't mega well suited to that. I had to refactor my part 1 function, to make including the "state" at every function call possible. I wish it were as easy as a
@cachedecorator, but i guess this way i had fun (for an arbitrary definition of "fun")Further cleaned up version: https://github.com/zogwarg/advent-of-code/blob/main/2023/jq/12-b.jq
Also lost a fair amount of time not not noticing that the sequence should be joined with
"?"not with"". (that'll teach me to always run on the example before the full input, when execution time is super long).Execution time: 17m10s (without memoization a single row was taking multiple minutes, and there's 1000 rows ^^...)
EDIT: see massive improvement by running in parallel in reply. :::
zogwarg@awful.systems · 3 pts · 2y
A nice workaround to jq single threadedness, since this is maq reduce and safe to parallelize. 17m10s -> 20s !!! ::: spoiler Spoiler link to commit. https://github.com/zogwarg/advent-of-code/commit/fef153411fe0bfe0e7d5f2d07da80bcaa18c952c :::
Not really spoilery details: Revolves around spawing mutiple jq instances and filtering the inputs bassed on a modulo of number of instances:
I use JQ at work, and never really needed this, i guess this trick is nice to have under the belt just in case.
swlabr@awful.systems · 2 pts · 2y
::: spoiler spoiler
Oooh I should run my code without memoization. Or just add a cache hit count.
:::