Skip to content

Commit 0e52016

Browse files
committed
2b
1 parent 6972e6e commit 0e52016

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

2/main.go

+36-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
func main() {
10-
// read file and load caloriesPerElf
10+
// read file and load games
1111
dat, err := os.ReadFile("input.txt")
1212
if err != nil {
1313
panic(err)
@@ -24,28 +24,52 @@ func main() {
2424
fmt.Printf("total score is %d", totalScore)
2525
}
2626

27-
var scores map[string]int = map[string]int{
28-
"X": 1,
29-
"Y": 2,
30-
"Z": 3,
27+
func getWinningMove(opp string) int {
28+
if opp == "A" {
29+
return 2
30+
}
31+
if opp == "B" {
32+
return 3
33+
}
34+
return 1
35+
}
36+
37+
func getLosingMove(opp string) int {
38+
if opp == "A" {
39+
return 3
40+
}
41+
if opp == "B" {
42+
return 1
43+
}
44+
return 2
45+
}
46+
47+
func getDrawMove(opp string) int {
48+
if opp == "A" {
49+
return 1
50+
}
51+
if opp == "B" {
52+
return 2
53+
}
54+
return 3
3155
}
3256

3357
func calculateGame(g []string) int {
3458
// A B C
3559
opp := g[0]
36-
// X Y Z
37-
me := g[1]
60+
// X - Lose; Y - Draw; Z - Win
61+
outcome := g[1]
3862

3963
// Win condition
40-
if opp == "A" && me == "Y" || opp == "B" && me == "Z" || opp == "C" && me == "X" {
41-
return 6 + scores[me]
64+
if outcome == "Z" {
65+
return 6 + getWinningMove(opp)
4266
}
4367

4468
// Loss condition
45-
if opp == "A" && me == "Z" || opp == "B" && me == "X" || opp == "C" && me == "Y" {
46-
return scores[me]
69+
if outcome == "X" {
70+
return getLosingMove(opp)
4771
}
4872

4973
// Draw condition
50-
return 3 + scores[me]
74+
return 3 + getDrawMove(opp)
5175
}

0 commit comments

Comments
 (0)