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

  • morrowind
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    7 months ago

    Crystal

    got stuck on both parts due to silly mistakes.
    On the other hand I’m no longer behind!

    code
    input = File.read("input.txt").lines
    rank = {
    	'J' => 1,
    	'2' => 2,
    	'3' => 3,
    	'4' => 4,
    	'5' => 5,
    	'6' => 6,
    	'7' => 7,
    	'8' => 8,	
    	'9' => 9,
    	'T' => 10,
    	# 'J' => 11,
    	'Q' => 12,
    	'K' => 13,
    	'A' => 14
    }
    
    hand = input.map do |line|
    	split = line.split
    	weights = split[0].chars.map {|c| rank[c]}
    	{weights, split[1].to_i}
    end
    
    hand.sort! do |a, b|
    	a_rank = get_rank(a[0], true)
    	b_rank = get_rank(b[0], true)
    	
    	# puts "#{a}-#{a_rank} #{b}-#{b_rank}"
    	next  1 if a_rank > b_rank
    	next -1 if b_rank > a_rank
    
    	val = 0
    	5.times do |i| 
    		val =  1 if a[0][i] > b[0][i]
    		val = -1 if b[0][i] > a[0][i]
    		break unless val == 0
    	end
    	val
    end
    
    sum = 0
    hand.each_with_index do |card, i|
    	sum += card[1]*(i+1)
    end
    puts sum
    
    
    def get_rank(card : Array(Int32), joker = false ) : Float64 | Int32
    	aa = card.uniq
    
    	if joker
    		card = aa.map { |c|
    			combo = card.map {|a| a == 1 ? c : a }
    			{combo, get_rank(combo)}
    		}.max_by {|a| a[1]}[0]
    		aa = card.uniq
    	end
    	
    	rank = 6 - aa.size
    	case rank
    	when 3
    		return 3.5 if card.count(aa[0]) == 3
    		return 3   if card.count(aa[0]) == 2
    		return 3   if card.count(aa[1]) == 2
    		return 3.5
    	when 4
    		return 4 if card.count(aa[0]) == 3 || card.count(aa[0]) == 2
    		return 4.5
    	else 
    		return rank
    	end
    end