|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +type Cup struct { |
| 10 | + Id int |
| 11 | + Next *Cup |
| 12 | +} |
| 13 | + |
| 14 | +var debug = false |
| 15 | + |
| 16 | +func do(input string, games int, ncups int) (ret1 string, ret2 int) { |
| 17 | + |
| 18 | + // cups := make([]int, len(input)) |
| 19 | + c := &Cup{} |
| 20 | + first := c |
| 21 | + |
| 22 | + prev := c |
| 23 | + // cupmap := make([]*Cup, ncups) |
| 24 | + cupmap := make(map[int](*Cup)) |
| 25 | + for _, v := range input { |
| 26 | + c.Id = int(byte(v - '0')) |
| 27 | + cupmap[c.Id] = c |
| 28 | + prev = c |
| 29 | + c.Next = &Cup{} |
| 30 | + c = c.Next |
| 31 | + } |
| 32 | + |
| 33 | + // log.Println("start at", len(input)+1) |
| 34 | + for v := len(input) + 1; v <= ncups; v++ { |
| 35 | + c.Id = v |
| 36 | + cupmap[c.Id] = c |
| 37 | + |
| 38 | + prev = c |
| 39 | + c.Next = &Cup{} |
| 40 | + c = c.Next |
| 41 | + } |
| 42 | + // log.Println("map is", len(cupmap)) |
| 43 | + |
| 44 | + prev.Next = first |
| 45 | + c.Next = first |
| 46 | + // prev := c |
| 47 | + c = first |
| 48 | + min := int(9999) |
| 49 | + max := int(0) |
| 50 | + if games > 1000000 { |
| 51 | + min = 1 |
| 52 | + max = ncups |
| 53 | + } |
| 54 | + |
| 55 | + for _, v := range input { |
| 56 | + _ = v |
| 57 | + // log.Printf("%d %+v\n", v, *c) |
| 58 | + if min > c.Id { |
| 59 | + min = c.Id |
| 60 | + } |
| 61 | + if max < c.Id { |
| 62 | + max = c.Id |
| 63 | + } |
| 64 | + c = c.Next |
| 65 | + } |
| 66 | + |
| 67 | + c = first |
| 68 | + // log.Println("minmax", min, max) |
| 69 | + pickedup := make([]*Cup, 3) |
| 70 | + |
| 71 | + t1 := time.Now() |
| 72 | + for moves := 1; moves <= games; moves++ { |
| 73 | + if time.Since(t1) > 5*time.Second { |
| 74 | + log.Println("Move: ", moves) |
| 75 | + t1 = time.Now() |
| 76 | + } |
| 77 | + if debug { |
| 78 | + log.Println("Move", moves) |
| 79 | + c2 := first |
| 80 | + for { |
| 81 | + if c2.Id == c.Id { |
| 82 | + fmt.Printf("(%d) ", c2.Id) |
| 83 | + } else { |
| 84 | + fmt.Printf("%d ", c2.Id) |
| 85 | + } |
| 86 | + c2 = c2.Next |
| 87 | + if c2 == first { |
| 88 | + break |
| 89 | + } |
| 90 | + } |
| 91 | + fmt.Println() |
| 92 | + } |
| 93 | + |
| 94 | + prev = c |
| 95 | + _ = prev |
| 96 | + c2 := c.Next |
| 97 | + |
| 98 | + for i := 0; i < 3; i++ { |
| 99 | + pickedup[i] = c2 |
| 100 | + if debug { |
| 101 | + fmt.Println("Picked up:", c2.Id) |
| 102 | + } |
| 103 | + c2 = c2.Next |
| 104 | + } |
| 105 | + prev.Next = c2 |
| 106 | + |
| 107 | + if debug { |
| 108 | + log.Println("dump") |
| 109 | + c2 := first |
| 110 | + for { |
| 111 | + if c2.Id == c.Id { |
| 112 | + fmt.Printf("(%d) ", c2.Id) |
| 113 | + } else { |
| 114 | + fmt.Printf("%d ", c2.Id) |
| 115 | + } |
| 116 | + c2 = c2.Next |
| 117 | + if c2 == first { |
| 118 | + break |
| 119 | + } |
| 120 | + } |
| 121 | + fmt.Println() |
| 122 | + } |
| 123 | + |
| 124 | + dest := c.Id - 1 |
| 125 | + if dest < min { |
| 126 | + dest = max |
| 127 | + } |
| 128 | + a: |
| 129 | + for i := 0; i < 3; i++ { |
| 130 | + // log.Println("Check", dest, pickedup[i].Id) |
| 131 | + if dest == pickedup[i].Id { |
| 132 | + dest-- |
| 133 | + if dest < min { |
| 134 | + // log.Println("Roll") |
| 135 | + dest = max |
| 136 | + } |
| 137 | + goto a |
| 138 | + } |
| 139 | + } |
| 140 | + if debug { |
| 141 | + log.Println("Dest", dest) |
| 142 | + } |
| 143 | + |
| 144 | + c2 = c |
| 145 | + |
| 146 | + if false { |
| 147 | + for c2.Id != dest { |
| 148 | + // prev = c |
| 149 | + c2 = c2.Next |
| 150 | + } |
| 151 | + } else { |
| 152 | + c2 = cupmap[dest] |
| 153 | + } |
| 154 | + |
| 155 | + if c2 == nil { |
| 156 | + log.Println("nil", dest) |
| 157 | + } |
| 158 | + |
| 159 | + next := c2.Next |
| 160 | + for i := 0; i < 3; i++ { |
| 161 | + c2.Next = pickedup[i] |
| 162 | + c2 = c2.Next |
| 163 | + } |
| 164 | + c2.Next = next |
| 165 | + |
| 166 | + c = c.Next |
| 167 | + if debug { |
| 168 | + log.Println("dump2") |
| 169 | + c2 := first |
| 170 | + for { |
| 171 | + if c2.Id == c.Id { |
| 172 | + fmt.Printf("(%d) ", c2.Id) |
| 173 | + } else { |
| 174 | + fmt.Printf("%d ", c2.Id) |
| 175 | + } |
| 176 | + c2 = c2.Next |
| 177 | + if c2 == first { |
| 178 | + break |
| 179 | + } |
| 180 | + } |
| 181 | + fmt.Println() |
| 182 | + } |
| 183 | + first = c |
| 184 | + } |
| 185 | + |
| 186 | + for first.Id != 1 { |
| 187 | + first = first.Next |
| 188 | + } |
| 189 | + c = first.Next |
| 190 | + |
| 191 | + if ncups < 100 { |
| 192 | + for c != first { |
| 193 | + ret1 += fmt.Sprint(c.Id) |
| 194 | + c = c.Next |
| 195 | + } |
| 196 | + } else { |
| 197 | + // log.Println(first.Id, c.Id, c.Next.Id) |
| 198 | + ret2 = c.Id * c.Next.Id |
| 199 | + } |
| 200 | + |
| 201 | + return ret1, ret2 |
| 202 | +} |
| 203 | + |
| 204 | +func main() { |
| 205 | + // log.Println(do("389125467", 10)) |
| 206 | + // log.Println(do("389125467", 100)) |
| 207 | + // log.Println(do("389125467", 10000000, 1000000)) |
| 208 | + // log.Println(do("389125467", 100, 10)) |
| 209 | + // log.Println(do("219347865", 100)) |
| 210 | + log.Println(do("219347865", 100, 9)) |
| 211 | + log.Println(do("219347865", 10000000, 1000000)) |
| 212 | +} |
0 commit comments