-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
128 lines (102 loc) · 2.49 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"fmt"
"os"
"github.com/devries/advent_of_code_2021/utils"
)
func main() {
f, err := os.Open("../inputs/day21.txt")
utils.Check(err, "error opening input file")
defer f.Close()
lines := utils.ReadLines(f)
var startA, startB int
_, err = fmt.Sscanf(lines[0], "Player 1 starting position: %d", &startA)
utils.Check(err, "unable to parse input")
_, err = fmt.Sscanf(lines[1], "Player 2 starting position: %d", &startB)
utils.Check(err, "unable to parse input")
r := solve(startA, startB)
fmt.Println(r)
}
func solve(startA int, startB int) int {
// The dice rolls are 9N+6 from N=0... This means a %10 sequence of moves for
// each player that repeats.
movesA := []int{6, 4, 2, 0, 8}
movesB := []int{5, 3, 1, 9, 7}
pointsA := getPointSequence(startA, movesA)
pointsB := getPointSequence(startB, movesB)
turnsA := movesToWin(pointsA)
turnsB := movesToWin(pointsB)
var turns, scoreA, scoreB, rolls, lowScore int
if turnsA < turnsB {
turns = turnsA
scoreA = score(turns, pointsA)
scoreB = score(turns-1, pointsB)
rolls = turns*6 - 3
lowScore = scoreB
} else {
turns = turnsB
scoreA = score(turns, pointsA)
scoreB = score(turns, pointsB)
rolls = turns * 6
lowScore = scoreA
}
return rolls * lowScore
}
func getPointSequence(start int, moves []int) []int {
// A point sequence derives from the move sequence, but may not be the same length
moveSum := 0
for _, v := range moves {
moveSum += v
}
cycleOffset := moveSum % 10
var repeats int
if cycleOffset == 0 {
repeats = 1
} else {
lcm := int(utils.Lcm(int64(cycleOffset), 10))
repeats = lcm / cycleOffset
}
ret := make([]int, 0)
pos := start
for i := 0; i < repeats*len(moves); i++ {
pos += moves[i%len(moves)]
pos %= 10
if pos == 0 {
ret = append(ret, 10)
} else {
ret = append(ret, pos)
}
}
return ret
}
func movesToWin(pointSequence []int) int {
total := 0
for _, i := range pointSequence {
total += i
}
multiples := 1000 / total
points := multiples * total
moves := multiples * len(pointSequence)
for i := 0; i < len(pointSequence); i++ {
if points >= 1000 {
return moves
}
moves += 1
points += pointSequence[i]
}
// Should never get here
return 0
}
func score(moves int, pointSequence []int) int {
total := 0
for _, i := range pointSequence {
total += i
}
seqLen := len(pointSequence)
repeats := moves / seqLen
score := repeats * total
for m := repeats * seqLen; m < moves; m++ {
score += pointSequence[m%seqLen]
}
return score
}