-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.go
78 lines (67 loc) · 1.32 KB
/
common.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
package main
import (
"bufio"
"os"
"strconv"
)
type Node struct {
Val int
Prev, Next *Node
}
func parse() ([]*Node, *Node) {
index := make([]*Node, 0)
var zero *Node
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
num, _ := strconv.Atoi(line)
newNode := &Node{Val: num}
index = append(index, newNode)
if num == 0 {
zero = newNode
}
}
// link items each other
for i := 0; i < len(index); i++ {
index[i].Prev = index[(i+len(index)-1)%len(index)]
index[i].Next = index[(i+1)%len(index)]
}
return index, zero
}
func groveCoordinates(curr *Node) int {
sum := 0
for i := 0; ; i++ {
switch i {
case 1000, 2000:
sum += curr.Val
case 3000:
return sum + curr.Val
}
curr = curr.Next
}
}
func move(curr *Node, delta int) *Node {
for ; delta > 0; delta-- {
curr = curr.Next
}
for ; delta < 0; delta++ {
curr = curr.Prev
}
return curr
}
func mix(index []*Node) {
for _, p := range index {
// Remove p from the chain
p.Prev.Next = p.Next
p.Next.Prev = p.Prev
curr := move(
p.Prev, // left side element in order to add p to the right.
p.Val%(len(index)-1), // -1 because I'm rotating on the list without p
)
// insert p to the right of curr
p.Prev = curr
p.Next = curr.Next
curr.Next.Prev = p
curr.Next = p
}
}