AdventOfCode 2023 Day07 Part1
This commit is contained in:
61
AdventOfCode2023.Tests/Day07_Tests.cs
Normal file
61
AdventOfCode2023.Tests/Day07_Tests.cs
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
namespace AdventOfCode2023.Tests;
|
||||||
|
|
||||||
|
public class Day07_Tests
|
||||||
|
{
|
||||||
|
private readonly string[] _example = {
|
||||||
|
"32T3K 765",
|
||||||
|
"T55J5 684",
|
||||||
|
"KK677 28",
|
||||||
|
"KTJJT 220",
|
||||||
|
"QQQJA 483",
|
||||||
|
};
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ResolvePart1__Example()
|
||||||
|
{
|
||||||
|
Day07 day = new();
|
||||||
|
|
||||||
|
string result = day.ResolvePart1(_example);
|
||||||
|
|
||||||
|
Assert.Equal("6440", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CamelCard_Type__Examples()
|
||||||
|
{
|
||||||
|
Day07.CamelCard card_FiveOfAKind = new("AAAAA");
|
||||||
|
Assert.Equal(Day07.CamelCard.Types.FiveOfAKind, card_FiveOfAKind.Type);
|
||||||
|
|
||||||
|
Day07.CamelCard card_FourOfAKind = new("AA8AA");
|
||||||
|
Assert.Equal(Day07.CamelCard.Types.FourOfAKind, card_FourOfAKind.Type);
|
||||||
|
|
||||||
|
Day07.CamelCard card_FullHouse = new("23332");
|
||||||
|
Assert.Equal(Day07.CamelCard.Types.FullHouse, card_FullHouse.Type);
|
||||||
|
|
||||||
|
Day07.CamelCard card_ThreeOfAKind = new("TTT98");
|
||||||
|
Assert.Equal(Day07.CamelCard.Types.ThreeOfAKind, card_ThreeOfAKind.Type);
|
||||||
|
|
||||||
|
Day07.CamelCard card_TwoPair = new("23432");
|
||||||
|
Assert.Equal(Day07.CamelCard.Types.TwoPair, card_TwoPair.Type);
|
||||||
|
|
||||||
|
Day07.CamelCard card_OnePair = new("A23A4");
|
||||||
|
Assert.Equal(Day07.CamelCard.Types.OnePair, card_OnePair.Type);
|
||||||
|
|
||||||
|
Day07.CamelCard card_HighCard = new("23456");
|
||||||
|
Assert.Equal(Day07.CamelCard.Types.HighCard, card_HighCard.Type);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CamelCard_CompareTo__Examples()
|
||||||
|
{
|
||||||
|
Day07.CamelCard card_01 = new("33332");
|
||||||
|
Day07.CamelCard card_02 = new("2AAAA");
|
||||||
|
Assert.Equal(-1, card_01.CompareTo(card_02));
|
||||||
|
Assert.Equal(1, card_02.CompareTo(card_01));
|
||||||
|
|
||||||
|
Day07.CamelCard card_77888 = new("77888");
|
||||||
|
Day07.CamelCard card_77788 = new("77788");
|
||||||
|
Assert.Equal(-1, card_77888.CompareTo(card_77788));
|
||||||
|
Assert.Equal(1, card_77788.CompareTo(card_77888));
|
||||||
|
}
|
||||||
|
}
|
||||||
172
AdventOfCode2023/Day07.cs
Normal file
172
AdventOfCode2023/Day07.cs
Normal file
@@ -0,0 +1,172 @@
|
|||||||
|
namespace AdventOfCode2023;
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
--- Day 7: Camel Cards ---
|
||||||
|
|
||||||
|
Your all-expenses-paid trip turns out to be a one-way, five-minute ride in an airship. (At least it's a cool airship!) It drops you off at the edge of a vast desert and descends back to Island Island.
|
||||||
|
|
||||||
|
"Did you bring the parts?"
|
||||||
|
|
||||||
|
You turn around to see an Elf completely covered in white clothing, wearing goggles, and riding a large camel.
|
||||||
|
|
||||||
|
"Did you bring the parts?" she asks again, louder this time. You aren't sure what parts she's looking for; you're here to figure out why the sand stopped.
|
||||||
|
|
||||||
|
"The parts! For the sand, yes! Come with me; I will show you." She beckons you onto the camel.
|
||||||
|
|
||||||
|
After riding a bit across the sands of Desert Island, you can see what look like very large rocks covering half of the horizon. The Elf explains that the rocks are all along the part of Desert Island that is directly above Island Island, making it hard to even get there. Normally, they use big machines to move the rocks and filter the sand, but the machines have broken down because Desert Island recently stopped receiving the parts they need to fix the machines.
|
||||||
|
|
||||||
|
You've already assumed it'll be your job to figure out why the parts stopped when she asks if you can help. You agree automatically.
|
||||||
|
|
||||||
|
Because the journey will take a few days, she offers to teach you the game of Camel Cards. Camel Cards is sort of similar to poker except it's designed to be easier to play while riding a camel.
|
||||||
|
|
||||||
|
In Camel Cards, you get a list of hands, and your goal is to order them based on the strength of each hand. A hand consists of five cards labeled one of A, K, Q, J, T, 9, 8, 7, 6, 5, 4, 3, or 2. The relative strength of each card follows this order, where A is the highest and 2 is the lowest.
|
||||||
|
|
||||||
|
Every hand is exactly one type. From strongest to weakest, they are:
|
||||||
|
|
||||||
|
Five of a kind, where all five cards have the same label: AAAAA
|
||||||
|
Four of a kind, where four cards have the same label and one card has a different label: AA8AA
|
||||||
|
Full house, where three cards have the same label, and the remaining two cards share a different label: 23332
|
||||||
|
Three of a kind, where three cards have the same label, and the remaining two cards are each different from any other card in the hand: TTT98
|
||||||
|
Two pair, where two cards share one label, two other cards share a second label, and the remaining card has a third label: 23432
|
||||||
|
One pair, where two cards share one label, and the other three cards have a different label from the pair and each other: A23A4
|
||||||
|
High card, where all cards' labels are distinct: 23456
|
||||||
|
|
||||||
|
Hands are primarily ordered based on type; for example, every full house is stronger than any three of a kind.
|
||||||
|
|
||||||
|
If two hands have the same type, a second ordering rule takes effect. Start by comparing the first card in each hand. If these cards are different, the hand with the stronger first card is considered stronger. If the first card in each hand have the same label, however, then move on to considering the second card in each hand. If they differ, the hand with the higher second card wins; otherwise, continue with the third card in each hand, then the fourth, then the fifth.
|
||||||
|
|
||||||
|
So, 33332 and 2AAAA are both four of a kind hands, but 33332 is stronger because its first card is stronger. Similarly, 77888 and 77788 are both a full house, but 77888 is stronger because its third card is stronger (and both hands have the same first and second card).
|
||||||
|
|
||||||
|
To play Camel Cards, you are given a list of hands and their corresponding bid (your puzzle input). For example:
|
||||||
|
|
||||||
|
32T3K 765
|
||||||
|
T55J5 684
|
||||||
|
KK677 28
|
||||||
|
KTJJT 220
|
||||||
|
QQQJA 483
|
||||||
|
|
||||||
|
This example shows five hands; each hand is followed by its bid amount. Each hand wins an amount equal to its bid multiplied by its rank, where the weakest hand gets rank 1, the second-weakest hand gets rank 2, and so on up to the strongest hand. Because there are five hands in this example, the strongest hand will have rank 5 and its bid will be multiplied by 5.
|
||||||
|
|
||||||
|
So, the first step is to put the hands in order of strength:
|
||||||
|
|
||||||
|
32T3K is the only one pair and the other hands are all a stronger type, so it gets rank 1.
|
||||||
|
KK677 and KTJJT are both two pair. Their first cards both have the same label, but the second card of KK677 is stronger (K vs T), so KTJJT gets rank 2 and KK677 gets rank 3.
|
||||||
|
T55J5 and QQQJA are both three of a kind. QQQJA has a stronger first card, so it gets rank 5 and T55J5 gets rank 4.
|
||||||
|
|
||||||
|
Now, you can determine the total winnings of this set of hands by adding up the result of multiplying each hand's bid with its rank (765 * 1 + 220 * 2 + 28 * 3 + 684 * 4 + 483 * 5). So the total winnings in this example are 6440.
|
||||||
|
|
||||||
|
Find the rank of every hand in your set. What are the total winnings?
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Day07 : IDay
|
||||||
|
{
|
||||||
|
public string ResolvePart1(string[] inputs)
|
||||||
|
{
|
||||||
|
List<Hand> hands = inputs.Select(input => new Hand(input)).ToList();
|
||||||
|
hands.Sort((handA, handB) => handB.Card.CompareTo(handA.Card));
|
||||||
|
|
||||||
|
long sum = 0;
|
||||||
|
for (int i = 0; i < hands.Count; i++)
|
||||||
|
{
|
||||||
|
sum += hands[i].Bid * (i + 1);
|
||||||
|
}
|
||||||
|
return sum.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public string ResolvePart2(string[] inputs)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CamelCard
|
||||||
|
{
|
||||||
|
private readonly static char[] Labels = { 'A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2' };
|
||||||
|
|
||||||
|
public enum Types
|
||||||
|
{
|
||||||
|
FiveOfAKind = 1,
|
||||||
|
FourOfAKind,
|
||||||
|
FullHouse,
|
||||||
|
ThreeOfAKind,
|
||||||
|
TwoPair,
|
||||||
|
OnePair,
|
||||||
|
HighCard,
|
||||||
|
}
|
||||||
|
|
||||||
|
private string CardLabels { get; }
|
||||||
|
public Types Type { get; }
|
||||||
|
|
||||||
|
public CamelCard(string cardLabels)
|
||||||
|
{
|
||||||
|
if (cardLabels.Length != 5) { throw new ArgumentException("cardLabels must be a string of 5 characters"); }
|
||||||
|
CardLabels = cardLabels;
|
||||||
|
Type = CalculateType(cardLabels);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Types CalculateType(string cardLabels)
|
||||||
|
{
|
||||||
|
Dictionary<char, int> dictLabelCounts = new();
|
||||||
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
char label = cardLabels[i];
|
||||||
|
if (dictLabelCounts.TryGetValue(label, out int value))
|
||||||
|
{
|
||||||
|
value++;
|
||||||
|
dictLabelCounts[label] = value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dictLabelCounts.Add(label, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dictLabelCounts.Count == 1) { return Types.FiveOfAKind; }
|
||||||
|
if (dictLabelCounts.Count == 5) { return Types.HighCard; }
|
||||||
|
if (dictLabelCounts.Count == 2)
|
||||||
|
{
|
||||||
|
if (dictLabelCounts.Any(p => p.Value == 4)) { return Types.FourOfAKind; }
|
||||||
|
return Types.FullHouse;
|
||||||
|
}
|
||||||
|
if (dictLabelCounts.Count == 3)
|
||||||
|
{
|
||||||
|
if (dictLabelCounts.Any(p => p.Value == 3)) { return Types.ThreeOfAKind; }
|
||||||
|
return Types.TwoPair;
|
||||||
|
}
|
||||||
|
if (dictLabelCounts.Any(p => p.Value == 2)) { return Types.OnePair; }
|
||||||
|
|
||||||
|
return Types.HighCard;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int CompareTo(CamelCard other)
|
||||||
|
{
|
||||||
|
if (Type < other.Type) { return -1; }
|
||||||
|
if (Type > other.Type) { return 1; }
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
while (i < 5 && CardLabels[i] == other.CardLabels[i]) { i++; }
|
||||||
|
if (i == 5) { return 0; }
|
||||||
|
|
||||||
|
int indexOfLabel = Array.IndexOf(Labels, CardLabels[i]);
|
||||||
|
int indexOfLabelOther = Array.IndexOf(Labels, other.CardLabels[i]);
|
||||||
|
if (indexOfLabel < indexOfLabelOther) { return -1; }
|
||||||
|
if (indexOfLabel > indexOfLabelOther) { return 1; }
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Hand
|
||||||
|
{
|
||||||
|
public CamelCard Card { get; }
|
||||||
|
public long Bid { get; }
|
||||||
|
|
||||||
|
public Hand(string strHand)
|
||||||
|
{
|
||||||
|
string[] strHandParts = strHand.Split(' ');
|
||||||
|
Card = new CamelCard(strHandParts[0]);
|
||||||
|
Bid = Convert.ToInt64(strHandParts[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
1000
AdventOfCode2023/inputs/Day07.txt
Normal file
1000
AdventOfCode2023/inputs/Day07.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user