Last active
August 29, 2015 14:23
-
-
Save miry/5f4838d03ddb30d062c4 to your computer and use it in GitHub Desktop.
Sample application to encode text to characters of machine turing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "os" | |
import "bufio" | |
import "strings" | |
/** | |
* Auto-generated code below aims at helping you parse | |
* the standard input according to the problem statement. | |
**/ | |
var zones [30]int = [30]int{} | |
const alphavit_size = 27 | |
const middle_number = 13 | |
func main() { | |
for i := range(zones) { | |
zones[i] = ' ' | |
} | |
scanner := bufio.NewScanner(os.Stdin) | |
scanner.Scan() | |
magicPhrase := scanner.Text() | |
current_zone := 0 | |
for _, l := range(magicPhrase) { | |
code := int(l) | |
if zones[current_zone] == code { | |
fmt.Print(".") | |
continue | |
} | |
minls := middle_number+1 | |
var result string | |
var str string | |
var ls int | |
zone := current_zone | |
nzone := current_zone | |
pzone := current_zone | |
str, ls = zoneRoutes(zone, l) | |
minls = ls | |
result = str | |
zone = nzone | |
for j := 1; j < minls-1; j += 1 { | |
nzone = nextZone(nzone) | |
str, ls = zoneRoutes(nzone, l) | |
ls += j | |
str = strings.Repeat(">", j) + str | |
if minls > ls { | |
minls = ls | |
result = str | |
zone = nzone | |
} | |
pzone = prevZone(pzone) | |
str, ls = zoneRoutes(pzone, l) | |
ls += j | |
str = strings.Repeat("<", j) + str | |
if minls > ls { | |
minls = ls | |
result = str | |
zone = pzone | |
} | |
} | |
fmt.Print(result+".") | |
zones[zone] = code | |
current_zone = zone | |
} | |
fmt.Println("") | |
} | |
func simpleDistance(li, ci int) string { | |
var c string | |
var abs_d int | |
d := li - ci | |
if d > 0 { | |
c = "+" | |
abs_d = d | |
} else { | |
abs_d = -d | |
c = "-" | |
} | |
return strings.Repeat(c, abs_d) | |
} | |
func roundSimpleDistance(li, ci int) string { | |
var c string | |
var abs_d int | |
d := li - ci | |
if d > 0 { | |
c = "+" | |
abs_d = d | |
} else { | |
c = "-" | |
abs_d = -d | |
} | |
if abs_d > middle_number { | |
abs_d = alphavit_size - abs_d | |
if c == "+" { | |
c = "-" | |
} else { | |
c = "+" | |
} | |
} | |
return strings.Repeat(c, abs_d) | |
} | |
func zoneRoutes(current_zone int, l rune) (string, int) { | |
result := [2]string{} | |
code := int(l) | |
if zones[current_zone] == code { | |
return "", 0 | |
} | |
current_char_index := 0 | |
if zones[current_zone] != ' ' { | |
current_char_index = int(zones[current_zone] - 'A' + 1) | |
} | |
letter_index := 0 | |
if l != ' ' { | |
letter_index = int(l - 'A' + 1) | |
} | |
result[0] = simpleDistance(letter_index, current_char_index) | |
result[1] = roundSimpleDistance(letter_index, current_char_index) | |
min := 0 | |
lms := len(result[min]) | |
for i, s := range(result) { | |
ls := len(s) | |
if lms > ls { | |
min = i | |
lms = ls | |
} | |
} | |
return result[min], lms | |
} | |
func prevZone(zone int) int { | |
if zone == 0 { | |
return 29 | |
} | |
return zone - 1 | |
} | |
func nextZone(zone int) int { | |
if zone == 29 { | |
return 0 | |
} | |
return zone + 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment