-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday02.go
More file actions
143 lines (131 loc) · 3.24 KB
/
day02.go
File metadata and controls
143 lines (131 loc) · 3.24 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"AdventOfCode-go/advent2022/utils"
"fmt"
)
func getScoreUsingStrategy(elfPlay byte, myPlay byte) int {
// A for Rock
// B for Paper
// C for Scissors
// My play: Rock (score 1) Paper (score 2) Scissors (score 3)
// myPlay: X = need to lose
// myPlay: Y = need to draw
// myPlay: Z = need to win
// Score for the round: 0 if lost; 3 is draw; 6 if won
// Scissors beats paper
// Paper beats rock
// Rock beats Scissors
switch elfPlay {
case 'A':
switch myPlay {
case 'X':
// I need to lose so play scissors so 3 (scissors) + 0 (lose)
return 3 + 0
case 'Y':
// I need to draw so play rock so 1 (rock) + 3 (draw)
return 1 + 3
case 'Z':
// I need to win so play paper so 2 (paper) + 6 (win)
return 2 + 6
}
case 'B':
switch myPlay {
case 'X':
// I need to lose so play rock so 1 (rock) + 0 (lose)
return 1 + 0
case 'Y':
// I need to draw so play paper so 2 (paper) + 3 (draw)
return 2 + 3
case 'Z':
// I need to win so play scissors so 3 (scissors) + 6 (win)
return 3 + 6
}
case 'C':
switch myPlay {
case 'X':
// I need to lose so paper so 2 (paper) + 0 (lose)
return 2 + 0
case 'Y':
// I need to draw so scissors so 3 (scissors) + 3 (draw)
return 3 + 3
case 'Z':
// I need to win so rock so 1 (rock) + 6 (win)
return 1 + 6
}
}
return 0
}
func getGameScore(elfPlay byte, myPlay byte) int {
// A for Rock
// B for Paper
// C for Scissors
// My play: X for Rock (score 1)
// My play: Y for Paper (score 2)
// My play: Z for Scissors (score 3)
// Score for the round: 0 if lost; 3 is draw; 6 if won
// Scissors beats paper
// Paper beats rock
// Rock beats Scissors
switch elfPlay {
case 'A':
switch myPlay {
case 'X':
// It's a draw, and I played rock so 1 (rock) + 3 (draw)
return 1 + 3
case 'Y':
// Paper wins and I played it so 2 (paper) + 6 (win)
return 2 + 6
case 'Z':
// Scissors loses and I played it so 3 (scissors) + 0 (loss)
return 3 + 0
}
case 'B':
switch myPlay {
case 'X':
// Rock loses and I played it so 1 (rock) + 0 (loss)
return 1 + 0
case 'Y':
// Paper draws and I played it so 2 (paper) + 3 (draw)
return 2 + 3
case 'Z':
// Scissors wins and I played it so 3 (scissors) + 6 (win)
return 3 + 6
}
case 'C':
switch myPlay {
case 'X':
// Rock beats scissors and I played it so 1 (rock) + 6 (win)
return 1 + 6
case 'Y':
// Paper loses and I played it so 2 (paper) + 0 (loss)
return 2 + 0
case 'Z':
// Scissors draws and I played it so 3 (scissors) + 3 (draw)
return 3 + 3
}
}
return 0
}
func elfRockPaperScissors(filename string, part byte, debug bool) int {
puzzleInput, _ := utils.ReadFile(filename)
var elfPlay, myPlay byte
var currentScore int
for _, j := range puzzleInput {
fmt.Sscanf(j, "%c %c\n", &elfPlay, &myPlay)
if part == 'a' {
currentScore += getGameScore(elfPlay, myPlay)
} else {
currentScore += getScoreUsingStrategy(elfPlay, myPlay)
}
}
return currentScore
}
// Main routine
func main() {
filenamePtr, execPart, debug := utils.CatchUserInput()
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
} else {
fmt.Printf("Result is: %d\n", elfRockPaperScissors(filenamePtr, execPart, debug))
}
}