aoc22/go/2/main.go

87 lines
1.5 KiB
Go

package main
import (
"bufio"
"os"
"path/filepath"
"strings"
)
var (
f = "../../2.input.txt"
RPS = map[string] int {
"X":1,
"Y":2,
"Z":3,
"A":1,
"B":2,
"C":3,
}
outcome = map[string] int {
"A X":3,
"A Y":6,
"A Z":0,
"B X":0,
"B Y":3,
"B Z":6,
"C X":6,
"C Y":0,
"C Z":3,
}
// A:Rock B:Paper C:Scissor
// X:lose Y:draw Z:win
rRPS = map[string] string {
"A X":"C",
"A Y":"A",
"A Z":"B",
"B X":"A",
"B Y":"B",
"B Z":"C",
"C X":"B",
"C Y":"C",
"C Z":"A",
}
rOutcome = map[string] int {
"X":0,
"Y":3,
"Z":6,
}
)
func must(e error) {
if e != nil {
panic(e)
}
}
func Score(line string) int {
sp := strings.Split(line, " ")
return RPS[sp[1]] + outcome[line]
}
func RealScore(line string) int {
sp := strings.Split(line, " ")
throw := rRPS[line]
return RPS[throw] + rOutcome[sp[1]]
}
func main() {
fp, err:= filepath.Abs(f)
must(err)
rf, err := os.Open(fp)
must(err)
s := bufio.NewScanner(rf)
s.Split(bufio.ScanLines)
totScore := 0
totRealScore := 0
for s.Scan() {
l := s.Text()
totScore = Score(l) + totScore
totRealScore = RealScore(l) + totRealScore
}
println("part 1 score:", totScore)
println("part 2 score:", totRealScore)
}