code.go 922 B

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