Skip to content

Instantly share code, notes, and snippets.

@ArturSkowronski
Created December 2, 2022 06:57
Show Gist options
  • Save ArturSkowronski/0d6838efbfcba0d056a058a5a4c5c323 to your computer and use it in GitHub Desktop.
Save ArturSkowronski/0d6838efbfcba0d056a058a5a4c5c323 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"os"
)
func algorithm1(lines []string) int {
sum := 0
winPoints := 6
drawPoints := 3
losePoints := 0
rockPoints := 1
paperPoints := 2
scissorPoints := 3
for _, line := range lines {
switch line {
case "A X":
sum += rockPoints + drawPoints
case "A Y":
sum += paperPoints + drawPoints
case "A Z":
sum += scissorPoints + losePoints
case "B X":
sum += rockPoints + losePoints
case "B Y":
sum += paperPoints + drawPoints
case "B Z":
sum += scissorPoints + winPoints
case "C X":
sum += rockPoints + winPoints
case "C Y":
sum += paperPoints + losePoints
case "C Z":
sum += scissorPoints + drawPoints
}
}
return sum
}
func algorithm2(lines []string) int {
sum := 0
winPoints := 6
drawPoints := 3
losePoints := 0
rockPoints := 1
paperPoints := 2
scissorPoints := 3
for _, line := range lines {
switch line {
case "A X":
sum += scissorPoints + losePoints
case "A Y":
sum += rockPoints + drawPoints
case "A Z":
sum += paperPoints + winPoints
case "B X":
sum += rockPoints + losePoints
case "B Y":
sum += paperPoints + drawPoints
case "B Z":
sum += scissorPoints + winPoints
case "C X":
sum += paperPoints + losePoints
case "C Y":
sum += scissorPoints + drawPoints
case "C Z":
sum += rockPoints + winPoints
}
}
return sum
}
func main() {
lines := []string{}
readFile, err := os.Open("day2.txt")
if err != nil {
fmt.Println(err)
}
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
lines = append(lines, fileScanner.Text())
}
println(len(lines))
fmt.Println(fmt.Sprintf("Solution 1: %d", algorithm1(lines)))
fmt.Println(fmt.Sprintf("Solution 2: %d", algorithm2(lines)))
readFile.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment