using System.Collections.Generic;
private static void Main(string[] args)
Dictionary<char, int> CardStrength = new Dictionary<char, int>
{'2', 1}, {'3', 2}, {'4', 3}, {'5', 4}, {'6', 5}, {'7', 6}, {'8', 7}, {'9', 8}, {'T', 9}, {'J', 10}, {'Q', 11}, {'K', 12}, {'A', 13}
Console.WriteLine("Advent of Code 2023 - Day 7");
Console.WriteLine("Camel Cards");
Console.WriteLine("--------------------");
var handsWithBids = ParseInput(input);
foreach (var handWithBid in handsWithBids)
Console.WriteLine($"Hand {handWithBid.Id}: {handWithBid.Hand} Bid: {handWithBid.Bid} Rank: {handWithBid.CardScore}");
RankHands(handsWithBids);
handsWithBids.Sort((h1, h2) => h1.CardScore.CompareTo(h2.CardScore));
foreach (var handWithBid in handsWithBids)
Console.WriteLine($"Hand {handWithBid.Id}: {handWithBid.Hand} Bid: {handWithBid.Bid} CardScore: {handWithBid.CardScore} Rank: {rank} Winnings: {rank * handWithBid.Bid}");
total += rank * handWithBid.Bid;
Console.WriteLine($"Total Winnings: {total}");
void RankHands(List<CardHand> cardHands)
foreach (var cardHand in cardHands)
var cardCounts = cardHand.Hand.GroupBy(c => c).ToDictionary(g => g.Key, g => g.Count());
var handType = GetHandType(cardCounts);
cardHand.CardScore = GetHandStrength(handType, cardHand.Hand);
int GetHandType(Dictionary<char, int> cardCounts)
if (cardCounts.Count == 1) return 7;
if (cardCounts.Values.Max() == 4) return 6;
if (cardCounts.Values.Max() == 3 && cardCounts.Values.Min() == 2) return 5;
if (cardCounts.Values.Max() == 3) return 4;
if (cardCounts.Values.Count(v => v == 2) == 2) return 3;
if (cardCounts.Values.Max() == 2) return 2;
BigInteger GetHandStrength(int handType, string hand)
return handType * 10000000000 +
CardStrength[hand[0]] * 100000000 +
CardStrength[hand[1]] * 1000000 +
CardStrength[hand[2]] * 10000 +
CardStrength[hand[3]] * 100 +
static List<CardHand> ParseInput(string input)
List<CardHand> hands = new List<CardHand>();
string[] lines = input.Split(Environment.NewLine);
foreach (string line in lines)
hands.Add(new CardHand(id++, line.Substring(0, 5), int.Parse(line.Substring(5))));
public int Id { get; set; }
public string Hand { get; set; }
public int Bid { get; set; }
public BigInteger CardScore { get; set; }
public CardHand(int id, string hand, int bid, int rank = 0)