-
Notifications
You must be signed in to change notification settings - Fork 0
/
30.go
92 lines (83 loc) · 2.64 KB
/
30.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
package main
import (
"fmt"
"net/http"
"io/ioutil"
"strings"
"image"
"image/color"
"image/png"
"os"
"strconv"
)
func main(){
data, _ := getbody("yankeedoodle.html")
data, _ = getbody("yankeedoodle.csv")
page := strings.ReplaceAll(string(data), "\n", "")
var fwords []string
for _, line := range strings.Split(page, ",") {
fwords = append(fwords, strings.TrimSpace(line))
}
fmt.Println(yell("fwords/"), fwords[:42])
fmt.Println(yell("len/"), len(fwords))
var factors []int
for n := len(fwords) - 1; n > 1; n-- {
if len(fwords) % n == 0 {
factors = append(factors, n)
}
}
if len(factors) != 2 { panic("!=2") }
fmt.Println(yell("fact/"), factors)
// conversion: []float32
floats := make([]float32, len(fwords))
for i, word := range fwords {
f, _ := strconv.ParseFloat(word, 32)
floats[i] = float32(f)
}
// write to image
outfile, _ := os.Create("lv30.png")
defer outfile.Close()
imgout := image.NewGray(image.Rect(0, 0, factors[1], factors[0]))
for i, f := range floats {
r := i / factors[1]
c := i % factors[1]
grey := uint8(f * 255)
imgout.SetGray(c, r, color.Gray{Y: grey})
}
png.Encode(outfile, imgout)
// reversed image/// do it again
outfile, _ = os.Create("lv30r.png")
defer outfile.Close()
imgout = image.NewGray(image.Rect(0, 0, factors[0], factors[1]))
for i, f := range floats {
r := i / factors[1]
c := i % factors[1]
grey := uint8(f * 255)
imgout.SetGray(r, c, color.Gray{Y: grey})
}
png.Encode(outfile, imgout)
// n = str(x[i])[5] + str(x[i+1])[5] + str(x[i+2][]6)
var res []uint8
for i := 0; i < len(fwords) - 3; i += 3 {
s := string(fwords[i][5]) + string(fwords[i + 1][5]) + string(fwords[i + 2][6])
n, _ := strconv.Atoi(s)
if n > 255 { panic(">255") }
res = append(res, uint8(n))
}
fmt.Println(yell("res/snip"), res[:42], len(res))
fmt.Println(yell("res/"), string(res))
}
func getbody(sub string) ( []uint8, error ) {
URL := "http://www.pythonchallenge.com/pc/ring/"
conn := & http.Client{}
req, _ := http.NewRequest("GET", URL + sub, nil)
req.SetBasicAuth( "repeat", "switch" )
resp, _ := conn.Do(req)
defer resp.Body.Close()
data, _ := ioutil.ReadAll(resp.Body)
//fmt.Println(string(data), yell("\bbody ends/"))
return data, nil
}
const Yell, Cyan, Rest string = "\033[33m", "\033[36m", "\033[0m"
func yell(s string) string { return Yell + s + Rest }
func cyan(s string) string { return Cyan + s + Rest }