code.go 832 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. type point struct {
  9. x int
  10. y int
  11. }
  12. func readInput(file *os.File) [][2]point {
  13. scanner := bufio.NewScanner(file)
  14. var points [][2]point
  15. for scanner.Scan() {
  16. line := scanner.Text()
  17. if line == "" {
  18. continue
  19. }
  20. var current [2]point
  21. n, err := fmt.Sscanf(line, "Sensor at x=%d, y=%d: closest beacon is at x=%d, y=%d", &current[0].x, &current[0].y, &current[1].x, &current[1].y)
  22. if n != 4 || err != nil {
  23. log.Fatal("Can't parse", line, err)
  24. }
  25. points = append(points, current)
  26. }
  27. return points
  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. points := readInput(file)
  39. fmt.Println(points)
  40. }