File tree Expand file tree Collapse file tree 5 files changed +148
-3
lines changed
day07/src/main/kotlin/de/havox_design/aoc2023/day07 Expand file tree Collapse file tree 5 files changed +148
-3
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ plugins {
44
55// project meta data
66group ' de.havox_design.aoc2023'
7- version ' 0.6.1 '
7+ version ' 0.6.2 '
88
99// Switch to gradle "all" distribution.
1010wrapper {
Original file line number Diff line number Diff line change 1+ package de.havox_design.aoc2023.day07
2+
3+ enum class Card (val symbol : Char , private val order : Int ) {
4+ TWO (' 2' , 2 ),
5+ THREE (' 3' , 3 ),
6+ FOUR (' 4' , 4 ),
7+ FIVE (' 5' , 5 ),
8+ SIX (' 6' , 6 ),
9+ SEVEN (' 7' , 7 ),
10+ EIGHT (' 8' , 8 ),
11+ NINE (' 9' , 9 ),
12+ TEN (' T' , 10 ),
13+ JOKER (' J' , 11 ),
14+ QUEEN (' Q' , 12 ),
15+ KING (' K' , 13 ),
16+ ACE (' A' , 14 );
17+
18+ companion object {
19+ fun from (c : Char ): Card {
20+ if (entries.map { card -> card.symbol }.contains(c)) {
21+ return entries.first { card -> card.symbol == c }
22+ }
23+
24+ throw IllegalArgumentException (" No knows card symbol '${c} '" )
25+ }
26+
27+ fun compare (a : Card , b : Card ): Int =
28+ a.order.compareTo(b.order)
29+ }
30+ }
Original file line number Diff line number Diff line change 11package de.havox_design.aoc2023.day07
22
33class Day07 (private var filename : String ) {
4- fun solvePart1 (): Long =
5- 6440L
4+ private val ELEMENT_DELIMITER = " "
5+
6+ fun solvePart1 (): Long {
7+ val data = mapInput(getResourceAsText(filename))
8+ val sortedData = data.sortedBy { game -> game.first }.toList()
9+ var winnings = 0L
10+
11+ for (index in sortedData.indices) {
12+ winnings + = (index + 1 ) * sortedData[index].second
13+ }
14+
15+ return winnings
16+ }
617
718 fun solvePart2 (): Long =
819 0L
920
21+ private fun mapInput (input : List <String >):List <Pair <Hand ,Int >> =
22+ input
23+ .map { row ->
24+ val elements = row.split(ELEMENT_DELIMITER )
25+ val hand = Hand .from(elements[0 ])
26+ val bit = elements[1 ].toInt()
27+
28+ return @map Pair (hand, bit)
29+ }
30+
1031 private fun getResourceAsText (path : String ): List <String > =
1132 this .javaClass.classLoader.getResourceAsStream(path)!! .bufferedReader().readLines()
1233}
Original file line number Diff line number Diff line change 1+ package de.havox_design.aoc2023.day07
2+
3+ enum class GameType (private var order : Int ) {
4+ FIVE_OF_A_KIND (6 ),
5+ FOUR_OF_A_KIND (5 ),
6+ FULL_HOUSE (4 ),
7+ THREE_OF_A_KIND (3 ),
8+ TWO_PAIRS (2 ),
9+ ONE_PAIR (1 ),
10+ HIGH_CARD (0 );
11+
12+ companion object {
13+ fun from (hand : Hand ): GameType {
14+ val cardMap = getCardMap(hand)
15+
16+ if (cardMap.size == 1 ) {
17+ return FIVE_OF_A_KIND
18+ }
19+ if (cardMap.size == 2 ) {
20+ if (cardMap.values.max() == 4 ) {
21+ return FOUR_OF_A_KIND
22+ }
23+ return FULL_HOUSE
24+ }
25+ if (cardMap.size == 3 ) {
26+ if (cardMap.values.max() == 3 ) {
27+ return THREE_OF_A_KIND
28+ }
29+ return TWO_PAIRS
30+ }
31+ if (cardMap.values.max() == 2 ) {
32+ return ONE_PAIR
33+ }
34+ return HIGH_CARD
35+ }
36+
37+ fun compare (a : GameType , b : GameType ): Int =
38+ a.order.compareTo(b.order)
39+
40+ private fun getCardMap (hand : Hand ): Map <Card , Int > {
41+ val result = HashMap <Card , Int >()
42+
43+ for (c in hand.cards) {
44+ if (result.containsKey(c)) {
45+ result[c] = result[c]!! + 1
46+ }
47+ else {
48+ result[c] = 1
49+ }
50+ }
51+
52+ return result
53+ }
54+ }
55+ }
Original file line number Diff line number Diff line change 1+ package de.havox_design.aoc2023.day07
2+
3+ class Hand (val cards : List <Card >): Comparable<Hand> {
4+ override fun compareTo (other : Hand ): Int =
5+ compare(this , other)
6+
7+ companion object {
8+ fun from (input : String ): Hand {
9+ val hand = ArrayList <Card >()
10+
11+ for (c in input.toCharArray()) {
12+ hand.add(Card .from(c))
13+ }
14+
15+ return Hand (hand)
16+ }
17+
18+ fun compare (a : Hand , b : Hand ): Int {
19+ if (a == b) {
20+ return 0
21+ }
22+
23+ val aGameType = GameType .from(a)
24+ val bGameType = GameType .from(b)
25+
26+ if (aGameType == bGameType) {
27+ for (index in a.cards.indices) {
28+ if (a.cards[index] == b.cards[index]) {
29+ continue
30+ }
31+
32+ return Card .compare(a.cards[index], b.cards[index])
33+ }
34+ }
35+
36+ return GameType .compare(aGameType, bGameType)
37+ }
38+ }
39+ }
You can’t perform that action at this time.
0 commit comments