-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.go
94 lines (80 loc) · 1.52 KB
/
02.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
package main
import (
"fmt"
"strings"
)
func AOC202202Win(opponent, self string) int {
var points = map[string]map[string]int{
"A": {
"X": 3,
"Y": 6,
"Z": 0,
},
"B": {
"X": 0,
"Y": 3,
"Z": 6,
},
"C": {
"X": 6,
"Y": 0,
"Z": 3,
},
}
return points[opponent][self]
}
func AOC202202Points(self string) int {
var points = map[string]int{
"X": 1,
"Y": 2,
"Z": 3,
}
return points[self]
}
func AOC202202Round(opponent, self string) (int, error) {
return AOC202202Win(opponent, self) + AOC202202Points(self), nil
}
func AOC2022021(input string) (string, error) {
sum := 0
for _, line := range strings.Split(input, "\n") {
parts := strings.Split(line, " ")
points, err := AOC202202Round(parts[0], parts[1])
if err != nil {
return "", fmt.Errorf("Line: %s: %v", line, err)
}
sum += points
}
return fmt.Sprintf("%d", sum), nil
}
func AOC202202Lookup(opponent, target string) string {
var lookup = map[string]map[string]string{
"A": {
"X": "Z",
"Y": "X",
"Z": "Y",
},
"B": {
"X": "X",
"Y": "Y",
"Z": "Z",
},
"C": {
"X": "Y",
"Y": "Z",
"Z": "X",
},
}
return lookup[opponent][target]
}
func AOC2022022(input string) (string, error) {
sum := 0
for _, line := range strings.Split(input, "\n") {
parts := strings.Split(line, " ")
points, err := AOC202202Round(parts[0], AOC202202Lookup(parts[0], parts[1]))
if err != nil {
return "", fmt.Errorf("Line: %s: %v", line, err)
}
sum += points
}
return fmt.Sprintf("%d", sum), nil
}