Day 4: Scratchcards
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)
- Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ or pastebin (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)
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
🔒This post will be unlocked when there is a decent amount of submissions on the leaderboard to avoid cheating for top spots
🔓 Unlocked after 8 mins
46 Comments
mykl@lemmy.world · 8 pts · 2y
I had to give Uiua another go today. (run it here)
Gobbel2000@feddit.de · 3 pts · 2y
WTF
UndercoverUlrikHD@programming.dev · 3 pts · 2y
morrowind@lemmy.ml · 2 pts · 2y
I'm fascinated and horrified
cacheson@kbin.social · 5 pts · 2y
Nim
This one was pretty simple, just parse the numbers into sets and check the size of the intersection. Part 2 just made the scoring mechanism a little more complicated.
CommunityLinkFixer@lemmings.world · 1 pts · 2y
Hi there! Looks like you linked to a Lemmy community using a URL instead of its name, which doesn't work well for people on different instances. Try fixing it like this: !nim@programming.dev
pngwen@lemmy.sdf.org · 1 pts · 2y
That's some elegant code! Then again, I suppose that's the beauty of nim.
cacheson@kbin.social · 3 pts · 2y
I'm rather spoiled by python, so I feel like it could be more elegant. xD
But yeah, I do like how this one turned out, and nim runs a whole lot faster than python does. I really like nim's "method call syntax". Instead of having methods associated with an individual type, you can just call any procedure as
x.f(remaining_args)to callfwithxas its first argument. Makes it easy to chain procedures. Since nim is strongly typed, it'll know which procedure you mean to use by the signature.Andy@programming.dev · 3 pts · 2y
Aside from the general conciseness, the "universal function call syntax" is my favorite aspect of nim.
If you want to take chaining procedures to the next level, try a concatenative language like Factor (I have a day 4 solution in this thread -- with no assignment to variables).
I also suggest having a look at Roc if you want a functional programming adventure, which offers great chaining syntax, a very friendly community, and is in an exciting development phase.
cacheson@kbin.social · 2 pts · 2y
Thank you, I'll keep those in mind. Functional programming seems interesting to me, but I don't have any practical experience with it. At some point I want to learn one of the languages that are dedicated to it. Nim does have some features for enabling a functional style, but the overall flexibility of the language probably makes it harder to learn said style.
Gobbel2000@feddit.de · 5 pts · 2y
Rust
This one wasn't too bad. The example for part 2 even tells you how to process everything by visiting each card once in order. Another option could be to recursively look at all won copies, but that's probably much less efficient.
lwhjp@lemmy.sdf.org · 4 pts · 2y
Haskell
11:39 -- I spent most of the time reading the scoring rules and (as usual) writing a parser...
sjmulder@lemmy.sdf.org · 2 pts · 2y
Still trying to make sense of it but that part two fold is just jummy!
soulsource@discuss.tchncs.de · 2 pts · 2y
I'm really impressed by your part 2. And I thought my solution was short...
lwhjp@lemmy.sdf.org · 2 pts · 2y
Not familiar with Lean4 but it looks like the same approach. High five!
janAkali@lemmy.one · 4 pts · 2y
LANGUAGE: Nim
Welcome to the advent of parsing!
Took me a lot more time than it should (Please, don't check prior commits 😅).
day_04.nim
mykl@lemmy.world · 2 pts · 2y
This should be the motto of AoC
hades@lemm.ee · 3 pts · 2y
mykl@lemmy.world · 3 pts · 2y
Dart Solution
Okay, that's more like it. Simple parsing and a bit of recursion, and fits on one screen. Perfect for day 4 :-)
snowe@programming.dev · 3 pts · 2y
Ruby
!ruby@programming.dev
Somehow took way longer on the second part than the first part trying a recursive approach and then realizing that was dumb.
https://github.com/snowe2010/advent-of-code/blob/master/ruby_aoc/2023/day04/day04.rb
Nighed@sffa.community · 1 pts · 2y
Edit: Sorry, should have read your code first, you made it work too. if it works it works, Recursive solutions just click for me over other solutions.
I made the recursion work, went to a depth of 24 for my input set.
::: spoiler Recursive C#
:::
porotoman99@lemmy.world · 3 pts · 2y
Python
Part 1: https://github.com/porotoman99/Advent-of-Code-2023/blob/main/Day%204/part1.py
Part 2: https://github.com/porotoman99/Advent-of-Code-2023/blob/main/Day%204/part2.py
I found out about this event for the first time yesterday and was able to get caught up in time for day 4.
capitalpb@programming.dev · 3 pts · 2y
I enjoyed this one. It was a nice simple break after Days 1 and 3; the type of basic puzzle I expect from the first few days of Advent of Code. Pretty simple logic in this one, I don't think I would change too much. I'm sure I'll find a way to clean up how it's written a bit, but I'm happy with this one today.
https://github.com/capitalpb/advent_of_code_2023/blob/main/src/solvers/day04.rs
corristo@programming.dev · 3 pts · 2y
APL
I'm using this years' AoC to learn (Dyalog) APL, so this is probably terrible code. I'm happy to receive pointers for improvement, particularly if there is a way to write the same logic with tacit functions or inner/outer products that I missed.
mykl@lemmy.world · 2 pts · 2y
I just posted a solution in Uiua, which is also probably equally terrible, but if you squint you can see some similarities in our approaches.
corristo@programming.dev · 2 pts · 2y
I haven't heard of Uiua before, but I can read some things :D I like the idea of rotating the vector instead of manually padding it with the required number of leading zeroes!
mykl@lemmy.world · 1 pts · 2y
I think it's only a few months old. I've enjoyed playing with it because it allows me to use stack manipulation as an alternative to combinators and every symbol has a fixed arity both of which make it feel a lot more accessible to me.
I was very pleased with myself when I thought of that rotation trick :-)
Andy@programming.dev · 3 pts · 2y
Factor on github (with comments and imports):
__init__@programming.dev · 3 pts · 2y
(python) Much easier than day 3.
::: spoiler code
:::
UlrikHD@programming.dev · 2 pts · 2y
Feels like the challenges are getting easier than harder currently. Fairly straightforward when doing it the lazy way with python. ::: spoiler Python
bob_lemon@feddit.de · 1 pts · 2y
That int-call on the return value for the point value is a good idea. I manually returned 0 if there were no matches.
UlrikHD@programming.dev · 1 pts · 2y
I personally would prefer the if check and return 0 in most instances just because it's clearer and more readable. But the two previous functions were one-liners so it just looked better if get_winnings() also was.
lwhjp@lemmy.sdf.org · 1 pts · 2y
Puzzles on the weekend are usually a bit more involved than weekdays. 23 is probably going to be a monster this year...
sjmulder@lemmy.sdf.org · 2 pts · 2y
Language: C
Another day of parsing, another day of
strsep()to the rescue. Today was one of those satisfying days where the puzzle text is complicated but the solution is simple once well understood.GitHub link
:::spoiler Code (29 lines)
mykl@lemmy.world · 1 pts · 2y
Looks like Lemmy's odd parsing broke your comment at the less-than sign.
sjmulder@lemmy.sdf.org · 1 pts · 2y
That's unfortunate, although it looks good on my instance (SDF). Anything I could do?
mykl@lemmy.world · 1 pts · 2y
Ohh that’s interesting, I’ve seen a few comments about mishandling of special chars in code blocks and assumed it was a server issue, maybe it’s fixed in newer releases or perhaps it’s client side.
Adanisi@lemmy.zip · 2 pts · 2y
pngwen@lemmy.sdf.org · 2 pts · 2y
PHP
Today was the easiest day so far IMHO. Today, I coded in PHP, a horrible language that produces even worse code. (Ok, full confession, I fed my family for about half a decade on PHP. I seemed to have gotten stuck with it, and so I earned a PhD to escape it.)
Anyway, the only trouble I had was I forgot about the explode function's capacity to return empty strings. Once I filtered those I had the correct answer on the first one, and then 10 minutes later I had the second part. I wrote my code true to raw php's awful idioms, though I didn't make it web based. I read from stdin.
My code is linked on github:
pnutzh4x0r@lemmy.ndlug.org · 2 pts · 2y
Language: Python
::: spoiler Part 1
Sets really came in handy for this challenge, as did recognizing that you can use powers of two to compute the points for each card. I tried using a regular expression to parse each card, but ended up just doing it manually with split :|
:::
::: spoiler Part 2
This took me longer than I wished... I had to think about it carefully before seeing how you can just keep track of the counts of each card, and then when you get to that card, you add to its copies your current count.
:::
GitHub Repo
bugsmith@programming.dev · 2 pts · 2y
Late as always (actually a day late by UK time).
My solution to this one runs slow, but it gets the job done. I didn't actually need the CardInfo struct by the time I was done, but couldn't be bothered to remove it. Previously, it held more than just count.
Day 04 in Rust 🦀
View formatted on GitLab
Massahud@programming.dev · 2 pts · 2y
Language: Python
Github
Catching up missed days.
Nighed@sffa.community · 2 pts · 2y
C# Recursion Time! (max depth 24)
Today I learnt how to get multiple captures out of the same group in Regex. I also learnt how much a console line write slows down your app.... (2 seconds without, never finished with)
::: spoiler Task1
:::
::: spoiler Task2
:::
Jummit@lemmy.one · 2 pts · 2y
Nice and easy.
::: spoiler Lua
morrowind@lemmy.ml · 2 pts · 2y
Crystal
late because I had to skip two days of aoc. Fairly easy
soulsource@discuss.tchncs.de · 1 pts · 2y
[Language: Lean4]
I'll only post the actual parsing and solution. I have written some helpers which are in other files, as is the main function. For the full code, please see my github repo.
I'm pretty sure that implementing part 2 in a naive way would cause Lean to demand a proof of termination, what might not be that easy to supply in this case... Luckily there's a way more elegant and way faster solution than the naive one, that can use structural recursion and therefore doesn't need an extra proof of termination.
::: spoiler Solution
:::
Ategon@programming.dev · 1 pts · 2y
[JavaScript] Swapped over to javascript from rust since I want to also practice some js. Managed to get part 1 in 4 minutes and got top 400 on the global leaderboard. Second part took a bit longer and took me 13 mins since I messed up by originally trying to append to the card array. (eventually swapped to keeping track of amounts in a separate array)
:::spoiler Part 1
:::
:::spoiler Part 2
:::
Code Link
Ategon@programming.dev · 1 pts · 2y
Improvement I found afterwards: