-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday02_roshambo.go
157 lines (126 loc) · 3.06 KB
/
day02_roshambo.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
// have to install ioutil
// go get io/ioutil
import (
"fmt"
"io/ioutil"
"log"
"strings"
)
type rhoShamBoGame struct {
opponent string
me string
}
func readRoShamBoFilePart1(filePath string) []rhoShamBoGame {
body, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatalf("unable to read file: %v", err)
}
allGames := make([]rhoShamBoGame, 0)
gamesString := strings.Split(strings.Trim(string(body), "\n"), "\n")
for _, gameString := range gamesString {
gameDetails := strings.Fields(gameString)
var opponent string
switch gameDetails[0] {
case "A":
opponent = "rock"
case "B":
opponent = "paper"
case "C":
opponent = "scissors"
}
var me string
switch gameDetails[1] {
case "X":
me = "rock"
case "Y":
me = "paper"
case "Z":
me = "scissors"
}
allGames = append(allGames, rhoShamBoGame{opponent: opponent, me: me})
}
return allGames
}
func readRoShamBoFilePart2(filePath string) []rhoShamBoGame {
body, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatalf("unable to read file: %v", err)
}
allGames := make([]rhoShamBoGame, 0)
var winMap = make(map[string]string)
winMap["paper"] = "rock"
winMap["rock"] = "scissors"
winMap["scissors"] = "paper"
loseMap := make(map[string]string)
loseMap["rock"] = "paper"
loseMap["scissors"] = "rock"
loseMap["paper"] = "scissors"
gamesString := strings.Split(strings.Trim(string(body), "\n"), "\n")
for _, gameString := range gamesString {
gameDetails := strings.Fields(gameString)
var opponent string
switch gameDetails[0] {
case "A":
opponent = "rock"
case "B":
opponent = "paper"
case "C":
opponent = "scissors"
}
var me string
switch gameDetails[1] {
case "X":
me = winMap[opponent]
case "Y":
me = opponent
case "Z":
me = loseMap[opponent]
}
allGames = append(allGames, rhoShamBoGame{opponent: opponent, me: me})
}
return allGames
}
func scoreRoShamBoGames(games []rhoShamBoGame) int {
var totalScore int
for _, game := range games {
// score shape i selected
switch game.me {
case "rock":
totalScore += 1
case "paper":
totalScore += 2
case "scissors":
totalScore += 3
}
// score outcome
if game.opponent == game.me {
totalScore += 3
continue
}
if ((game.me == "paper") && (game.opponent == "rock")) || ((game.me == "scissors") && (game.opponent == "paper")) || ((game.me == "rock") && (game.opponent == "scissors")) {
totalScore += 6
}
}
return totalScore
}
func day02() {
// Part 1
games := readRoShamBoFilePart1("2022/data/day02_sample.txt")
result := scoreRoShamBoGames(games)
if result != 15 {
panic("Part 1 example is failing")
}
games = readRoShamBoFilePart1("2022/data/day02_input.txt")
result = scoreRoShamBoGames(games)
fmt.Println("Part 1:", result)
// Part 2
games = readRoShamBoFilePart2("2022/data/day02_sample.txt")
result = scoreRoShamBoGames(games)
if result != 12 {
panic("Part 2 example is failing")
}
games = readRoShamBoFilePart2("2022/data/day02_input.txt")
result = scoreRoShamBoGames(games)
fmt.Println("Part 2:", result)
}