This commit is contained in:
2022-12-02 07:41:32 +01:00
parent 9fe1abe08c
commit e84c13c689
3 changed files with 140 additions and 0 deletions

47
day02/common/round.go Normal file
View File

@@ -0,0 +1,47 @@
package common
type Move int
const (
Rock Move = iota
Paper
Scissor
)
type Round struct {
Opponent Move
Response Move
}
func (round Round) GetScore() int {
score := 0
switch round {
case Round{Rock, Rock}:
score += 4
case Round{Rock, Paper}:
score += 8
case Round{Rock, Scissor}:
score += 3
case Round{Paper, Rock}:
score += 1
case Round{Paper, Paper}:
score += 5
case Round{Paper, Scissor}:
score += 9
case Round{Scissor, Rock}:
score += 7
case Round{Scissor, Paper}:
score += 2
case Round{Scissor, Scissor}:
score += 6
}
return score
}
func GetTotalScore(rounds []Round) int {
score := 0
for _, round := range rounds {
score += round.GetScore()
}
return score
}

46
day02/ex1/main.go Normal file
View File

@@ -0,0 +1,46 @@
package main
import (
"bufio"
"fmt"
"os"
"aoc2022/day02/common"
)
func Parse(scanner bufio.Scanner) []common.Round {
rounds := []common.Round{}
for scanner.Scan() {
line := scanner.Text()
var opponent common.Move
switch line[0] {
case 'A':
opponent = common.Rock
case 'B':
opponent = common.Paper
case 'C':
opponent = common.Scissor
}
var response common.Move
switch line[2] {
case 'X':
response = common.Rock
case 'Y':
response = common.Paper
case 'Z':
response = common.Scissor
}
rounds = append(rounds, common.Round{Opponent: opponent, Response: response})
}
return rounds
}
func main() {
rounds := Parse(*bufio.NewScanner(os.Stdin))
fmt.Println(common.GetTotalScore(rounds))
}

47
day02/ex2/main.go Normal file
View File

@@ -0,0 +1,47 @@
package main
import (
"bufio"
"fmt"
"os"
"aoc2022/day02/common"
)
func Parse(scanner bufio.Scanner) []common.Round {
rounds := []common.Round{}
for scanner.Scan() {
line := scanner.Text()
var round common.Round
switch line {
case "A X":
round = common.Round{Opponent: common.Rock, Response: common.Scissor}
case "A Y":
round = common.Round{Opponent: common.Rock, Response: common.Rock}
case "A Z":
round = common.Round{Opponent: common.Rock, Response: common.Paper}
case "B X":
round = common.Round{Opponent: common.Paper, Response: common.Rock}
case "B Y":
round = common.Round{Opponent: common.Paper, Response: common.Paper}
case "B Z":
round = common.Round{Opponent: common.Paper, Response: common.Scissor}
case "C X":
round = common.Round{Opponent: common.Scissor, Response: common.Paper}
case "C Y":
round = common.Round{Opponent: common.Scissor, Response: common.Scissor}
case "C Z":
round = common.Round{Opponent: common.Scissor, Response: common.Rock}
}
rounds = append(rounds, round)
}
return rounds
}
func main() {
rounds := Parse(*bufio.NewScanner(os.Stdin))
fmt.Println(common.GetTotalScore(rounds))
}