Anna Wiggins
48d1a97acd
* Use uppercase letters. * Print number of iterations it took to reach a steady state. * Add a --infinite mode that loops forever, and only prints the final outcome. * [WIP] Start with a random alphabet.
118 lines
2.1 KiB
Go
118 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"sort"
|
|
)
|
|
|
|
var SPELLINGS = map[rune]string{
|
|
'A': "AY",
|
|
'B': "BEE",
|
|
'C': "SEE",
|
|
'D': "DEE",
|
|
'E': "EE",
|
|
'F': "EFF",
|
|
'G': "GEE",
|
|
'H': "AITCH",
|
|
'I': "EYE",
|
|
'J': "JAY",
|
|
'K': "KAY",
|
|
'L': "ELL",
|
|
'M': "EMM",
|
|
'N': "ENN",
|
|
'O': "OH",
|
|
'P': "PEE",
|
|
'Q': "CUE",
|
|
'R': "ARR",
|
|
'S': "ESS",
|
|
'T': "TEE",
|
|
'U': "EWE",
|
|
'V': "VEE",
|
|
'W': "DOUBLE EWE",
|
|
'X': "ECKS",
|
|
'Y': "WHY",
|
|
'Z': "ZEE",
|
|
}
|
|
|
|
func main() {
|
|
random := false
|
|
infinite := false
|
|
for i := 1; i < len(os.Args); i++ {
|
|
if os.Args[i] == "--random" {
|
|
random = true
|
|
}
|
|
if os.Args[i] == "--infinite" {
|
|
infinite = true
|
|
}
|
|
}
|
|
|
|
for {
|
|
var alphabet []rune
|
|
if random {
|
|
// todo
|
|
} else {
|
|
alphabet = []rune{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}
|
|
}
|
|
prev := string(alphabet)
|
|
fmt.Println(string(prev))
|
|
i := 0
|
|
for {
|
|
alphabet = sort_alphabet(alphabet)
|
|
if string(alphabet) == prev {
|
|
if infinite {
|
|
fmt.Println(prev)
|
|
}
|
|
break
|
|
}
|
|
prev = string(alphabet)
|
|
if !infinite {
|
|
fmt.Println(prev)
|
|
}
|
|
i++
|
|
}
|
|
fmt.Println("Iterations:", i)
|
|
|
|
if !infinite {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// sort the provided alphabet (a slice of runes) by spelling, using the alphabet itself
|
|
// as the lexicographic ordering. Return the new alphabet.
|
|
func sort_alphabet(alphabet []rune) []rune {
|
|
new_alphabet := make([]rune, len(alphabet))
|
|
copy(new_alphabet, alphabet)
|
|
sort.Slice(new_alphabet, func(i, j int) bool {
|
|
i_spelling := SPELLINGS[new_alphabet[i]]
|
|
j_spelling := SPELLINGS[new_alphabet[j]]
|
|
|
|
i_index := -1
|
|
j_index := -1
|
|
|
|
k := 0
|
|
for k = 0; k < len(i_spelling) && k < len(j_spelling); k++ {
|
|
i_index = find_index(alphabet, rune(i_spelling[k]))
|
|
j_index = find_index(alphabet, rune(j_spelling[k]))
|
|
|
|
if i_index != j_index {
|
|
break
|
|
}
|
|
}
|
|
return i_index < j_index
|
|
})
|
|
return new_alphabet
|
|
}
|
|
|
|
func find_index(alphabet []rune, target rune) int {
|
|
for i, letter := range alphabet {
|
|
if letter == target {
|
|
return i
|
|
}
|
|
}
|
|
log.Panicf("Tried to find a letter that doesn't exist")
|
|
return -1
|
|
}
|