-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path99_template.go
188 lines (156 loc) · 3.08 KB
/
99_template.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"fmt"
"image"
"log"
"os"
"strconv"
"strings"
"time"
. "github.com/logrusorgru/aurora"
)
func _log(a ...interface{}) {
fmt.Println(a...)
}
func _log1(a ...interface{}) {
fmt.Println(a...)
fmt.Scanln()
}
func sliceAtoi(in []string) []int {
out := make([]int, len(in))
for i, v := range in {
out[i], _ = strconv.Atoi(v)
}
return out
}
func abs(n int) int {
y := n >> 63 // y ← x ⟫ 63
return (n ^ y) - y // (x ⨁ y) - y
}
func b2i(b bool) int {
if b {
return 1
}
return 0
}
type cell = int
type pos = image.Point
type rect = image.Rectangle
type row []cell
type field []row
// map2d = field with bounds.
type map2d struct {
f field
cap, b rect
locked bool
}
func makeMap2d(w, h int) map2d {
f := make(field, 0, h)
for y := 0; y < h; y++ {
f = append(f, make(row, w))
}
return map2d{f: f, cap: rect{pos{0, 0}, pos{w, h}}}
}
func (m *map2d) get(p pos) cell {
return m.f[p.Y][p.X]
}
func (m *map2d) lock() {
m.locked = true
}
func (m *map2d) set(p pos, c cell) {
if m.locked {
panic("map is locked")
}
m.f[p.Y][p.X] = c
if !p.In(m.b) {
m.b = m.b.Union(rect{p, p.Add(pos{1, 1})})
}
}
// pStep returns position of step in specified direction. 0123 -> ESWN
func pStep(p pos, d int) pos {
return pos{p.X + (1-d)%2, p.Y + (2-d)%2}
}
// pStep2 returns position of step in specified direction. 0123 -> NEWS
// [0,-1], [-1,0], [1,0], [0,1]
func pStep2(p pos, d int) pos {
x := p.X + (1-d)%2
y := p.Y + (2-d)%2
panic("WIP")
x = d % 3 * (1)
y = 1
return pos{x, y}
}
////////////////////////////////////////////////////////////////////////
// Solution
//
type task struct{}
func parse(in string) task {
ss := strings.Split(in, "\n")
for _, s := range ss {
s = s
}
return task{}
}
func (t *task) process() {
}
func (t *task) part1() int {
return 1
}
func (t *task) part2() int {
return 2
}
////////////////////////////////////////////////////////////////////////
// tests
//
func verify(v, ex int) {
if v != ex {
log.Output(3, fmt.Sprint(v, "!=", ex))
os.Exit(1)
}
}
func test() {
t0 := time.Now()
log.SetPrefix("[test] ")
log.SetFlags(log.Lshortfile)
test1 := func(in string, ex int) {
t := parse(in)
t.process()
verify(t.part1(), ex)
}
test1(``, 1)
fmt.Println("tests passed", Black(time.Since(t0)).Bold())
}
////////////////////////////////////////////////////////////////////////
// main
//
func main() {
test()
delete(ins, "github")
delete(ins, "google")
for i, in := range ins {
fmt.Println(Brown(fmt.Sprint("=== for ", i, " ===")))
var t0 time.Time
var d time.Duration
t0 = time.Now()
t := parse(in)
d = time.Since(t0)
fmt.Println(Gray("parse:"), Black(d).Bold())
t0 = time.Now()
t.process()
d = time.Since(t0)
fmt.Println(Gray("process:"), Black(d).Bold())
t0 = time.Now()
v1 := t.part1()
d = time.Since(t0)
fmt.Println(Gray("part 1:"), Black(d).Bold(), Green(v1).Bold())
t0 = time.Now()
v2 := t.part2()
d = time.Since(t0)
fmt.Println(Gray("part 2:"), Black(d).Bold(), Green(v2).Bold())
fmt.Println()
}
}
var ins = map[string]string{
"github": `1`,
"google": `2`,
}