code.go 712 B

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