-
Notifications
You must be signed in to change notification settings - Fork 0
/
2b.go
86 lines (74 loc) · 1.23 KB
/
2b.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
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
outcome string
}
func (m Match) score() int {
// A Rock, B Paper, C Scissor
// X loose Y draw Z win
symbolToIndex := map[string]int{"A": 0, "B": 1, "C": 2, "X": 0, "Y": 1, "Z": 2}
i1 := symbolToIndex[m.opponent]
score := 0
i2 := -1
// who won
// loose
if m.outcome == "X" {
switch i1 {
case 0: i2 = 2
case 1: i2 = 0
case 2: i2 = 1
}
score += 0
}
if m.outcome == "Y" {
i2 = i1
score += 3
}
if m.outcome == "Z" {
switch i1 {
case 0: i2 = 1
case 1: i2 = 2
case 2: i2 = 0
}
score += 6
}
// point for which shape
score += i2 + 1
return score
}
func main() {
dat, err := os.ReadFile("2.txt")
check(err)
data := string(dat)
// data = `A Y
// B X
// C Z`
rows := strings.Split(data, "\n")
result := mapp(rows, func(v string) Match {
vals := strings.Split(v, " ")
return Match{opponent: vals[0], outcome: vals[1]}
})
total := 0
for _,match := range result {
total += match.score()
}
fmt.Println(total)
}