Part 2 fühlt sich zwar sehr dirty an, aber das Ergebnis passt zumindest
Spoiler anzeigen
Bald ist meine arbeitsfreie Woche rum, dann hat sich's mit meinen zeitnahen Posts.
VB.NET-Quellcode
- Friend Class ValueCalculatorForDay7
- Private ReadOnly CardDecks As New List(Of CardDeck)
- Function GetValueFor(FilePath As String, Part As Integer) As Integer
- Dim RawDataLines = GetRawDataFrom(FilePath)
- ParseRawData(RawDataLines, Part)
- SetRanksForCardDecks(Part)
- Return GetValueForGame()
- End Function
- Private Function GetRawDataFrom(FilePath As String) As IEnumerable(Of String)
- Return IO.File.ReadAllLines(FilePath)
- End Function
- Private Sub ParseRawData(RawDataLines As IEnumerable(Of String), Part As Integer)
- RawDataLines.ToList.ForEach(Sub(x) CardDecks.Add(New CardDeck(x.Split, Part)))
- End Sub
- Private Sub SetRanksForCardDecks(Part As Integer)
- Dim OrderedCardDeck = CardDecks.OrderBy(Function(x) x.CardDeckType).ThenBy(Function(x) x.TranslatedCardOrder(Part = 2))
- For i = 0 To OrderedCardDeck.Count - 1
- OrderedCardDeck(i).RankInGame = i + 1
- Next
- End Sub
- Private Function GetValueForGame() As Integer
- Return CardDecks.OrderBy(Function(x) x.RankInGame).Sum(Function(x) x.GetValue)
- End Function
- End Class
- Friend Enum CardDeckType
- HighCard
- OnePair
- TwoPair
- ThreeOfAKind
- FullHouse
- FourOfAKind
- FiveOfAKind
- End Enum
- Friend Class CardDeck
- Property CardOrder As String
- Property CardDeckType As CardDeckType
- Property Bid As Integer
- Property RankInGame As Integer
- Private ReadOnly CardValueMappingsForPart1 As New Dictionary(Of Char, Char) From {{"2"c, "A"c}, {"3"c, "B"c}, {"4"c, "C"c}, {"5"c, "D"c}, {"6"c, "E"c}, {"7"c, "F"c}, {"8"c, "G"c}, {"9"c, "H"c}, {"T"c, "I"c}, {"J"c, "J"c}, {"Q"c, "K"c}, {"K"c, "L"c}, {"A"c, "M"c}}
- Private ReadOnly CardValueMappingsForPart2 As New Dictionary(Of Char, Char) From {{"J"c, "A"c}, {"2"c, "B"c}, {"3"c, "C"c}, {"4"c, "D"c}, {"5"c, "E"c}, {"6"c, "F"c}, {"7"c, "G"c}, {"8"c, "H"c}, {"9"c, "I"c}, {"T"c, "J"c}, {"Q"c, "K"c}, {"K"c, "L"c}, {"A"c, "M"c}}
- Sub New(RawData As IEnumerable(Of String), Part As Integer)
- CardOrder = RawData(0)
- Bid = Integer.Parse(RawData(1))
- DetermineCardDeckType(Part)
- End Sub
- Private Sub DetermineCardDeckType(Part As Integer)
- Dim IndexPacks As New List(Of IndexPack)
- CardOrder.ToList.ForEach(Sub(x) If Not IndexPacks.Any(Function(y) y.Character = x) Then IndexPacks.Add(New IndexPack With {.Character = x, .Occurences = GetOccurencesIn(CardOrder, x)}))
- If IndexPacks.Where(Function(x) x.Occurences = 2).Count = 1 Then CardDeckType = CardDeckType.OnePair
- If IndexPacks.Where(Function(x) x.Occurences = 2).Count = 2 Then CardDeckType = CardDeckType.TwoPair
- If IndexPacks.Any(Function(x) x.Occurences = 3) Then CardDeckType = CardDeckType.ThreeOfAKind
- If IndexPacks.Any(Function(x) x.Occurences = 3) AndAlso IndexPacks.Any(Function(x) x.Occurences = 2) Then CardDeckType = CardDeckType.FullHouse
- If IndexPacks.Any(Function(x) x.Occurences = 4) Then CardDeckType = CardDeckType.FourOfAKind
- If IndexPacks.Any(Function(x) x.Occurences = 5) Then CardDeckType = CardDeckType.FiveOfAKind
- If Part = 2 AndAlso CardDeckType <> CardDeckType.FiveOfAKind AndAlso CardOrder.IndexOf("J"c) > -1 Then
- If CardDeckType = CardDeckType.FourOfAKind Then CardDeckType = CardDeckType.FiveOfAKind
- If CardDeckType = CardDeckType.FullHouse Then CardDeckType = CardDeckType.FiveOfAKind
- If CardDeckType = CardDeckType.ThreeOfAKind Then CardDeckType = CardDeckType.FourOfAKind
- If CardDeckType = CardDeckType.TwoPair AndAlso IndexPacks.Single(Function(x) x.Occurences = 1).Character = "J"c Then CardDeckType = CardDeckType.FullHouse
- If CardDeckType = CardDeckType.TwoPair AndAlso IndexPacks.Any(Function(x) x.Occurences = 2 AndAlso x.Character = "J"c) Then CardDeckType = CardDeckType.FourOfAKind
- If CardDeckType = CardDeckType.TwoPair AndAlso IndexPacks.Any(Function(x) x.Occurences = 1 AndAlso x.Character = "J"c) Then CardDeckType = CardDeckType.ThreeOfAKind
- If CardDeckType = CardDeckType.OnePair Then CardDeckType = CardDeckType.ThreeOfAKind
- If CardDeckType = CardDeckType.HighCard Then CardDeckType = CardDeckType.OnePair
- End If
- End Sub
- Private Function GetOccurencesIn(Text As String, Character As Char) As Integer
- Dim Occurences = 0
- For i = 0 To Text.Length - 1
- If Text(i) = Character Then Occurences += 1
- Next
- Return Occurences
- End Function
- Function TranslatedCardOrder(Part As Integer) As String
- Dim CardValueMappingsToUse = If(Part = 1, CardValueMappingsForPart1, CardValueMappingsForPart2)
- Return CardOrder.Select(Function(x) CardValueMappingsToUse.Single(Function(y) y.Key = x).Value).ToArray
- End Function
- Function GetValue() As Integer
- Return Bid * RankInGame
- End Function
- End Class
- Friend Class IndexPack
- Property Character As Char
- Property Occurences As Integer
- End Class
Bald ist meine arbeitsfreie Woche rum, dann hat sich's mit meinen zeitnahen Posts.
Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.
Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.
Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.
Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „VaporiZed“ ()