re-organise repo
This commit is contained in:
89
2022/go/day02/day02.go
Normal file
89
2022/go/day02/day02.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package day02
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Part1(input string) int {
|
||||
var sum int = 0
|
||||
|
||||
values := map[string]int{
|
||||
"Rock": 1,
|
||||
"Paper": 2,
|
||||
"Scissors": 3,
|
||||
}
|
||||
|
||||
translateMap := map[string]string{
|
||||
"X": "Rock",
|
||||
"Y": "Paper",
|
||||
"Z": "Scissors",
|
||||
"A": "Rock",
|
||||
"B": "Paper",
|
||||
"C": "Scissors",
|
||||
}
|
||||
|
||||
winOn := map[string]string{
|
||||
"Rock": "Scissors",
|
||||
"Paper": "Rock",
|
||||
"Scissors": "Paper",
|
||||
}
|
||||
|
||||
lines := strings.Split(input, "\n")
|
||||
for _, line := range lines {
|
||||
objects := strings.Split(line, " ")
|
||||
opponent := translateMap[objects[0]]
|
||||
player := translateMap[objects[1]]
|
||||
sum = sum + values[player]
|
||||
if player == opponent {
|
||||
sum = sum + 3
|
||||
} else if winOn[player] == opponent {
|
||||
sum = sum + 6
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
func Part2(input string) int {
|
||||
var sum int = 0
|
||||
|
||||
values := map[string]int{
|
||||
"Rock": 1,
|
||||
"Paper": 2,
|
||||
"Scissors": 3,
|
||||
}
|
||||
|
||||
translateMap := map[string]string{
|
||||
"X": "Lose",
|
||||
"Y": "Draw",
|
||||
"Z": "Win",
|
||||
"A": "Rock",
|
||||
"B": "Paper",
|
||||
"C": "Scissors",
|
||||
}
|
||||
|
||||
winOn := map[string]string{
|
||||
"Rock": "Paper",
|
||||
"Paper": "Scissors",
|
||||
"Scissors": "Rock",
|
||||
}
|
||||
|
||||
loseOn := map[string]string{
|
||||
"Rock": "Scissors",
|
||||
"Paper": "Rock",
|
||||
"Scissors": "Paper",
|
||||
}
|
||||
|
||||
lines := strings.Split(input, "\n")
|
||||
for _, line := range lines {
|
||||
objects := strings.Split(line, " ")
|
||||
opponent := translateMap[objects[0]]
|
||||
result := translateMap[objects[1]]
|
||||
if result == "Draw" {
|
||||
sum = sum + 3 + values[opponent]
|
||||
} else if result == "Win" {
|
||||
sum = sum + 6 + values[winOn[opponent]]
|
||||
} else {
|
||||
sum = sum + values[loseOn[opponent]]
|
||||
}
|
||||
}
|
||||
return sum}
|
||||
Reference in New Issue
Block a user