π - 2025 DAY 6 SOLUTIONS - π
Day 6: Trash Compactor
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
Uiua
I'm new to Uiua, so probably not the best way to express the solution!
( Ν‘Β° ΝΚββ¬β΄β¬β΄
Looks good. I like how you managed to use the same structure for Parts 1 and 2; that's more than I did. And yours is faster than mine too.
One of us! One of us!
Nice use of the inversion, I always forget that's a thing
nushell
I was afk when the puzzle went up so I had another go at doing it on my phone in Turmux with my shell's scripting language. It's quite nice how your shell is also a REPL so you can build up the answer in pieces, although I wrote a file for the second part.
Part 2
Uiua
This was fun :D
I had a fun experience just throwing the strings with both numbers and spaces at the parse function. In the online pad, everything worked out fine but running the same code on my input locally gave me a "invalid float literal" error.
I thought I'd missed some edge case in the real input again, like is often the case.
Turns out that the Uiua version I used locally had a bug that's fixed in the latest build. For once it wasn't directly my fault ^^
Run with example input
:::spoiler Code
:::
Haskell
There's probably a really clever way of abstracting just the difference between the two layouts.
Ulua probably has a single character that rotates the input -90 degrees...
β...
Your code really reads like your explaining the solution out loud. That's so elegant!
Thanks! I try to write code to be readable by humans above all else.
@Camille @lwhjp +1 for Literate Programming!
Haskell
Kotlin
I'm not fully happy with my parsing today, but oh well. I also thought about just plain building the grid and then rotating it, but "normal input parsing" works too.
::: spoiler Solution
:::
full code on Codeberg
I also thought about trying to rotate, but not for very long. Mine would be a bit simpler if I'd done what you did and build the number string and then check if it's blank.
Managed to keep it compact, but boy, do I hate cephalopod math >_<
Python
Nim
The hardest part was reading the part 2 description. I literally looked at it for minutes trying to understand where the problem numbers come from and how they're related to the example input. But then it clicked.
The next roadblock was that my template was stripping whitespace at the end of the last line, making parsing a lot harder. I've replaced
strip()withstrip(chars={'\n'})to keep the trailing space intact.Runtime:
1.4 ms618 ΞΌs::: spoiler view code
:::
Full solution at Codeberg: solution.nim
Python
For part1, regex is king. Thought about rotating the grid for part2, but going column by column is simple enough.
::: spoiler view code
:::
Go
Damn, I actually reeaally enjoyed this one! I didn't expect the twist of part 2, but somehow it wasn't that hard to manage.
Here is my modern solution:
::: spoiler day06.go
:::
Rust
Mainly difficult parsing today.
View on github
Janet
C
Well so much for reading a grid of ints in part 1! For part 2, initially I reworked the parsing to read into a big buffer, but then thought it would be fun to try and use memory-mapped I/O as not to use any more memory than strictly necessary for the final version:
::: spoiler Code
:::
Excel
Pt1 - Its a solution, but the column width on Numbers prevents a full solution
https://docs.google.com/spreadsheets/d/e/2PACX-1vTzFApVAk9k5Q4EQhozST1HftYLgVzApgxjQx7MAl5AAE5eWEvrQnDQWmdj7J2Hyw/pub?output=ods
If I get p2 working, I'll post a new link. Dont think i will have much luck though.
Rust
Pt1 easy, pt2 cry
edit: Updated with more iterring :)
::: spoiler view code
:::
Why not use
.iter().sum()and.iter().product()?Because I wasn't aware of them, thanks!
Go
Part 2: Read the whole input in a rune matrix. Scan it column by column, store the numbers as you go, ignoring all spaces, and store the operand when you find it. When you hit an empty column or the end, do the operation and add it to the total.
::: spoiler spoiler
:::
Ruby
I decided to rotate the entire input character-by-character, then parse the numbers (see the full source here)
Rust
Finally having some fun with iters. I think part 2 came out nicely once I figured I can rotate the whole input to get numbers that look like ours.
Q: Anyone have any tips how to reason about the intermediate types in a long iter chain? I'm currently placing dummy maps, and on occasion, even printing the values from them when I get too stuck.
::: spoiler the code
:::
Kotlin
More grid stuff and two-dimensional problem solving, I like it!
The first part just requires extracting the numbers and operators, transposing the grid and summing/multiplying the numbers.
The second part is also not too hard. I just search for the numbers in the transposed grid, making sure to leave out the last column. That one might contain an operator ("+" or "*"). Remember it for later. If the entire row is made of spaces, we have finished parsing a math problem. Just remember to account for the last one! π
As with part one, just reduce the found problems and we're done!
This solution requires the trailing spaces to be present in the input files. I had to disable an option in my IDE to prevent it from breaking my nice solution.
Code on Github ::: spoiler Code
:::
(Browser-based) Javascript
I got lazy and lucky writing the first part; hard-coded the number of lines / operands and it worked on the first try! I didn't factor in the final block not being padded by spaces on both sides for part 2, and so needed to test on the example code β which has 3 lines of operands instead of the problem input's 4 lines, so I had to properly solve the problem across all possible amounts of lines of input π©.
I am very jealous of all y'all who have a transpose function available in your language of choice.
::: spoiler Code
:::
Uiua
Messy code that might be worth revisiting later, but array handling is right up Uiua's street.