123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package main
- import (
- "fmt"
- "io/ioutil"
- "log"
- "os"
- "strconv"
- "strings"
- )
- func readInput(file string) (int, int) {
- content, err := ioutil.ReadFile(file)
- if err != nil {
- log.Fatal(err)
- }
- lines := strings.Split(string(content), "\n")
- if len(lines) != 2 {
- log.Fatal("expected 2 lines")
- }
- partsFirstLine := strings.Split(lines[0], ": ")
- if len(partsFirstLine) != 2 {
- log.Fatal("expected 2 parts")
- }
- player1, err := strconv.Atoi(partsFirstLine[1])
- if err != nil {
- log.Fatal(err)
- }
- partsSecondLine := strings.Split(lines[1], ": ")
- if len(partsSecondLine) != 2 {
- log.Fatal("expected 2 parts")
- }
- player2, err := strconv.Atoi(partsSecondLine[1])
- if err != nil {
- log.Fatal(err)
- }
- return player1, player2
- }
- var dice int
- func roll() int {
- dice++
- return dice
- }
- func part1(player1, player2 int) int {
- var score1, score2 int
- first := true
- countDice := 0
- for {
- if score1 >= 1000 || score2 >= 1000 {
- break
- }
- if first {
- player1 = (roll() + roll() + roll() + player1) % 10
- if player1 == 0 {
- player1 = 10
- }
- score1 += player1
- first = false
- countDice += 3
- } else {
- player2 = (roll() + roll() + roll() + player2) % 10
- if player2 == 0 {
- player2 = 10
- }
- score2 += player2
- first = true
- countDice += 3
- }
- }
- if score1 > score2 {
- return score2 * countDice
- }
- return score1 * countDice
- }
- func main() {
- if len(os.Args) < 2 {
- log.Fatal("Please provide a file name as argument")
- }
- player1, player2 := readInput(os.Args[1])
- dice = 0
- fmt.Println("Part1:", part1(player1, player2))
- }
|