package main import ( "bufio" "flag" "fmt" "os" "strings" "unicode" ) // Function to encode characters into their respective number pairs func encodeChar(char rune) int { // Define the alphabet and numbers with a leading space alphabet := " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // Find the index of the character index := strings.IndexRune(alphabet, char) if index == -1 { return -1 // Return -1 if character is not found } return index } // Function to apply shift and wrap around func applyShift(value, shift int) int { shifted := (value + shift) % 37 if shifted < 0 { shifted += 37 } return shifted } // Function to format the output based on group size and line size func formatOutput(encoded []int, groupSize int, lineSize int) string { var output strings.Builder for _, num := range encoded { output.WriteString(fmt.Sprintf("%02d", num)) } // Apply grouping finalOutput := output.String() var groupedOutput strings.Builder counter := 0 groupCounter := 0 for i := 0; i < len(finalOutput); i += groupSize { if i+groupSize < len(finalOutput) { groupedOutput.WriteString(finalOutput[i:i+groupSize] + " ") } else { groupedOutput.WriteString(finalOutput[i:]) } counter++ groupCounter++ // Check if we reached the line size if lineSize > 0 && groupCounter >= lineSize && i+groupSize < len(finalOutput) { groupedOutput.WriteString("\n") groupCounter = 0 } } return strings.TrimSpace(groupedOutput.String()) } func main() { // Define command-line flags shift := flag.Int("s", 0, "Shift amount for the cipher") groupSize := flag.Int("g", 0, "Group size for output formatting") lineSize := flag.Int("l", 0, "Line size by number of groups") flag.Parse() // Read input from stdin reader := bufio.NewReader(os.Stdin) input, _ := reader.ReadString('\n') input = strings.TrimSpace(input) var encoded []int // Encode the input string for _, char := range input { if !unicode.IsLetter(char) && !unicode.IsDigit(char) && char != ' ' { continue // Skip characters not in the defined alphabet and numbers } encodedValue := encodeChar(unicode.ToUpper(char)) if encodedValue == -1 { continue // Skip characters not found in the alphabet string } shiftedValue := applyShift(encodedValue, *shift) encoded = append(encoded, shiftedValue) } // Format the output formattedOutput := formatOutput(encoded, *groupSize, *lineSize) fmt.Println(formattedOutput) }