-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path9.go
50 lines (44 loc) · 874 Bytes
/
9.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
package main
import "fmt"
type Node struct {
Value int
Prev, Next *Node
}
func (prev *Node) Insert(value int) *Node {
next := prev.Next
node := &Node{value, prev, next}
prev.Next, next.Prev = node, node
return node
}
func (n *Node) Remove() *Node {
next, prev := n.Next, n.Prev
prev.Next, next.Prev = next, prev
return next
}
func run(numPlayers, numMarbles int) int {
node := &Node{}
node.Prev, node.Next = node, node
players := make([]int, numPlayers)
for i := 1; i <= numMarbles; i++ {
if i%23 == 0 {
for j := 0; j < 7; j++ {
node = node.Prev
}
players[i%numPlayers] += i + node.Value
node = node.Remove()
} else {
node = node.Next.Insert(i)
}
}
max := players[0]
for _, p := range players {
if p > max {
max = p
}
}
return max
}
func main() {
fmt.Println(run(419, 71052))
fmt.Println(run(419, 71052*100))
}