-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (82 loc) · 2.3 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"github.com/ThePuzzler/puzzler"
"github.com/ThePuzzler/puzzler_help"
)
type fullSolution struct {
Output []solution `json:"output"`
}
type solution struct {
Layer string `json:"layer"`
Algorithm string `json:"algorithm"`
Time string `json:"time"`
Question string `json:"question"`
Body puzzler.Puzzle `json:"body"`
}
func main() {
userHandler()
}
func userHandler() {
mode := flag.String("m", "s", "c = create a puzzle, s = solve a puzzle")
puzzlepath := flag.String("pp", "puzzle.txt", "Path of the puzzle")
maxLayers := flag.Int("ln", 10, "layers of hashes you are expecting")
solFilePath := flag.String("sp", "Solution.json", "desired filepath of solution")
createConfig := flag.String("config", "puzzlconfig.json", "filepath for the configuration json for puzzle creation")
flag.Parse()
if *maxLayers >= 20 {
panic("To many layers! What?! Are you crazy?")
}
switch *mode {
case "c":
creationMode(*createConfig, *puzzlepath)
case "s":
solveMode(*puzzlepath, maxLayers, solFilePath)
case "h":
puzzler_help.Help()
default:
panic("Please use a mode flag \n e.g \n\t -m=c -config=myconfigfile.txt -pp=puzzle.txt ")
}
}
func solveMode(puzzlepath string, maxLayers *int, solutionPath *string) {
problem := puzzler.OpenPuzzle(puzzlepath)
sol := russianDollUnhasher(maxLayers, problem)
sol.saveSolution(solutionPath)
}
func creationMode(config string, puzzlePath string) {
layerConfig := puzzler.GetCreationConfig(config)
layerConfig.MessageAsFile()
layerConfig.CreatePuzzle(puzzlePath)
}
func russianDollUnhasher(ln *int, p puzzler.Puzzle) fullSolution {
fullSol := *new(fullSolution)
for i := 1; i <= *ln; i++ {
sol := *new(solution)
s := p.UnhashPuzzleReturnSolution()
sol.Question = s[0]
sol.Algorithm = puzzler.GetAlgorithm()
sol.Time = puzzler.GetDuration().String()
sol.Body = s[1:]
sol.Layer = fmt.Sprintf("%d", i)
fullSol.Output = append(fullSol.Output, sol)
if len(s) > 2 {
p = s
} else {
return fullSol
}
}
return fullSol
}
func (f *fullSolution) saveSolution(fp *string) {
marsh, err := json.Marshal(f)
if err != nil {
panic(err.Error())
}
write_err := ioutil.WriteFile(*fp, marsh, 0664)
if write_err != nil {
panic(write_err.Error())
}
}