🌚 - 2024 DAY 4 SOLUTIONS - 🌚
Day 4: Ceres Search
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
I struggled a lot more when doing list slices that I would've liked to
Haskell
Uiua
Just part1 for now as I need to walk the dog :-)
[edit] Part 2 now added, and a nicer approach than Part 1 in my opinion, if you're able to keep that many dimensions straight in your head :-)
[edit 2] Tightened it up a bit more.
I'm not even sure how to write most of these characters
The operators have all got ascii names you can type, and the formatter converts them to the symbols. It's a bit odd but really worthwhile, as you get access to the powerful array handling functionality that made solving today's challenges so much more straightforward than in other languages.
It looks quite functional indeed
Nim
Could be done more elegantly, but I haven’t bothered yet.
Codeberg repo
Haskell
Popular language this year :)
I got embarrassingly stuck on this one trying to be clever with list operations. Then I realized I should just use an array...
Haskell
C
What can I say, bunch of for loops! I add a 3 cell border to avoid having to do bounds checking in the inner loops.
::: spoiler Code
:::
https://github.com/sjmulder/aoc/blob/master/2024/c/day04.c
Factor
::: spoiler spoiler
:::
Better viewed on GitHub.
Rust
Ugh. Spent way too long on today's. Should have just used my own grid structure from last year. I will likely refactor to use that. Even though it's likely a super slow implementation, the convenience of dealing with it is better than shoehorning in the
grid::Grid<T>from that crate.::: spoiler solution (no supporting code)
::: And here's a link to the Github if you care to see the gross supporting code :D
Nim
Part 1 was done really quickly. Part 2 as well, but the result was not accepted...
Turns out +MAS isn't actually a thing :P
python
::: spoiler solution
:::
Uiua
This one was nice. The second part seemed quite daunting at first but wasn't actually that hard in the end.
Run with example input here
Rust
I had a hunch about part two that didn't pay off, so I over-coded this instead of just using an array of arrays.
Rust
Blunt force grid navigation https://gitlab.com/landreville/advent-of-code-2024/-/blob/main/src/bin/04.rs
I tried to think of some clever LINQ to do this one, but was blanking entirely.
So naïve search it is.
::: spoiler C#
:::
I haven't quite started yet, and this one does feel like a busy work kinda problem. I was wondering if I could write something to rotate the board and do the search, but I think that might be not worth the effort
Rust
One of those with running through tricky grid indices. The vector types from the euclid crate helped in dealing with positions.
::: spoiler Code
:::
(also on github)
Kotlin
C#
Go
Just a bunch of ifs and bounds checking. Part 2 was actually simpler.
::: spoiler Code
:::
Raku
Oof, my struggle to make custom index walking paths for part 1 did not pay off for part 2.
::: spoiler Solution
:::
github
Python
::: spoiler
:::
Julia
Had some time to clean up the code today since the solution was quite straight forward after making a plan on how to approach it.
::: spoiler spoiler
:::
Python
Essentially I'm extracting strings from the word search and compare them to the desired value. For part one that means extracting from an X in eight directions. Because I'm reading from the central X outwards, I don't need to reverse any of them.
Part two reads two strings in an X-shape around the coordinates of each X. The resulting strings are filtered down to include only "MAS" and "SAM". If there are exactly two strings we found an X-MAS.
Smalltalk
I could have done it in 2 fns if I made them more generic, but couldn't be bothered
Part 2
Elixir
Zig
Lisp
Not super happy with the code, but it got the job done.
::: spoiler Part 1 and 2
:::
Part 1:
Part 2:
I actually found part two A LOT easier than part 1.
Rust
I'm a day behind on this one due to a lot of work with my job and school.
The standard library
windowsmethod and pattern matching have been carrying me this year so far.