AdventOfCode2021/21/main.go

96 lines
1.5 KiB
Go
Raw Permalink Normal View History

2021-12-24 01:58:54 +00:00
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"time"
)
func mustAtoi(line string) int {
i, _ := strconv.Atoi(line)
return i
}
func main() {
start := time.Now()
partOne()
duration := time.Since(start)
partTwo()
duration2 := time.Since(start)
fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration)
}
func makeScanner(test bool) *bufio.Scanner {
var f *os.File
if test {
f, _ = os.Open("inputs/testinput")
} else {
f, _ = os.Open("inputs/input")
}
reader := bufio.NewReader(f)
return bufio.NewScanner(reader)
}
func MakeDeterministicDie(max int) func() int {
currentDieValue := 0
return func() int {
if currentDieValue >= max {
currentDieValue = 0
}
currentDieValue++
return currentDieValue
}
}
func RollThree(f func() int) int {
return f() + f() + f()
}
func partOne() {
scanner := makeScanner(false)
scanner.Scan()
player1 := mustAtoi(strings.Split(scanner.Text(), " ")[4])
scanner.Scan()
player2 := mustAtoi(strings.Split(scanner.Text(), " ")[4])
die := MakeDeterministicDie(100)
score1 := 0
score2 := 0
rolls := 0
for {
player1 = player1 + RollThree(die)
rolls += 3
for player1 > 10 {
player1 -= 10
}
score1 += player1
if score1 >= 1000 {
fmt.Println(score2 * rolls)
break
}
player2 = player2 + RollThree(die)
rolls += 3
for player2 > 10 {
player2 -= 10
}
score2 += player2
if score2 >= 1000 {
fmt.Println(score1 * rolls)
break
}
}
}
func partTwo() {
scanner := makeScanner(false)
for scanner.Scan() {
// line := scanner.Text()
}
}