day24.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. func readDirections(line string) []string {
  9. var path []string
  10. signs := []rune(line)
  11. size := len(signs)
  12. for i := 0; i < size; i++ {
  13. switch signs[i] {
  14. case 'e', 'w':
  15. path = append(path, string(signs[i]))
  16. case 's', 'n':
  17. path = append(path, string(signs[i:i+2]))
  18. i++
  19. }
  20. }
  21. return path
  22. }
  23. func readFile(file *os.File) [][]string {
  24. var paths [][]string
  25. scanner := bufio.NewScanner(file)
  26. for scanner.Scan() {
  27. line := scanner.Text()
  28. if line == "" {
  29. break
  30. }
  31. paths = append(paths, readDirections(line))
  32. }
  33. if err := scanner.Err(); err != nil {
  34. log.Fatalf("Scanner error: %s", err)
  35. }
  36. return paths
  37. }
  38. type position struct {
  39. x float64
  40. y float64
  41. }
  42. func makeMove(path []string) position {
  43. currentPosition := position{x: 0, y: 0}
  44. for _, item := range path {
  45. switch item {
  46. case "e":
  47. currentPosition.x += 1
  48. case "w":
  49. currentPosition.x -= 1
  50. case "se":
  51. currentPosition.x += 0.5
  52. currentPosition.y -= 0.5
  53. case "sw":
  54. currentPosition.x -= 0.5
  55. currentPosition.y -= 0.5
  56. case "nw":
  57. currentPosition.x -= 0.5
  58. currentPosition.y += 0.5
  59. case "ne":
  60. currentPosition.x += 0.5
  61. currentPosition.y += 0.5
  62. }
  63. }
  64. return currentPosition
  65. }
  66. func main() {
  67. if len(os.Args) < 2 {
  68. log.Fatal("You need to specify a file!")
  69. }
  70. filePath := os.Args[1]
  71. file, err := os.Open(filePath)
  72. if err != nil {
  73. log.Fatalf("Failed to open %s!\n", filePath)
  74. }
  75. paths := readFile(file)
  76. if err := file.Close(); err != nil {
  77. log.Fatalf("Failed to close file: %s", err)
  78. }
  79. for _, path := range paths {
  80. fmt.Println(makeMove(path))
  81. }
  82. }