code.go 823 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 (p Point) key() string {
  12. return fmt.Sprintf("%d_%d", p.y, p.x)
  13. }
  14. func readInput(file *os.File) []Point {
  15. scanner := bufio.NewScanner(file)
  16. var points []Point
  17. for scanner.Scan() {
  18. line := scanner.Text()
  19. if line == "" {
  20. break
  21. }
  22. var point Point
  23. n, err := fmt.Sscanf(line, "%d,%d", &point.x, &point.y)
  24. if n != 2 || err != nil {
  25. log.Fatalf("Not able to parse byte '%s': %s", line, err)
  26. }
  27. points = append(points, point)
  28. }
  29. return points
  30. }
  31. func main() {
  32. if len(os.Args) < 2 {
  33. log.Fatal("You need to specify a file!")
  34. }
  35. filePath := os.Args[1]
  36. file, err := os.Open(filePath)
  37. if err != nil {
  38. log.Fatalf("Failed to open %s!\n", filePath)
  39. }
  40. obstacles := readInput(file)
  41. fmt.Println(obstacles)
  42. }