this post was submitted on 07 Dec 2023
18 points (100.0% liked)

Advent Of Code

773 readers
8 users here now

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.

AoC 2023

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

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
18
submitted 11 months ago* (last edited 11 months ago) by Ategon to c/advent_of_code
 

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


๐Ÿ”’ Thread is locked until there's at least 100 2 star entries on the global leaderboard

๐Ÿ”“ Thread has been unlocked after around 20 mins

you are viewing a single comment's thread
view the rest of the comments
[โ€“] snowe 2 points 11 months ago* (last edited 11 months ago)

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?, and zip


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