Snippets

stíobhart matulevicz GCHQ Puzzle Cracker, Revisited

Created by stíobhart matulevicz
package main

import (
	"fmt"
)

func main() {

	//coded message
	messageline01 := "IN AAAAIAN INAAANAIA IA IAINA AI AA IAIIA IAA AAIAAINN AA IAAANN IAINANI"
	messageline02 := "NA ANNNNMA NAANIANMN NN ANNAN NN AM MNNNN ANI MAAINNIA AM NNAMIA NNAANIN"
	messageline03 := "AM MMIAAMA MMIMAAMMA MM AMAAA MA AM AAAMA AAA MAMAAAAM AM AAIMMM MMMMAMA"

	//print to stop Go whingeing about unused vars
	fmt.Println("\nMESSAGE:\n\n", messageline01, "\n", messageline02, "\n", messageline03)

	//braille alphabet map
	braille := map[string]string{
		"A": "100000",
		"B": "101000",
		"C": "110000",
		"D": "110100",
		"E": "100100",
		"F": "111000",
		"G": "111100",
		"H": "101100",
		"I": "011000",
		"J": "011100",
		"K": "100010",
		"L": "101010",
		"M": "110010",
		"N": "110110",
		"O": "100110",
		"P": "111010",
		"Q": "111110",
		"R": "101110",
		"S": "011010",
		"T": "011110",
		"U": "100011",
		"V": "101011",
		"W": "011101",
		"X": "110011",
		"Y": "110111",
		"Z": "100111",
	}
	//end braille alphabet map

	//print to stop Go whingeing about unused vars
	fmt.Println("\nBRAILLE MAP:\n\n", braille)

	//morse code map
	morse := map[string]string{
		"A": "10",
		"I": "11",
		"M": "00",
		"N": "01",
	}
	//end morse code map

	//print to stop Go whingeing about unused vars
	fmt.Println("\nMORSE MAP:\n\n", morse)

	//try building vertically
	fmt.Println("\n===========================================================")
	fmt.Println("MESSAGE — VERTICAL READING")
	fmt.Println("===========================================================\n")

	//string to hold vertmessage
	vertmessage := ""

	//loop through length of message line
	for i := 0; i < len(messageline01); i++ {
		//if we're on a blank, add a space to the string
		if string(messageline01[i]) == " " {
			vertmessage += " "
		} else {
			//build morse triplet from vert letters
			morsetriplet := string(messageline01[i]) + string(messageline02[i]) + string(messageline03[i])
			//add to vertmessage
			vertmessage += morsetriplet
		}
	}
	//end loop through length of message line

	fmt.Println("VERTMESSAGE:\n\n", vertmessage)

	fmt.Println("\n===========================================================")
	fmt.Println("VERTICAL MESSAGE DECODING:")
	fmt.Println("===========================================================\n")

	//create var to hold Esperanto string
	esperanto := ""

	//loop through length of vertmessage 3 at a time to select each morsetriplet
	for i := 0; i < len(vertmessage); i += 3 {

		//if it's a blank, add a space and increment counter [ie. skip to next letter]
		if string(vertmessage[i]) == " " {
			esperanto += " "
			i += 1
		}

		//build a braille code to lookup from the morse triplets
		braillecode := morse[string(vertmessage[i])] + morse[string(vertmessage[i+1])] + morse[string(vertmessage[i+2])]

		//look up the braillecode in braille map
		for key, value := range braille {
			if value == braillecode {
				esperanto += key
			}
		}
		//end look up braillecode in braille map

	}
	//end loop through length of vertmessage 3 at a time

	//print the constructed esperanto string
	fmt.Println(esperanto)
	fmt.Println("\n===========================================================\n")

} //end main

Comments (0)