Advertisement
Guest User

Untitled

a guest
Dec 17th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. func main() {
  2. lines := lib.ReadFileLines("input.txt")
  3. fmt.Println(step1(lines))
  4. fmt.Println(step2(lines))
  5. }
  6.  
  7. const (
  8. active = '#'
  9. inactive = '.'
  10. )
  11.  
  12. type point4 struct {
  13. W, X, Y, Z int64
  14. }
  15.  
  16. type grid map[point4]bool
  17.  
  18. func (g *grid) countActive() int64 {
  19. a := int64(0)
  20. for _, v := range *g {
  21. if v {
  22. a++
  23. }
  24. }
  25. return a
  26. }
  27.  
  28. // extremities returns minX, maxX, minY, maxY, minZ, maxZ, minW, maxW
  29. func (g *grid) extremities() (int64, int64, int64, int64, int64, int64, int64, int64) {
  30. minX, maxX, minY, maxY, minZ, maxZ, minW, maxW := int64(0), int64(0), int64(0), int64(0), int64(0), int64(0), int64(0), int64(0)
  31. for k, v := range *g {
  32. if v {
  33. minX = lib.Min(minX, k.X)
  34. maxX = lib.Max(maxX, k.X)
  35. minY = lib.Min(minY, k.Y)
  36. maxY = lib.Max(maxY, k.Y)
  37. minZ = lib.Min(minZ, k.Z)
  38. maxZ = lib.Max(maxZ, k.Z)
  39. minW = lib.Min(minW, k.W)
  40. maxW = lib.Max(maxW, k.W)
  41. }
  42. }
  43. return minX, maxX, minY, maxY, minZ, maxZ, minW, maxW
  44. }
  45.  
  46. func (g *grid) neighbors(pt *point4) int {
  47. neighbors := 0
  48. for x := pt.X - 1; x <= pt.X+1; x++ {
  49. for y := pt.Y - 1; y <= pt.Y+1; y++ {
  50. for z := pt.Z - 1; z <= pt.Z+1; z++ {
  51. for w := pt.W - 1; w <= pt.W+1; w++ {
  52. if (*g)[point4{X: x, Y: y, Z: z, W: w}] {
  53. neighbors++
  54. }
  55. }
  56. }
  57. }
  58. }
  59. if (*g)[*pt] {
  60. neighbors--
  61. }
  62. return neighbors
  63. }
  64.  
  65. func initGrid(lines []string) grid {
  66. g := make(grid)
  67. for y, l := range lines {
  68. for x, c := range []rune(l) {
  69. g[point4{X: int64(x), Y: int64(y), Z: 0, W: 0}] = (c == active)
  70. }
  71. }
  72. return g
  73. }
  74.  
  75. func advance3(old grid) grid {
  76. g := make(grid)
  77. minX, maxX, minY, maxY, minZ, maxZ, _, _ := old.extremities()
  78.  
  79. for x := minX - 1; x <= maxX+1; x++ {
  80. for y := minY - 1; y <= maxY+1; y++ {
  81. for z := minZ - 1; z <= maxZ+1; z++ {
  82. here := point4{X: x, Y: y, Z: z, W: 0}
  83. neighbors := old.neighbors(&here)
  84. if old[here] {
  85. g[here] = neighbors == 2 || neighbors == 3
  86. } else {
  87. g[here] = neighbors == 3
  88. }
  89. }
  90. }
  91. }
  92. return g
  93. }
  94.  
  95. func advance4(old grid) grid {
  96. g := make(grid)
  97. minX, maxX, minY, maxY, minZ, maxZ, minW, maxW := old.extremities()
  98.  
  99. for x := minX - 1; x <= maxX+1; x++ {
  100. for y := minY - 1; y <= maxY+1; y++ {
  101. for z := minZ - 1; z <= maxZ+1; z++ {
  102. for w := minW - 1; w <= maxW+1; w++ {
  103. here := point4{X: x, Y: y, Z: z, W: w}
  104. neighbors := old.neighbors(&here)
  105. if old[here] {
  106. g[here] = neighbors == 2 || neighbors == 3
  107. } else {
  108. g[here] = neighbors == 3
  109. }
  110. }
  111. }
  112. }
  113. }
  114. return g
  115. }
  116.  
  117. func step1(lines []string) int64 {
  118. g := initGrid(lines)
  119. for i := 0; i < 6; i++ {
  120. g = advance3(g)
  121. }
  122. return g.countActive()
  123. }
  124.  
  125. func step2(lines []string) int64 {
  126. g := initGrid(lines)
  127. for i := 0; i < 6; i++ {
  128. g = advance4(g)
  129. }
  130. return g.countActive()
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement