-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.go
68 lines (57 loc) · 1.08 KB
/
2.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
package main
import (
"fmt"
"os"
"strings"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func mapp[T any,K any](input []T, f func (val T) K) []K {
var res []K
for _, val := range input {
v := f(val)
res = append(res, v)
}
return res
}
type Match struct {
opponent string
you string
}
func (m Match) score() int {
// A Rock, B Paper, C Scissor
// X Y Z
symbolToIndex := map[string]int{"A": 0, "B": 1, "C": 2, "X": 0, "Y": 1, "Z": 2}
i1 := symbolToIndex[m.opponent]
i2 := symbolToIndex[m.you]
score := 0
// who won
if i1 == i2 {
score += 3
} else if (i1 == 0 && i2 == 2) || (i1 == 1 && i2 == 0) || (i1 == 2 && i2 == 1) {
score += 0
} else {
score += 6
}
// point for which shape
score += i2 + 1
return score
}
func main() {
dat, err := os.ReadFile("2.txt")
check(err)
data := string(dat)
rows := strings.Split(data, "\n")
result := mapp(rows, func(v string) Match {
vals := strings.Split(v, " ")
return Match{opponent: vals[0], you: vals[1]}
})
total := 0
for _,match := range result {
total += match.score()
}
fmt.Println(total)
}