code.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "sort"
  8. )
  9. type Object struct {
  10. xMin, yMin, zMin int
  11. xMax, yMax, zMax int
  12. zRealMin, zRealMax int
  13. supports []*Object
  14. standsOn []*Object
  15. canBeDeleted bool
  16. }
  17. func readInput(file *os.File) []Object {
  18. scanner := bufio.NewScanner(file)
  19. var objects []Object
  20. for scanner.Scan() {
  21. line := scanner.Text()
  22. if line == "" {
  23. break
  24. }
  25. var object Object
  26. n, err := fmt.Sscanf(line, "%d,%d,%d~%d,%d,%d", &object.xMin, &object.yMin, &object.zMin, &object.xMax, &object.yMax, &object.zMax)
  27. if n != 6 || err != nil {
  28. log.Fatalf("Bad input: %s\n%s", line, err)
  29. }
  30. objects = append(objects, object)
  31. }
  32. return objects
  33. }
  34. func intersect(a, b Object) bool {
  35. return a.xMin <= b.xMax && a.xMax >= b.xMin && a.yMin <= b.yMax && a.yMax >= b.yMin
  36. }
  37. func process(objects []Object) {
  38. sort.Slice(objects, func(i, j int) bool { return objects[i].zMin < objects[j].zMin })
  39. endsAt := make(map[int][]*Object)
  40. for i := range objects {
  41. stable := false
  42. for z := objects[i].zMin - 1; z > 0; z-- {
  43. below := endsAt[z]
  44. for j := range below {
  45. if intersect(objects[i], *below[j]) {
  46. objects[i].zRealMin = z + 1
  47. objects[i].zRealMax = objects[i].zRealMin + (objects[i].zMax - objects[i].zMin)
  48. objects[i].standsOn = append(objects[i].standsOn, below[j])
  49. below[j].supports = append(below[j].supports, &objects[i])
  50. if !stable {
  51. endsAt[objects[i].zRealMax] = append(endsAt[objects[i].zRealMax], &objects[i])
  52. }
  53. stable = true
  54. }
  55. }
  56. if stable {
  57. break
  58. }
  59. }
  60. if !stable {
  61. objects[i].zRealMin = 1
  62. objects[i].zRealMax = objects[i].zRealMin + (objects[i].zMax - objects[i].zMin)
  63. endsAt[objects[i].zRealMax] = append(endsAt[objects[i].zRealMax], &objects[i])
  64. }
  65. }
  66. }
  67. func part1(objects []Object) int {
  68. var result int
  69. process(objects)
  70. for i := range objects {
  71. if len(objects[i].supports) == 0 {
  72. objects[i].canBeDeleted = true
  73. result++
  74. continue
  75. }
  76. canBeDeleted := true
  77. for j := range objects[i].supports {
  78. if len(objects[i].supports[j].standsOn) < 2 {
  79. canBeDeleted = false
  80. break
  81. }
  82. }
  83. if canBeDeleted {
  84. objects[i].canBeDeleted = true
  85. result++
  86. }
  87. }
  88. return result
  89. }
  90. func countFallen(object *Object, fallen map[*Object]bool) int {
  91. var count int
  92. if !fallen[object] {
  93. fallen[object] = true
  94. count++
  95. }
  96. for i := range object.supports {
  97. count += countFallen(object.supports[i], fallen)
  98. }
  99. return count
  100. }
  101. func part2(objects []Object) int {
  102. var result int
  103. for i := range objects {
  104. if objects[i].canBeDeleted {
  105. continue
  106. }
  107. fallen := make(map[*Object]bool)
  108. for j := range objects[i].supports {
  109. result += countFallen(objects[i].supports[j], fallen)
  110. }
  111. }
  112. return result
  113. }
  114. func main() {
  115. if len(os.Args) < 2 {
  116. log.Fatal("You need to specify a file!")
  117. }
  118. filePath := os.Args[1]
  119. file, err := os.Open(filePath)
  120. if err != nil {
  121. log.Fatalf("Failed to open %s!\n", filePath)
  122. }
  123. objects := readInput(file)
  124. fmt.Println("Part1:", part1(objects))
  125. fmt.Println("Part2:", part2(objects))
  126. }