@@ -15,20 +15,63 @@ type move struct {
15
15
oppo string
16
16
my string
17
17
}
18
+ type move2 struct {
19
+ oppo string
20
+ outcome string
21
+ }
22
+
23
+ func (m move2 ) score () int {
24
+ return m .scoreOutcome () + m .scoreMyMove ()
25
+ }
26
+ func (m move2 ) scoreOutcome () int {
27
+ if m .outcome == "X" {
28
+ return 0
29
+ } else if m .outcome == "Y" {
30
+ return 3
31
+ } else if m .outcome == "Z" {
32
+ return 6
33
+ }
34
+ panic (m .outcome )
35
+ }
36
+ func (m move2 ) scoreMyMove () int {
37
+ var myShape string
38
+ if m .outcome == "Y" {
39
+ myShape = m .oppo
40
+ } else if m .outcome == "X" {
41
+ if m .oppo == "A" {
42
+ myShape = "C"
43
+ } else if m .oppo == "B" {
44
+ myShape = "A"
45
+ } else {
46
+ myShape = "B"
47
+ }
48
+ } else {
49
+ if m .oppo == "A" {
50
+ myShape = "B"
51
+ } else if m .oppo == "B" {
52
+ myShape = "C"
53
+ } else {
54
+ myShape = "A"
55
+ }
56
+ }
57
+
58
+ return scoreMyShape (myShape )
59
+ }
18
60
19
61
func (m move ) score () int {
20
- return m .scoreMove () + m . scoreMyShape ()
62
+ return m .scoreMove () + scoreMyShape (m . my )
21
63
}
22
- func ( m move ) scoreMyShape () int {
23
- if m . my == "X" {
64
+ func scoreMyShape (shape string ) int {
65
+ if shape == "X" || shape == "A " {
24
66
return 1
25
- } else if m . my == "Y" {
67
+ } else if shape == "Y" || shape == "B " {
26
68
return 2
27
- } else if m . my == "Z" {
69
+ } else if shape == "Z" || shape == "C " {
28
70
return 3
29
71
}
30
- panic (m . my )
72
+ panic (shape )
31
73
}
74
+
32
75
func (m move ) scoreMove () int {
33
76
if (m .oppo == "A" && m .my == "X" ) ||
34
77
(m .oppo == "B" && m .my == "Y" ) ||
@@ -43,7 +86,16 @@ func (m move) scoreMove() int {
43
86
}
44
87
}
45
88
46
- func scoreMoves (moves []move ) []int {
89
+ func scoreMoves1 (moves []move ) []int {
90
+ scores := make ([]int , len (moves ))
91
+
92
+ for i , m := range moves {
93
+ scores [i ] = m .score ()
94
+ }
95
+
96
+ return scores
97
+ }
98
+ func scoreMoves2 (moves []move2 ) []int {
47
99
scores := make ([]int , len (moves ))
48
100
49
101
for i , m := range moves {
@@ -53,7 +105,7 @@ func scoreMoves(moves []move) []int {
53
105
return scores
54
106
}
55
107
56
- func parseInput (input string ) []move {
108
+ func parseInput1 (input string ) []move {
57
109
lines := strings .Split (input , "\n " )
58
110
var moves []move
59
111
@@ -69,15 +121,41 @@ func parseInput(input string) []move {
69
121
70
122
return moves
71
123
}
124
+ func parseInput2 (input string ) []move2 {
125
+ lines := strings .Split (input , "\n " )
126
+ var moves []move2
127
+
128
+ for _ , line := range lines {
129
+ if len (line ) < 3 {
130
+ break
131
+ }
132
+
133
+ oppoMove := string (line [0 ])
134
+ outcome := string (line [2 ])
135
+ moves = append (moves , move2 {oppoMove , outcome })
136
+ }
137
+
138
+ return moves
139
+ }
72
140
73
141
func partOne (input string ) int {
74
- moves := parseInput (input )
75
- scores := scoreMoves (moves )
142
+ moves := parseInput1 (input )
143
+ scores := scoreMoves1 (moves )
144
+ // fmt.Printf("%v\n", scores)
145
+ sum := util .SumIntSlice (scores )
146
+ return sum
147
+ }
148
+ func partTwo (input string ) int {
149
+ moves := parseInput2 (input )
150
+ scores := scoreMoves2 (moves )
76
151
// fmt.Printf("%v\n", scores)
77
152
sum := util .SumIntSlice (scores )
78
153
return sum
79
154
}
80
155
func main () {
81
- score := partOne (input2 )
82
- fmt .Printf ("%d\n " , score )
156
+ score1 := partOne (input2 )
157
+ fmt .Printf ("%d\n " , score1 )
158
+
159
+ score2 := partTwo (input2 )
160
+ fmt .Printf ("%d\n " , score2 )
83
161
}
0 commit comments