What are you coding?

Today I have no time to be coding at night, we’re going to a concert. So I am happy that it’s regex is the solution day at day 3 :slight_smile:

1 Like

Day 3

Here there be spoilers

After reading part 1, I thought, “Oh, well, that’s regexes.” But I don’t like embedding regex-based logic into code; mostly because regexs are hard to visually/mentally parse after coming back to the code months later. I mean, elegantly formatted and commented code itself is hard enough to read after returning cold – regexs are the absolute worst. Probably why I never got into perl as a language.

So I tried to do some nested split() and strip() logic and that got very spaghetti-y very soon. This is worse. Fine: <regex> <regex> <regex>, that was easy, but I’m still not happy about it.

Embarrassingly enough, the hardest part of part2 was realizing (at midnight) that the sample data I used in part1 didn’t contain a valid do() or don't(). =/

Why are you sequencing multiple regex?

I wasn’t. I was trying to not use regexs and chaining other methods together instead.

1 Like

Why would you try not to use them? I mean it is the tool for this particular task, isn’t it?

1 Like

I’m stubborn :slight_smile:

And I think they are ugly in code

1 Like

Stubborn I get, I tried my best for Day 2 not to just try every position until the ideas I had for possible edge cases were driving me mad… and then suddenly everything turned out to be an entirely different bug (grumble stupid java)

But ugly? I disagree! Beautiful, elegant, sharp! Very sharp! As Radho would say :slight_smile:

If you include them in software and not just code of course you need to write all kinds of tests and all that and there are some drawbacks and security issues (kind of like sql injection or so I am told, I keep forgetting the details)… but ugly?

(mul\\((\\d{1,3}),(\\d{1,3})\\)|(do\\(\\)|don't\\(\\)))

The beauty… … I had to blur it or be blinded by it :rofl:

(No no they aren’t pretty, I love them none the less, charisma and “innere Werte”)

2 Likes

r/AdventOfCode is having a field day with the memes.

My favorite:

2 Likes

AoC 3: yeah, glad I’m not doing it in PostScript this year. Not knowing the Rust regex library as well as I might, I ended up doing it in two stages, one to spot a valid op in the first place and a second to capture and decode mul’s arguments.

EC day 12: now that’s a proper AoC type problem, where for the last part you need to dump the simulation and go with algebra so that it completes in reasonable time.

2 Likes

AoC 4: this is actually quite similar to a The Weekly challenge problem that’s coming out next week, and I was able to recycle my code from there.

For part 2

I abandoned the search stack completely, and just looked for each A then analysed its neighbours.

2 Likes

I did the exact same thing. I was kind of disappointed because I spent extra time on part 1 to make it handle arbitrary searches

1 Like

If AoC teaches us anything, it’s “don’t optimise prematurely”. Though it feels good when I’ve written the slightly fancier version and that’s what part 2 turns out to be. :slight_smile:

2 Likes

Maybe that’s the point. AoC is teaching us to hack together part1 because you never know what part2 is going to be.

In the real world, this would translate as, “hack together parts 1, 2, 3, 4, 5, …, n” because you never know what part n+1 will be.

2 Likes

I’m not really into “speed programming” (and the problems drop at 5am in my time zone anyway) so I mostly try to write “good” solutions, reasonably easy to read and understand. Towards the end they usually degenerate a bit…

2 Likes

My goal is typically something that can be returned to later and generally understood without too much scrutiny.

2 Likes

EC day 13: oh hello Dijkstra, I wondered when you would turn up. I found a great library for this in last year’s AoC.

Yeah, the poor bastard who may have to read my code in a year’s time is probably me. As in this case, where I could simply look at what I’d done before and tweak it to fit the new problem.

3 Likes

Today was the day I got to enjoy previous years of implementing a versatile Matrix class for AoC :slight_smile:

My solution not necessarily pretty but it took me no time to implement and the biggest mistakes I made were spelling the search “xmas” instead of “XMAS” and forgetting to remove the 2nd example from my downloaded json because it is not really an example.

My Matrix class can find all coordinates for a given number/character and from there I can move into any of the 8 directions.

The nicest trick I figured out for part 2 was that instead of doing lengthy if/else I could just sum up the two adjacent letters and see if they matched ‘M’ + ‘S’ … character math is the best math.

Having had a long day, I really appreciate being done quickly.

2 Likes

Heh, I just doubled up the string and searched it for MMSS.

My code’s all going up on codeberg at Firedrake/adventofcode2024: Advent of Code, 2024 - Codeberg.org and Firedrake/everybody.codes2024: Solutions for Everybody.Codes 2024 - Codeberg.org .

2 Likes

Day 5 was surprisingly straightforward. Mostly I had to spend quite a few lines on parsing the input. Having two types of input does that usually. There is more “does it match?” and removing empty lines code.

The tricky moment in the first half was realizing that each “first” number can appear multiple times… and in part two I remembered a nice Java feature called a Comparator and then it was just calling sort on the list :slight_smile:

Of course I had multiples of messing up the boolean comparisons…

I have not deactivated my AI assistant.

It does some interesting auto completions even with code like this now. It has advanced quite a bit from last year. BUT… the chances are not even 50% that it guesses right what I want to do. Still in some cases it provides helpful syntax reminders… in others it is just costing me time when I can’t immediately spot a subtle mistake.

Overall it helps avoid typing boring things more than it hinders me.
And of course it does not solve the puzzle. I am not even trying to use it for that. Where is the fun in that?

Everybody Codes 14:

Part 1 is a pretty straightforward regex parser; all I care about is the maximum Z value.

Part 2 is a three-dimensional grid fill.

Part 3 fooled me at first by the worked example not demonstrating that it did actually need pathfinding, not just a Manhattan distance. But given that, throw it at Dijkstra and extract the results.

Advent of Code 5:

“Aha”, I thought, “topological sorting, I know this”. But actually no, because at least my main puzzle input has cycles in it if you resolve it completely (i.e. A before B, B before C, C before A—which is fine if you never have all of A, B and C in the same update), but if you do a full toposort on the input, it can’t complete.

Part 1: So instead a priority pair is a Map of earlier to a Set of later. I check each pair of pages in the update for orderedness: if the later one doesn’t appear in a priority list, or the earlier one doesn’t appear in the later one’s priority list, they are at least not out of order.

Part 2: I write a custom sort function. checking both page numbers and returning any ordering information we can find.

2 Likes