Skip to content

Instantly share code, notes, and snippets.

@daltonclaybrook
Last active September 22, 2016 05:02
Show Gist options
  • Save daltonclaybrook/216973c9e70faba8407ca57516d1cdbd to your computer and use it in GitHub Desktop.
Save daltonclaybrook/216973c9e70faba8407ca57516d1cdbd to your computer and use it in GitHub Desktop.
Punchcard
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
type ScanError string
func (er *ScanError) Error() string {
return string(*er)
}
type Input struct {
alphabet string
lines []string
}
func getInput() (*Input, error) {
scanner := bufio.NewScanner(os.Stdin)
fmt.Print("Alphabet: ")
scanner.Scan()
alphaComponents := strings.Split(scanner.Text(), " ")
if len(alphaComponents) != 2 {
error := ScanError("Alphabet must have exactly two components.")
return nil, &error
}
alpha := alphaComponents[1]
fmt.Print("Line count: ")
scanner.Scan()
count, err := strconv.Atoi(scanner.Text())
if err != nil {
return nil, err
}
lines := make([]string, count)
for i := 0; i < count; i++ {
fmt.Printf("Line %v: ", i+1)
scanner.Scan()
scan := scanner.Text()
lines[i] = scan
}
return &Input{alpha, lines}, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment