code.go 739 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "strings"
  8. )
  9. type move struct {
  10. direction string
  11. steps int
  12. }
  13. func readInput(file string) []move {
  14. var moves []move
  15. content, err := ioutil.ReadFile(file)
  16. if err != nil {
  17. log.Fatal(err)
  18. }
  19. lines := strings.Split(string(content), "\n")
  20. for _, line := range lines {
  21. if line == "" {
  22. continue
  23. }
  24. var currentMove move
  25. n, err := fmt.Sscanf(line, "%s %d", &currentMove.direction, &currentMove.steps)
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. if n != 2 {
  30. continue
  31. }
  32. moves = append(moves, currentMove)
  33. }
  34. return moves
  35. }
  36. func main() {
  37. if len(os.Args) < 2 {
  38. log.Fatal("No input file specified")
  39. }
  40. moves := readInput(os.Args[1])
  41. fmt.Println(moves)
  42. }