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
Ruby
!ruby@programming.dev
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?
, andzip
Cleaned up solution:
def get_score(tally) vals = tally.values map = { ->(x) { x.any?(5) } => 7, ->(x) { x.any?(4) } => 6, ->(x) { x.any?(3) && x.any?(2) } => 5, ->(x) { x.any?(3) && tally.all? { |_, v| v != 2 } } => 4, ->(x) { x.count(2) == 2 } => 3, ->(x) { x.one?(2) && tally.all? { |_, v| v <= 2 } } => 2, ->(x) { x.all?(1) } => 1, } map.find { |lambda, _| lambda.call(vals) }[1] end def get_ranking(lines, score_map, scores) lines.zip(scores).to_h.sort do |a, b| a_line, a_score = a b_line, b_score = b if a_score == b_score a_hand, _ = a_line.split b_hand, _ = b_line.split diff = a_hand.chars.zip(b_hand.chars).drop_while { |a, b| a == b }[0] card_1 = score_map.index(diff[0]) card_2 = score_map.index(diff[1]) card_1 <=> card_2 else a_score <=> b_score end end end def calculate_total_winnings(ranking) max_rank = ranking.size (1..max_rank).sum(0) do |rank| line = ranking[rank - 1] _, bid = line[0].split bid.to_i * rank end end score_map_p1 = %w[. . 2 3 4 5 6 7 8 9 T J Q K A] score_map_p2 = %w[. . J 2 3 4 5 6 7 8 9 T Q K A] execute(1) do |lines| scores = lines.map do |line| hand, _ = line.split tally = hand.split('').tally get_score tally end ranking = get_ranking(lines, score_map_p1, scores) calculate_total_winnings ranking end execute(2) do |lines| scores = lines.map do |line| hand, _ = line.split hand_split = hand.split('') tally = hand_split.tally if hand_split.any? { |c| c == 'J' } highest_non_j = tally.reject { |k, v| k == 'J' }.max_by { |k, v| v } if highest_non_j.nil? tally = { 'A': 5 } else tally[highest_non_j[0]] += tally['J'] end tally.delete('J') end get_score tally end ranking = get_ranking(lines, score_map_p2, scores) calculate_total_winnings(ranking) end