21 part 1
This commit is contained in:
parent
7c8ee5edcd
commit
711badad96
95
21/main.go
Normal file
95
21/main.go
Normal file
@ -0,0 +1,95 @@
|
||||
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()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user