-
Notifications
You must be signed in to change notification settings - Fork 0
/
part01.go
116 lines (101 loc) · 1.96 KB
/
part01.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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
)
var input_file = flag.String("input", "input.txt", "Input file")
var debug = flag.Bool("debug", false, "Debug")
func main() {
flag.Parse()
file, err := os.Open(*input_file)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Scan()
line := scanner.Text()
board := make([]byte, len(line))
for i, r := range line {
board[i] = byte(r) - '0'
}
cur := 0
for move := 1; move <= 100; move++ {
fmt.Printf("-- move (%v) --\n", move)
fmt.Printf("cups: ")
for i, v := range board {
if i == cur {
fmt.Printf("(%v)", v)
} else {
fmt.Printf(" %v ", v)
}
}
fmt.Println()
pickup := make([]byte, 3)
for i := 0; i < 3; i++ {
pickup[i] = board[(cur+1+i)%len(board)]
}
fmt.Printf("pick up:")
for i, v := range pickup {
fmt.Printf(" %v", v)
if i != len(pickup)-1 {
fmt.Print(",")
}
}
fmt.Println()
dest_label := board[cur] - 1
if dest_label == 0 {
dest_label = 9
}
for dest_label == pickup[0] || dest_label == pickup[1] || dest_label == pickup[2] {
dest_label -= 1
if dest_label == 0 {
dest_label = 9
}
}
fmt.Printf("destination: %v\n", dest_label)
fmt.Println()
board2 := make([]byte, 0, len(board))
for _, v := range board {
if v == pickup[0] || v == pickup[1] || v == pickup[2] {
// do nothing
} else if v == dest_label {
board2 = append(board2, v)
board2 = append(board2, pickup[0])
board2 = append(board2, pickup[1])
board2 = append(board2, pickup[2])
} else {
board2 = append(board2, v)
}
}
cur_label := board[cur]
board = board2
for i, v := range board {
if v == cur_label {
cur = i + 1
}
}
if cur == len(board) {
cur = 0
}
}
print := false
for _, v := range board {
if print {
fmt.Print(v)
}
if v == 1 {
print = true
}
}
for _, v := range board {
if v == 1 {
break
}
fmt.Print(v)
}
fmt.Println()
}