code.go 767 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. type box struct {
  9. x, y, z int
  10. id int
  11. }
  12. func readInput(file *os.File) []box {
  13. scanner := bufio.NewScanner(file)
  14. var boxes []box
  15. var id int
  16. for scanner.Scan() {
  17. line := scanner.Text()
  18. if line == "" {
  19. continue
  20. }
  21. var x, y, z int
  22. n, err := fmt.Sscanf(line, "%d,%d,%d", &x, &y, &z)
  23. if n != 3 || err != nil {
  24. log.Fatalf("Bad input: %s", line)
  25. }
  26. boxes = append(boxes, box{x: x, y: y, z: z, id: id})
  27. id++
  28. }
  29. return boxes
  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. boxes := readInput(file)
  41. fmt.Println(boxes)
  42. }