code.go 1.1 KB

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