From 711badad9644d40fb258e8ac22f419d5b39537d3 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Thu, 23 Dec 2021 18:58:54 -0700 Subject: [PATCH] 21 part 1 --- 21/main.go | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 21/main.go diff --git a/21/main.go b/21/main.go new file mode 100644 index 0000000..4a48e40 --- /dev/null +++ b/21/main.go @@ -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() + } +}