-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (74 loc) · 2.18 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
package main
import (
"a-very-golf/anagram"
"a-very-golf/hashmap"
"a-very-golf/permutation"
"fmt"
"log"
"os"
"time"
)
var Iterations = 0
func main() {
// Ensure user entered valid unsorted anagram.
success, err := anagram.IsValidInput()
if err != nil {
log.Fatal(err)
}
var unsortedAnagram string
if success {
unsortedAnagram = os.Args[1]
}
hashmap.AnagramCandidates = make(map[string]int)
hashmap.SeenAnagrams = make(map[string]int)
timeStart := time.Now()
fmt.Printf("Starting at %d:%d.%d\n", timeStart.Local().Hour(), timeStart.Local().Minute(), timeStart.Local().Second())
// Generate anagrams and extract valid strings of English words.
permutation.FindAnagramCandidates(unsortedAnagram, 0, len(unsortedAnagram))
wordsPerLine := 6
wordsPrinted := 0
var wordsToPrint []string
// Print anagrams.
if len(hashmap.AnagramCandidates) > 0 {
anagramsCount := len(hashmap.AnagramCandidates) - 1
i := 0
for anagram := range hashmap.AnagramCandidates {
wordsToPrint = append(wordsToPrint, anagram)
wordsPrinted++
if wordsPrinted%wordsPerLine == 0 {
fmt.Println(assembleAnagramsOnSameLine(wordsToPrint))
// Empty our words to print slice.
wordsToPrint = nil
// Print all remaining words if we are iterating over the last entry in `AnagramCandidates`.
} else if anagramsCount == i {
fmt.Println(assembleAnagramsOnSameLine(wordsToPrint) + "\n")
}
i++
}
// We didn't find any anagrams.
} else {
fmt.Println("Not a single anagram was found. oof.")
}
timeFinish := time.Now()
fmt.Printf("Finished at %d:%d.%d\n", timeFinish.Local().Hour(), timeFinish.Local().Minute(), timeFinish.Local().Second())
fmt.Printf("Time elapsed: %s\n", timeFinish.Sub(timeStart))
fmt.Printf("Iterations: %d", permutation.Iterations)
}
// Assembles however many words are in the given slice on one string.
// We can outut more results into a grid in the terminal this way.
func assembleAnagramsOnSameLine(anagrams []string) string {
if len(anagrams) == 0 {
return ""
}
var output string
wordsOnLine := 0
for _, anagram := range anagrams {
if wordsOnLine == 0 {
output = anagram
} else {
output += " | " + anagram
}
wordsOnLine++
}
return output
}