code.go 885 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strings"
  8. )
  9. const delta byte = 97
  10. func readInput(file *os.File) ([][]string, []string) {
  11. scanner := bufio.NewScanner(file)
  12. patterns := make([][]string, 25)
  13. var towels []string
  14. var patternsRead bool
  15. for scanner.Scan() {
  16. line := scanner.Text()
  17. if line == "" {
  18. patternsRead = true
  19. continue
  20. }
  21. if !patternsRead {
  22. parts := strings.Split(line, ", ")
  23. for _, part := range parts {
  24. patterns[part[0]-delta] = append(patterns[part[0]-delta], part)
  25. }
  26. } else {
  27. towels = append(towels, line)
  28. }
  29. }
  30. return patterns, towels
  31. }
  32. func main() {
  33. if len(os.Args) < 2 {
  34. log.Fatal("You need to specify a file!")
  35. }
  36. filePath := os.Args[1]
  37. file, err := os.Open(filePath)
  38. if err != nil {
  39. log.Fatalf("Failed to open %s!\n", filePath)
  40. }
  41. patterns, towels := readInput(file)
  42. fmt.Println(patterns, towels)
  43. }