code.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. type action struct {
  11. on bool
  12. x []int
  13. y []int
  14. z []int
  15. }
  16. func readInput(file string) []action {
  17. content, err := ioutil.ReadFile(file)
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. lines := strings.Split(string(content), "\n")
  22. var input []action
  23. for _, line := range lines {
  24. if line == "" {
  25. continue
  26. }
  27. var a action
  28. if strings.HasPrefix(line, "on") {
  29. a.on = true
  30. }
  31. trash := strings.Split(line, " ")
  32. if len(trash) != 2 {
  33. log.Fatal("Invalid input")
  34. }
  35. parts := strings.Split(trash[1], ",")
  36. if len(parts) != 3 {
  37. log.Fatal("Invalid input")
  38. }
  39. for i, part := range parts {
  40. another := strings.Split(part, "=")
  41. if len(another) != 2 {
  42. log.Fatal("Invalid input")
  43. }
  44. numbers := strings.Split(another[1], "..")
  45. if len(numbers) != 2 {
  46. log.Fatal("Invalid input")
  47. }
  48. var first, second int
  49. if first, err = strconv.Atoi(numbers[0]); err != nil {
  50. log.Fatal(err)
  51. }
  52. if second, err = strconv.Atoi(numbers[1]); err != nil {
  53. log.Fatal(err)
  54. }
  55. switch i {
  56. case 0:
  57. a.x = []int{first, second}
  58. case 1:
  59. a.y = []int{first, second}
  60. case 2:
  61. a.z = []int{first, second}
  62. }
  63. }
  64. input = append(input, a)
  65. }
  66. return input
  67. }
  68. func main() {
  69. if len(os.Args) < 2 {
  70. log.Fatal("Please provide a file name as argument")
  71. }
  72. input := readInput(os.Args[1])
  73. fmt.Println(input)
  74. }