code.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. type cube struct {
  69. x, y, z int
  70. }
  71. func countOn(cubes map[cube]bool) int {
  72. count := 0
  73. for _, on := range cubes {
  74. if on {
  75. count++
  76. }
  77. }
  78. return count
  79. }
  80. func part1(input []action) int {
  81. cubes := make(map[cube]bool)
  82. for _, action := range input {
  83. startX := action.x[0]
  84. if startX < -50 {
  85. startX = -50
  86. }
  87. endX := action.x[1]
  88. if endX > 50 {
  89. endX = 50
  90. }
  91. for x := startX; x <= endX; x++ {
  92. startY := action.y[0]
  93. if startY < -50 {
  94. startY = -50
  95. }
  96. endY := action.y[1]
  97. if endY > 50 {
  98. endY = 50
  99. }
  100. for y := startY; y <= endY; y++ {
  101. startZ := action.z[0]
  102. if startZ < -50 {
  103. startZ = -50
  104. }
  105. endZ := action.z[1]
  106. if endZ > 50 {
  107. endZ = 50
  108. }
  109. for z := startZ; z <= endZ; z++ {
  110. cubes[cube{x, y, z}] = action.on
  111. }
  112. }
  113. }
  114. }
  115. return countOn(cubes)
  116. }
  117. func main() {
  118. if len(os.Args) < 2 {
  119. log.Fatal("Please provide a file name as argument")
  120. }
  121. input := readInput(os.Args[1])
  122. fmt.Println("Part1:", part1(input))
  123. }