8
votes
Day 2: Fulminating Farmland
Today's problem description: https://adventofcode.com/2026/day/2
Please post your solutions in your own top-level comment. Here's a template you can copy-paste into your comment to format it nicely, with the code collapsed by default inside an expandable section with syntax highlighting (you can replace python with any of the "short names" listed in this page of supported languages):
<details>
<summary>Part 1</summary>
```python
Your code here.
```
</details>
Took some time to clean this up a bit before posting it, though it could probably still stand to be made more succinct. I used the waypoint characters directly when solving this initially, but that made part 2 especially kind of ugly. Instead, I now convert the waypoints to integers (from 0 to 5), which as a bonus allows part 1 to be done more efficiently. Part 2 is handled by keeping track of how many of each waypoint remain, and repeatedly selecting the most optimal waypoint that's still available.
Solution (Lily)
This is kind of brute-force and ugly, especially the Part 2 solution. I believe that it could be made a decent bit shorter, but oh well. Namely, I think there's a static ordering that is always optimal.
Part 1
Part 2
Visualization
For part 2, you're correct that there's a static order. When the colour is the same, the step size is always 6, so you only need to maximize the step size when the colour changes. So if you can find the optimal ordering of 'roygbv', you've found the optimal ordering of the sorted list.
EDIT: I looked at your Reddit post and it seems like you've already figured this out.
This could be way cleaner... the second part was a waste of time. Not that these should be way harder, but three of four have been trivial.
Google Sheets
When I started doing Part 2 for a moment I thought I was getting to reuse Day 01's code, but the ordering of instructions always been the same saved me from abusing BTreeMap a second time.
Both Parts (Rust)
For part 2, I spotted that you just need the length of the string and which colours occur in order to calculate the furthest distance, but I couldn't figure out how to cleanly count the jumps for the occurring colours in one line without using my previous algorithm. I didn't consider that I could just assume that all the colours occur, due to the length of the string
Part 1
Part 2
I'm still working on part 2 but figured I'd share my part 1:Both parts in Gleam!
Part 1
It doesn't feel super idiomatic maybe? I dunno, functional programming is fun but I think I don't do it enough so I always make unreadable code lol.
Part 2
consume_sorted()is actually a noop right now because I got kinda stuck with looping in gleam :P butttt I realized if I just get the counts of the waypoints it's trivial to do the rest.e.g.
[#(0, 2), #(2, 2), #(4, 2)]from one of the examples (where 0 is "r", 1 is "o", etc. and the second value is the count of that waypoint in the program), we can do 26 because we start at r so that's the longest, then the next longest is to 4 so 26+4 and we've got[#(2, 2), #(4, 1]left, so 26+4+16 then repeat for the 2 26+4+16+4+1*6. Works the same for the large input so I just did that in a calculator and got the answer. I still can't wrap my head around how to implement the algorithm without loops! Not used to this stuff.I actually didn't even finish my program for part 2 because I'm so bad at gleam but I figured out it's pretty easy to get the answer (more in the details).
Not a terribly "clean" solution; just a do-what-the-problem-says approach. It does turn out you can greedily optimize part 2 and it works just fine, to reduce 991 loops down to 5, but I'm not sure if that would hold generally. (I think I saw someone say that would be NP-hard, so it probably wouldn't be appropriate for day 2.)
Part 1
Part 2
Here's my solution in Guile
Day 2: Fulminating Farmland
Solutions for both parts in Lua:
Both parts