code.go 875 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. type Point struct {
  9. x, y int
  10. }
  11. func (p *Point) key() string {
  12. return fmt.Sprintf("%d_%d", p.y, p.x)
  13. }
  14. func readInput(file *os.File) (map[byte][]Point, [][]byte) {
  15. scanner := bufio.NewScanner(file)
  16. var matrix [][]byte
  17. groups := make(map[byte][]Point)
  18. var y int
  19. for scanner.Scan() {
  20. line := scanner.Text()
  21. if line == "" {
  22. break
  23. }
  24. matrix = append(matrix, []byte(line))
  25. for x := range line {
  26. if line[x] != '.' {
  27. groups[line[x]] = append(groups[line[x]], Point{x: x, y: y})
  28. }
  29. }
  30. y++
  31. }
  32. return groups, matrix
  33. }
  34. func main() {
  35. if len(os.Args) < 2 {
  36. log.Fatal("You need to specify a file!")
  37. }
  38. filePath := os.Args[1]
  39. file, err := os.Open(filePath)
  40. if err != nil {
  41. log.Fatalf("Failed to open %s!\n", filePath)
  42. }
  43. groups, matrix := readInput(file)
  44. fmt.Println(groups, matrix)
  45. }