πͺ - 2023 DAY 7 SOLUTIONS -πͺ
Day 7: Camel Cards
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/ , pastebin, or github (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
π Thread is locked until there's at least 100 2 star entries on the global leaderboard
π Thread has been unlocked after around 20 mins
28 replies
Nim
I wrote some nice code for sorting poker hands, just defining the
<and==operations for myCardSetandHandtypes, and letting the standard library's sort function handle the rest.It was quite frustrating to be told that my answer was wrong, though. I dumped the full sorted hand list and checked it manually to make sure everything was working properly, and it was. Wasted a few hours trying to figure out what was wrong. Ended up grabbing someone else's code and running it in order to compare the resulting hand list. Theirs was clearly ordered wrong, but somehow ended up with the correct answer?
Turns out that Camel Cards isn't Poker. -_-
Rather than rewrite my code entirely, I settled on some slightly ugly hacks to make it work for Camel Cards, and to handle the wildcards in part 2.
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: ![email protected]
It's Uiua time!
It works, but even I can't understand this code any more as I'm well into my second beer, so don't put this into production, okay? (Run it here if you dare.)
Lord have mercy upon our souls
Lots and lots of print statements :-)
Dart
I'm glad I took the time to read the directions very carefully before starting coding :-)
Top Tip: my ranking of hand types relies on the fact that if you count instances of each face and sort the resulting list from high to low, you get a list that when compared with lists from other hands gives an exact correspondence with the order of the hand types as defined, so no need for a bunch of if/thens, just
Otherwise it should all be pretty self-explanatory apart from where I chose to map card rank to hex digits in order to facilitate sorting, so 'b' means 'J'!
Nim
Part 1 is just a sorting problem. Nim's standard library supports sorting with custom compare functions, so I only had to implement cmp() for my custom type and I was done in no time.
To get the star in Part 2 I was generating every possible combination of card hands with Jokers replaced by other cards. It was pretty fast, under a second. Didn't figure out the deterministic method by myself, but coded it after couple hints from Nim Discord people.
Didn't expect an easy challenge for today, but was pleasantly surprised. No weird edge cases, no hidden traps, puzzle text was easy to understand and input parsing is painless.
Total runtime: 1 ms
Puzzle rating: Almost Pefect 9/10
Code: day_07/solution.nim
Scala3
My solution in Rust.
Took me way too long to realize I could simply add jokers to the count of the most common card in the hand.
[language: Lean4]
As with the previous days: I'll only post the solution and parsing, not the dependencies I've put into separate files. For the full source code, please see github.
The key idea for part 2 was that ::: spoiler Spoiler it doesn't make any sense to pick different cards for the jokers, and that it's always the highest score to assign all jokers to the most frequent card. :::
::: spoiler Solution
:::
Python
Part 1: https://github.com/porotoman99/Advent-of-Code-2023/blob/main/Day%207/part1.py
:::spoiler Code
:::
Part 2: https://github.com/porotoman99/Advent-of-Code-2023/blob/main/Day%207/part2.py
:::spoiler Code
:::
I tried to do this one as quickly as possible, so the code is more messy than I would prefer, but it works, and I don't think the solution is too bad overall.
Edit: I went back and changed it to be a bit better. Here are my new solutions:
Part 1 v2: https://github.com/porotoman99/Advent-of-Code-2023/blob/main/Day%207/part1v2.py
:::spoiler Code
:::
Part 2 v2: https://github.com/porotoman99/Advent-of-Code-2023/blob/main/Day%207/part2v2.py
:::spoiler Code
:::
Crystal
got stuck on both parts due to silly mistakes.
On the other hand I'm no longer behind!
::: spoiler code
:::
This wasn't too bad. Had a worried moment when the part 2 solution took more than half a second. Maybe a better solution that brute forcing all the joker combinations, but it worked.
::: spoiler Python
:::
I barely registered a difference between part 1 and part 2.
I suppose it took about 3.5 times as long, but I didn't notice :P
Edit: I realize that I made the implicit assumption in my solution that it doesn't make sense to have multiple jokers be interpreted as different values. i.e., The hand with the maximum value will have all Jokers interpreted as the same other card. I think that is true though. It worked out for me anyway.
Yea I was thinking there might be a simplification trick, but also figured "there can't be that many combinations right?" I suspect that was probably an intended optimisation.
I think one doesn't need to generate all combinations. All combinations using cards already present in the hand should be enough (since a joker can only increase the value of the hand by being grouped with existing cards (since in this game having four of a kind is always better than having any hand with a four of a kind/full house and having 3 is always better than any hand with pairs, and having a pair is better than any card without any cards of the same kind)). This massively decreases the amount of combinations needed to be generated per jokery hand.
Raku
My hand-type strength calculations could probably be trimmed down a bit. I didn't hit any big issues today.
View code on github
:::spoiler Code (note: doesn't currently display correctly on Lemmy website)
:::
Ruby
[email protected]
https://github.com/snowe2010/advent-of-code/blob/master/ruby_aoc/2023/day07/day07.rb
Gonna clean it up now, but pretty simple at the end of it all. Helps that ruby has several methods to make this dead simple, like
tally,any?,all?, andzipCleaned up solution:
Language: Python
This was fun. More enjoyable than I initially thought (though I've done card sorting code before).
::: spoiler Part 1
This was pretty straightforward: create a histogram of the cards in each hand to determine their type, and if there is a tie-breaker, compare each card pairwise. I use the Counter class from collections to do the counting, and then had a dictionary/table to convert labels to numeric values for comparison. I used a very OOP approach and wrote a magic method for comparing hands and used that with Python's builtin sort. I even got to use
Enum!:::
::: spoiler Part 2
For the second part, I just had to add some post-processing code to convert the jokers into actual cards. The key insight is to find the highest and most numerous non-Joker card and convert all the Jokers to that card label.
This had two edge cases that tripped me up:
'JJJJJ': There is no other non-Joker here, so I messed up and ranked this the lowest because I ended up removing all counts.
'JJJ12': This also messed me up b/c the Joker was the most numerous card, and I didn't handle that properly.
Once I fixed the post-processing code though, everything else remained the same. Below, I only show the parts that changed from Part A.
:::
GitHub Repo
C#
Not too bad - I just scored every hand for the first part so I could easily sort it.
For the second part I just brute forced the replacements for the hand type matchinge (first digit of score)
::: spoiler Task1 public class Day7Task1:IRunnable {
:::
::: spoiler Task2
:::
Rust
Getting the count of each card, the two highest counts easily show what type of hand we have. For part 2 I just added the number of jokers to the highest count.
I spent some time messing around with generics to minimize code duplication between the solutions to both parts. I could have absolutely just copied everything and made small changes, but now my solution is generic over puzzle parts.
Factor on github (with comments and imports):
Oh boy. bitwise nonsense. Ok, can you explain it to me? I'm terrible at bitwise stuff.
That is a really cool solution. Thanks for the explanation! I took a much more... um... naive path lol.
I didn't multiply the streak, I just took the jokers and added them to the highest hand already in the list. Is that not what you did? It looked the same to me.
Wow, this is exactly what I did, but in C#. That's cool.
JavaScript
Ended up misreading the instructions due to trying to go fast. Built up a system to compare hand values like its poker before I realized its not poker
Likely last day im going to be able to write code for due to exams coming up
Code Link
:::spoiler Code Block
:::
Two days, a few failed solutions, some misread instructions, and a lot of manually parsing output data and debugging silly tiny mistakes... but it's finally done. I don't really wanna talk about it.
https://github.com/capitalpb/advent_of_code_2023/blob/main/src/solvers/day07.rs