18
๐ช - 2023 DAY 7 SOLUTIONS -๐ช
(programming.dev)
An unofficial home for the advent of code community on programming.dev!
Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.
Solution Threads
M | T | W | T | F | S | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 |
Icon base by Lorc under CC BY 3.0 with modifications to add a gradient
console.log('Hello World')
Language: Python
This was fun. More enjoyable than I initially thought (though I've done card sorting code before).
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
!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