Mercurial > repos > other > adventofcode2023
diff day7.rb @ 9:9ec95ff0d33d
Add Day 7 solutions with string sorting
Each hand gets a score based on the ranking, then concats the
card values to get a sortable string for ranking
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Thu, 07 Dec 2023 21:06:48 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/day7.rb Thu Dec 07 21:06:48 2023 +0000 @@ -0,0 +1,37 @@ +#! /usr/bin/env ruby + +require 'set' + +if ARGV.length != 1 + abort("Incorrect arguments - needs input file") +elsif not File.exist? (ARGV[0]) + abort("File #{ARGV[0]} did not exist") +end + +file = ARGV[0] + +Hand = Struct.new(:cards, :score, :bid) + +$card_scoring = ("1".."9").to_a.concat(["T", "J", "Q", "K", "A"]) + +FULL_HOUSE = Set.new([2,3]) +TWO_PAIR = {2 => 2, 1 => 1} + +def score_hand(cards) + card_vals = cards.each_char.map {|c| $card_scoring.index(c)} + card_counts = card_vals.group_by{|v| v}.map {|k, v| v.length} + card_count_counts = card_counts.group_by{|v| v}.map{|k,v| [k, v.length]}.to_h + of_a_kind = card_counts.max + rank = of_a_kind * 2 # 5, 4 or 3 of a kind or a pair => 10, 8, 6 and 4 + if Set.new(card_counts) == FULL_HOUSE + rank = 7 + elsif card_count_counts == TWO_PAIR + rank = 5 + end + card_vals.reduce("#{rank-1}") {|str, v| "#{str}.#{(v+1).to_s.rjust(2, '0')}"} +end + +hands = File.open(file, "r").each_line(chomp: true).map {|line| cards, bid = line.split(" "); Hand.new(cards, score_hand(cards), bid.to_i)} + +puts hands.sort {|a, b| a.score <=> b.score}.map.with_index(1) {|val, i| i * val.bid}.sum +# Rest of algorithm here \ No newline at end of file