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

Advent Of Code

761 readers
1 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
 

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
[โ€“] morrowind@lemmy.ml 2 points 11 months ago* (last edited 11 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