AOC2022/day2/day2.go
2022-12-03 03:40:46 +01:00

61 lines
1.1 KiB
Go

package main
import (
"AOC2022/helper"
"fmt"
"os"
)
func main() {
args := os.Args[1:]
rounds := helper.ReadTextFile(args[0])
sum := 0
for _, round := range rounds {
sum += roundResult(int(round[0])-64, int(round[2])-87)
}
fmt.Println(sum)
sum = 0
for _, round := range rounds {
opponent := int(round[0]) - 64
neededResult := int(round[2]) - 87
move := generateCorrectMove(opponent, neededResult)
sum += roundResult(opponent, move)
}
fmt.Println(sum)
}
func roundResult(opponent, player int) int {
weakness := getWeakness(opponent)
if player == weakness {
return player + 6
}
if opponent == player {
return player + 3
}
return player + 0
}
func getWeakness(input int) int {
tmpWeakness := (input + 1) % 4
if tmpWeakness == 0 {
return 1
}
return tmpWeakness
}
func generateCorrectMove(opponent, neededResult int) int {
weakness := getWeakness(opponent)
if neededResult == 3 {
return weakness
}
if neededResult == 1 {
possibleMove := []int{1, 2, 3}
for _, x := range possibleMove {
if x != weakness && x != opponent {
return x
}
}
}
return opponent
}