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
33 Comments
skissue@programming.dev · 5 pts · 252d
Uiua
I'm new to Uiua, so probably not the best way to express the solution!
Deebster@programming.dev · 6 pts · 252d
Quant@programming.dev · 3 pts · 251d
( Ν‘Β° ΝΚββ¬β΄β¬β΄
mykl@lemmy.world · 4 pts · 252d
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.
Quant@programming.dev · 2 pts · 251d
One of us! One of us!
Nice use of the inversion, I always forget that's a thing
Deebster@programming.dev · 5 pts · 253d
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
lwhjp@piefed.blahaj.zone · 4 pts · 253d
Haskell
There's probably a really clever way of abstracting just the difference between the two layouts.
CameronDev@programming.dev · 5 pts · 253d
Ulua probably has a single character that rotates the input -90 degrees...
skissue@programming.dev · 5 pts · 252d
β...
Camille@lemmy.ml · 4 pts · 252d
Your code really reads like your explaining the solution out loud. That's so elegant!
lwhjp@piefed.blahaj.zone · 3 pts · 252d
Thanks! I try to write code to be readable by humans above all else.
troi@techhub.social · 2 pts · 252d
@Camille @lwhjp +1 for Literate Programming!
Quant@programming.dev · 4 pts · 251d
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
:::
LeixB@lemmy.world · 3 pts · 252d
Haskell
eco_game@discuss.tchncs.de · 3 pts · 253d
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
chunkystyles@sopuli.xyz · 3 pts · 252d
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.
GiantTree@feddit.org · 2 pts · 244d
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
:::
Chais@sh.itjust.works · 2 pts · 239d
Managed to keep it compact, but boy, do I hate cephalopod math >_<
Python
Jayjader@jlai.lu · 2 pts · 243d
(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
:::
urandom@lemmy.world · 2 pts · 252d
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
:::
reboot6675@sopuli.xyz · 2 pts · 252d
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
:::
janAkali@lemmy.sdf.org · 2 pts · 253d
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
landreville@lemmy.world · 2 pts · 252d
Ruby
I decided to rotate the entire input character-by-character, then parse the numbers (see the full source here)
Pyro@programming.dev · 2 pts · 253d
Python
For part1, regex is king. Thought about rotating the grid for part2, but going column by column is simple enough.
::: spoiler view code
:::
tranzystorek_io@beehaw.org · 2 pts · 253d
Janet
CameronDev@programming.dev · 2 pts · 252d
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.
Camille@lemmy.ml · 2 pts · 253d
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
:::
strlcpy@lemmy.sdf.org · 2 pts · 253d
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
:::
Gobbel2000@programming.dev · 2 pts · 253d
Rust
Mainly difficult parsing today.
View on github
CameronDev@programming.dev · 2 pts · 253d
Rust
Pt1 easy, pt2 cry
edit: Updated with more iterring :)
::: spoiler view code
:::
Deebster@programming.dev · 2 pts · 253d
Why not use
.iter().sum()and.iter().product()?CameronDev@programming.dev · 2 pts · 253d
Because I wasn't aware of them, thanks!
mykl@lemmy.world · 1 pts · 253d
Uiua
Messy code that might be worth revisiting later, but array handling is right up Uiua's street.