code.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 findRobot(line string) *Point {
  12. for i, char := range line {
  13. if char == '@' {
  14. return &Point{x: i}
  15. }
  16. }
  17. return nil
  18. }
  19. func readInput(file *os.File) (*Point, [][]byte, []byte) {
  20. scanner := bufio.NewScanner(file)
  21. var matrix [][]byte
  22. var moves []byte
  23. var robot *Point
  24. var y int
  25. readingMatrix := true
  26. for scanner.Scan() {
  27. line := scanner.Text()
  28. if line == "" {
  29. if readingMatrix {
  30. readingMatrix = false
  31. continue
  32. }
  33. break
  34. }
  35. if readingMatrix {
  36. matrix = append(matrix, []byte(line))
  37. if robot == nil {
  38. robot = findRobot(line)
  39. if robot != nil {
  40. robot.y = y
  41. }
  42. y++
  43. }
  44. } else {
  45. moves = append(moves, []byte(line)...)
  46. }
  47. }
  48. return robot, matrix, moves
  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. robot, matrix, moves := readInput(file)
  60. fmt.Println(robot, matrix, moves)
  61. }