You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
advent-of-code/2023/ruby/day_07.rb

51 lines
1.3 KiB

HAND_STRENGTH = [
[5], # five of a kind
[1, 4], # four of a kind
[2, 3], # full house
[1, 1, 3], # three of a kind
[1, 2, 2], # two pair
[1, 1, 1, 2], # one pair
[1, 1, 1, 1, 1], # high card
].reverse.each.with_index.to_h { [_1, _2] }
10 months ago
module PartOne
CARD_STRENGTH = ((?2..?9).to_a + %w[ T J Q K A ]).each.with_index.to_h
def self.strength(cards)
tally = cards.tally
strengths = [
HAND_STRENGTH.fetch(tally.values.sort),
*cards.map { CARD_STRENGTH.fetch(_1) },
]
strengths.map { (?a.ord + _1).chr }.join
10 months ago
end
end
module PartTwo
CARD_STRENGTH = ([?J] + (?2..?9).to_a + %w[ T Q K A ]).each.with_index.to_h
def self.strength(cards)
tally = cards.tally
jokers = tally.delete(?J) || 0
counts = tally.values.sort
if counts.empty?
counts = [5]
else
counts[-1] += jokers
10 months ago
end
strengths = [
HAND_STRENGTH.fetch(counts),
*cards.map { CARD_STRENGTH.fetch(_1) },
]
strengths.map { (?a.ord + _1).chr }.join
10 months ago
end
end
hands = ARGF.read.scan(/(\w+)\s+(\d+)/).to_h { [_1.chars, _2.to_i ] }
10 months ago
p hands
# .sort_by {|(hand,_)| PartOne::strength(hand) }
.sort_by {|(hand,_)| PartTwo::strength(hand) }
10 months ago
.map.with_index {|(_,bid),i| bid * (i+1) }
.sum