This repository has been archived by the owner on May 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomart.go
152 lines (127 loc) · 2.29 KB
/
randomart.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
package randomart
type AugmentFunc func(x, y int)
type StepFunc func(x, y, width, heigth, inst int) (nextx, nexty int)
const (
SSH_FLDSIZE_X = 17
SSH_FLDSIZE_Y = 9
)
func DiagonalStep(x, y, maxx, maxy, inst int) (nextx, nexty int) {
if (inst & 0x1) != 0 {
if x+1 < maxx {
x++
}
} else {
if x > 0 {
x--
}
}
if (inst & 0x2) != 0 {
if y+1 < maxy {
y++
}
} else {
if y > 0 {
y--
}
}
return x, y
}
func GridWrapStep(x, y, maxx, maxy, inst int) (nextx, nexty int) {
switch inst {
case 0:
x++
case 1:
x--
case 2:
y++
case 3:
y--
}
if x < 0 {
x = maxx - 1
}
if y < 0 {
y = maxy - 1
}
return x % maxx, y % maxy
}
func OctogonalStep(x, y, maxx, maxy, inst int) (nextx, nexty int) {
switch inst {
case 0:
x++
case 1:
x++
y++
case 2:
y++
case 3:
x--
y++
case 4:
x--
case 5:
x--
y--
case 6:
y--
case 7:
x++
y--
}
if x < 0 {
x = 0
}
if y < 0 {
y = 0
}
if x >= maxx {
x = maxx - 1
}
if y >= maxy {
y = maxy - 1
}
return x, y
}
func OpenSSH(instructions []byte) (ret [SSH_FLDSIZE_Y][SSH_FLDSIZE_X]byte) {
const augmentation_string = " .o+=*BOX@%&#/^SE"
l := len(augmentation_string) - 1
var field [SSH_FLDSIZE_X][SSH_FLDSIZE_Y]int
var lastx, lasty int
augment := func(x, y int) {
lastx, lasty = x, y
if field[x][y] < l-2 {
field[x][y]++
}
}
Generic(instructions, 2, SSH_FLDSIZE_X/2, SSH_FLDSIZE_Y/2, SSH_FLDSIZE_X, SSH_FLDSIZE_Y, DiagonalStep, augment)
// Mark starting point and end point.
field[SSH_FLDSIZE_X/2][SSH_FLDSIZE_Y/2] = l - 1
field[lastx][lasty] = l
for x := 0; x < SSH_FLDSIZE_X; x++ {
for y := 0; y < SSH_FLDSIZE_Y; y++ {
val := field[x][y]
if val > len(augmentation_string)-1 {
val = len(augmentation_string) - 1
}
ret[y][x] = augmentation_string[val]
}
}
return
}
func Generic(instructions []byte, isize uint, startx, starty, width, height int, step StepFunc, augment AugmentFunc) {
xpos := startx
ypos := starty
var register uint64
var registerBits uint
for _, b := range instructions {
register |= (uint64(b) << registerBits)
registerBits += 8
for registerBits >= isize {
inst := int(register & ((1 << isize) - 1))
register >>= isize
registerBits -= isize
xpos, ypos = step(xpos, ypos, width, height, inst)
augment(xpos, ypos)
}
}
}