-
Notifications
You must be signed in to change notification settings - Fork 0
/
p59.go
76 lines (60 loc) · 1.24 KB
/
p59.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"time"
)
func main() {
start := time.Now()
f, _ := os.Open("./data/d59")
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanWords)
alphabet := make([]rune, 26)
for i := 0; i < 26; i++ {
alphabet[i] = rune('a' + i)
}
mesg := []rune{}
key := make([]rune, 3)
percentage := float64(0)
for scanner.Scan() {
text := scanner.Text()
ascii, _ := strconv.Atoi(text)
mesg = append(mesg, rune(ascii))
}
for _, a := range alphabet {
for _, b := range alphabet {
for _, c := range alphabet {
tmpKey := make([]rune, 3)
tmpKey[0] = a
tmpKey[1] = b
tmpKey[2] = c
cnt := float64(0)
for i := 0; i < len(mesg); i++ {
word := mesg[i] ^ tmpKey[i%3]
if ('A' <= word && word <= 'Z') || ('a' <= word && word <= 'z') {
cnt++
}
}
if cnt/float64(len(mesg)) > percentage {
key = tmpKey
percentage = cnt / float64(len(mesg))
}
}
}
}
for i := 0; i < len(mesg); i++ {
mesg[i] ^= key[i%3]
}
fmt.Println("Key:", string(key))
fmt.Println("Mesg:", string(mesg))
ans := 0
for i := 0; i < len(mesg); i++ {
ans += int(mesg[i])
}
fmt.Println(ans)
f.Close()
elapsed := time.Since(start)
fmt.Println("Elasped:", elapsed)
}