-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
179 lines (152 loc) · 3.69 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
_ "embed"
"flag"
"fmt"
"log"
"strings"
"github.com/davidn5013/aoc/cast"
"github.com/davidn5013/aoc/util"
)
//go:embed input.txt
var input string
func init() {
// do this in init (not main) so test file has same input
input = strings.TrimRight(input, "\n")
if len(input) == 0 {
panic("empty input.txt file")
}
}
func main() {
var part int
flag.IntVar(&part, "part", 1, "part 1 or 2")
flag.Parse()
fmt.Println("Running part", part)
if part == 1 {
ans := part1(input)
util.CopyToClipboard(fmt.Sprintf("%v", ans))
fmt.Println("Output:", ans)
} else {
ans := part2(input)
util.CopyToClipboard(fmt.Sprintf("%v", ans))
fmt.Println("Output:", ans)
}
}
// card number A, K, Q, J, T, 9, 8, 7, 6, 5, 4, 3, or 2
// 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
func part1(input string) int {
cards := parseInput(input)
for _, card := range cards {
fmt.Println(card.card, len(cardcnt(card.card, 0)), cardtype(card.card))
}
c1 := cards[0].card
c2 := cards[1].card
fmt.Println(c1, c2, firstHandBig(c1, c2))
return 0
}
func cardtype(card string) string {
switch {
case len(cardcnt(card, 5)) >= 1:
return "Five of a kind"
case len(cardcnt(card, 4)) >= 1:
return "Four of a kind"
case len(cardcnt(card, 3)) >= 1 && len(cardcnt(card, 2)) >= 1:
return "Full house"
case len(cardcnt(card, 3)) >= 1:
return "Three of a kind"
case twopar(card):
return "Two par"
case len(cardcnt(card, 2)) >= 1:
return "Par"
}
return "High card"
}
func cardval(card string) int {
cardval := map[string]int{
"A": 14,
"K": 13,
"Q": 12,
"J": 11,
"T": 10,
"9": 9,
"8": 8,
"7": 8,
"6": 6,
"5": 5,
"4": 4,
"3": 3,
"2": 2,
}
return cardval[string(card)]
}
func firstHandBig(card1, card2 string) bool {
if len(card1) != len(card2) {
log.Fatal("ERR different size of cards ", len(card1), len(card2))
}
fmt.Println(card1, card2)
for i, v := range card1 {
c1 := string(v)
c2 := string(card2[i])
v1 := cardval(c1)
v2 := cardval(c2)
if v1 == v2 {
continue
}
if v1 > v2 {
fmt.Println(c1, c2, v1, v2)
return true
} else {
break
}
}
return false
}
func twopar(card string) bool {
m := cardcnt(card, 2)
ans := make(map[string]int)
for i, v := range m {
if v == 2 {
ans[i] = v
}
}
return len(ans) == 2
}
func cardcnt(card string, filter int) (ans map[string]int) {
t := make(map[string]int, len(card))
for i := 0; i < len(card); i++ {
t[string(card[i])]++
}
ans = make(map[string]int, len(t))
for i, v := range t {
if v == filter || filter == 0 {
ans[string(i)] = v
}
}
return ans
}
func part2(input string) int {
return 0
}
type Cards struct {
sortvalue int
card string
rankvalue int
}
func parseInput(input string) (ans []Cards) {
for _, line := range strings.Split(input, "\n") {
parts := strings.Fields(line)
ans = append(ans, Cards{
card: parts[0],
rankvalue: cast.ToInt(parts[1]),
})
}
return ans
}