-
Notifications
You must be signed in to change notification settings - Fork 0
/
22.go
146 lines (126 loc) · 4.18 KB
/
22.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
package main
import (
"fmt"
"net/http"
"io/ioutil"
"regexp"
"reflect"
"bytes"
"image"
"image/gif"
"image/color"
)
var rawgif []byte
const URL string = "http://www.pythonchallenge.com/pc/hex/"
const Yell, Cyan, Rest string = "\033[33m", "\033[36m", "\033[0m"
func main(){
fmt.Println("typ/", reflect.TypeOf(rawgif), "len/", len(rawgif))
// treat/read data as gif
reader := bytes.NewReader(rawgif)
readgif, _ := gif.DecodeAll(reader)
fmt.Println("typ/reader", reflect.TypeOf(reader))
fmt.Println("typ/readgif", reflect.TypeOf(readgif))
// count frames, all frames have same bounds
nframes := 0
frameset := make(map[image.Rectangle]bool)
pxlmap := make(map[color.Color]int)
ncenter := 0
// we also know 100,100 happens 5 times
FiveGuys := [][][]int {}
for i := 0; i < 5; i++ { FiveGuys = append(FiveGuys, [][]int{}) }
xcurr, ycurr := 100, 100
xmin, xmax, ymin, ymax := 200, -1, 200, -1
for _, frame := range readgif.Image {
nframes++
frameset[frame.Bounds()] = true
// inspect all pixels
X, Y := frame.Bounds().Max.X, frame.Bounds().Max.Y
x, y := 0, 0
for y < Y {
x = 0
for x < X {
r, g, b, _ := frame.At(x, y).RGBA()
// if r / 257 == 8 && g / 257 == 8 && b / 257 == 8 {
if r >> 8 == 8 && g >> 8 == 8 && b >> 8 == 8 {
//fmt.Println(Cyan + "color 8/" + Rest, x, y)
if x == 100 && y == 100 {
ncenter++
xcurr, ycurr = 100, 100
}
if x < 100 { xcurr-- } else if x > 100 { xcurr++ }
if y < 100 { ycurr-- } else if y > 100 { ycurr++ }
if xmin > xcurr { xmin = xcurr }
if xmax < xcurr { xmax = xcurr }
if ymin > ycurr { ymin = ycurr }
if ymax < ycurr { ymax = ycurr }
//fmt.Println("x,y/", x, y, "curr/", xcurr, ycurr, "nth letter/", ncenter)
FiveGuys[ncenter-1] = append(
FiveGuys[ncenter-1], []int{ xcurr, ycurr })
}
pxlmap[frame.At(x, y)]++
x++
}
y++
}
}
// deduction along the way
fmt.Println("nframes/", nframes, "set/", frameset)
fmt.Println(Yell + "\t^ a total of 133 frames" + Rest)
for k, v := range pxlmap { fmt.Println("color/", k, "qty", v) }
fmt.Println(Yell + "\t^ one of 2 existing colors has but 1-pix per frame" + Rest)
fmt.Println("ncenter/", "coor(100, 100) reached", ncenter, "times")
// Bruteforce
xoffset, yoffset := xmax - xmin + 1, ymax - ymin + 1
for i, coors := range FiveGuys {
char := [][]string{}
var r, c int
r = 0
for r < yoffset {
temp := []string{}
c = 0
for c < xoffset {
temp = append(temp, " ")
c++
}
r++
char = append(char, temp)
}
for _, coor := range coors {
x, y := coor[0], coor[1]
char[y - ymin][x - xmin] = "@"
}
r = 0
for r < yoffset {
c = 0
s := ""
for c < xoffset {
s += char[r][c]
c++
}
fmt.Println(s, "//")
r++
}
fmt.Println(Yell + "\n\t---", i, "---\n", Rest)
}
}
func init(){
// GET
conn := & http.Client{}
req, _ := http.NewRequest("GET", URL + "copper.html", nil)
req.SetBasicAuth( "butter", "fly" )
resp, _ := conn.Do(req)
defer resp.Body.Close()
// to be told how url should be modified
temp, _ := ioutil.ReadAll(resp.Body)
body := string(temp)
// get sub2, ie. white.gif
re := regexp.MustCompile(`(?s)maybe (.*?) would`)
matches := re.FindAllStringSubmatch(body, -1)
sub2 := matches[0][1]
conn = & http.Client{}
req, _ = http.NewRequest("GET", URL + sub2, nil)
req.SetBasicAuth( "butter", "fly" )
resp, _ = conn.Do(req)
defer resp.Body.Close()
rawgif, _ = ioutil.ReadAll(resp.Body)
}