File tree Expand file tree Collapse file tree 4 files changed +19
-19
lines changed
Expand file tree Collapse file tree 4 files changed +19
-19
lines changed Original file line number Diff line number Diff line change @@ -38,23 +38,23 @@ func main() {
3838func parseInput (input string ) {
3939 lines := strings .Split (strings .TrimSpace (input ), "\n " )
4040 hands = make ([]Hand , len (lines ))
41-
41+
4242 for i , line := range lines {
4343 spaceIdx := strings .Index (line , " " )
44-
44+
4545 // Parse hand
4646 var h Hand
4747 for j := 0 ; j < 5 ; j ++ {
4848 h .cards [j ] = cardValue (line [j ])
4949 }
50-
50+
5151 // Parse bid
5252 bid := 0
5353 for j := spaceIdx + 1 ; j < len (line ); j ++ {
5454 bid = bid * 10 + int (line [j ]- '0' )
5555 }
5656 h .bid = bid
57-
57+
5858 hands [i ] = h
5959 }
6060}
@@ -104,30 +104,30 @@ func handStrength(counts [5]int) int {
104104func getHandStrength (h Hand , jokerRule bool ) int {
105105 counts := [5 ]int {}
106106 cardCounts := make (map [int ]int )
107-
107+
108108 for _ , card := range h .cards {
109109 cardCounts [card ]++
110110 }
111-
111+
112112 // Handle jokers (value 11 in part 1, converted to 1 in part 2)
113113 jokers := 0
114114 if jokerRule {
115115 jokers = cardCounts [1 ]
116116 delete (cardCounts , 1 )
117117 }
118-
118+
119119 // Fill counts array
120120 i := 0
121121 for _ , count := range cardCounts {
122122 counts [i ] = count
123123 i ++
124124 }
125-
125+
126126 // Add jokers to the highest count
127127 if jokers > 0 {
128128 slices .Sort (counts [:])
129129 counts [4 ] += jokers
130130 }
131-
131+
132132 return handStrength (counts )
133133}
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import "testing"
55func BenchmarkPartOne (b * testing.B ) {
66 parseInput (input )
77 b .ResetTimer ()
8-
8+
99 for n := 0 ; n < b .N ; n ++ {
1010 doPartOne ()
1111 }
@@ -14,7 +14,7 @@ func BenchmarkPartOne(b *testing.B) {
1414func BenchmarkPartTwo (b * testing.B ) {
1515 parseInput (input )
1616 b .ResetTimer ()
17-
17+
1818 for n := 0 ; n < b .N ; n ++ {
1919 doPartTwo ()
2020 }
Original file line number Diff line number Diff line change @@ -6,16 +6,16 @@ func doPartOne() int {
66 // Create a copy for sorting
77 sortedHands := make ([]Hand , len (hands ))
88 copy (sortedHands , hands )
9-
9+
1010 slices .SortFunc (sortedHands , func (a , b Hand ) int {
1111 // Compare hand strengths
1212 sa := getHandStrength (a , false )
1313 sb := getHandStrength (b , false )
14-
14+
1515 if sa != sb {
1616 return sa - sb
1717 }
18-
18+
1919 // Same strength, compare cards in order
2020 for i := 0 ; i < 5 ; i ++ {
2121 if a .cards [i ] != b .cards [i ] {
@@ -24,7 +24,7 @@ func doPartOne() int {
2424 }
2525 return 0
2626 })
27-
27+
2828 result := 0
2929 for i , h := range sortedHands {
3030 result += (i + 1 ) * h .bid
Original file line number Diff line number Diff line change @@ -18,16 +18,16 @@ func doPartTwo() int {
1818 }
1919 sortedHands [i ] = newHand
2020 }
21-
21+
2222 slices .SortFunc (sortedHands , func (a , b Hand ) int {
2323 // Compare hand strengths with joker rule
2424 sa := getHandStrength (a , true )
2525 sb := getHandStrength (b , true )
26-
26+
2727 if sa != sb {
2828 return sa - sb
2929 }
30-
30+
3131 // Same strength, compare cards in order
3232 for i := 0 ; i < 5 ; i ++ {
3333 if a .cards [i ] != b .cards [i ] {
@@ -36,7 +36,7 @@ func doPartTwo() int {
3636 }
3737 return 0
3838 })
39-
39+
4040 result := 0
4141 for i , h := range sortedHands {
4242 result += (i + 1 ) * h .bid
You can’t perform that action at this time.
0 commit comments