code.go 792 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. type Robot struct {
  9. x, y int
  10. vX, Vy int
  11. }
  12. func readInput(file *os.File) []Robot {
  13. scanner := bufio.NewScanner(file)
  14. var robots []Robot
  15. for scanner.Scan() {
  16. line := scanner.Text()
  17. if line == "" {
  18. break
  19. }
  20. var robot Robot
  21. n, err := fmt.Sscanf(line, "p=%d,%d v=%d,%d", &robot.x, &robot.y, &robot.vX, &robot.Vy)
  22. if n != 4 || err != nil {
  23. log.Fatalf("Not able to parse robot '%s': %s", line, err)
  24. }
  25. robots = append(robots, robot)
  26. }
  27. return robots
  28. }
  29. func main() {
  30. if len(os.Args) < 2 {
  31. log.Fatal("You need to specify a file!")
  32. }
  33. filePath := os.Args[1]
  34. file, err := os.Open(filePath)
  35. if err != nil {
  36. log.Fatalf("Failed to open %s!\n", filePath)
  37. }
  38. robots := readInput(file)
  39. fmt.Println(robots)
  40. }