code.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. type octo struct {
  11. value int
  12. flashed bool
  13. }
  14. func readInput(file string) [][]octo {
  15. content, err := ioutil.ReadFile(file)
  16. if err != nil {
  17. log.Fatal(err)
  18. }
  19. lines := strings.Split(string(content), "\n")
  20. var input [][]octo
  21. for _, line := range lines {
  22. if line == "" {
  23. continue
  24. }
  25. var row []octo
  26. for _, char := range line {
  27. if number, err := strconv.Atoi(string(char)); err == nil {
  28. row = append(row, octo{number, false})
  29. } else {
  30. log.Fatal(err)
  31. }
  32. }
  33. input = append(input, row)
  34. }
  35. return input
  36. }
  37. func doNeighbors(input [][]octo, x, y int) int {
  38. var neighbors int
  39. for i := -1; i <= 1; i++ {
  40. for j := -1; j <= 1; j++ {
  41. if i == 0 && j == 0 {
  42. continue
  43. }
  44. if x+i < 0 || x+i >= len(input[y]) {
  45. continue
  46. }
  47. if y+j < 0 || y+j >= len(input) {
  48. continue
  49. }
  50. if !input[y+j][x+i].flashed {
  51. input[y+j][x+i].value++
  52. if input[y+j][x+i].value > 9 {
  53. input[y+j][x+i].value = 0
  54. input[y+j][x+i].flashed = true
  55. neighbors++
  56. neighbors += doNeighbors(input, x+i, y+j)
  57. }
  58. }
  59. }
  60. }
  61. return neighbors
  62. }
  63. func reset(input [][]octo) {
  64. for y, row := range input {
  65. for x, _ := range row {
  66. if input[y][x].flashed {
  67. input[y][x].value = 0
  68. input[y][x].flashed = false
  69. }
  70. }
  71. }
  72. }
  73. func part1(input [][]octo) int {
  74. var flashed int
  75. for i := 0; i < 100; i++ {
  76. for y, row := range input {
  77. for x, _ := range row {
  78. if input[y][x].flashed {
  79. continue
  80. }
  81. input[y][x].value++
  82. if input[y][x].value > 9 {
  83. input[y][x].value = 0
  84. input[y][x].flashed = true
  85. flashed++
  86. flashed += doNeighbors(input, x, y)
  87. }
  88. }
  89. }
  90. reset(input)
  91. }
  92. return flashed
  93. }
  94. func allFlashed(input [][]octo) bool {
  95. for _, row := range input {
  96. for _, octo := range row {
  97. if !octo.flashed {
  98. return false
  99. }
  100. }
  101. }
  102. return true
  103. }
  104. func part2(input [][]octo) int {
  105. var flashed int
  106. count := 101
  107. for {
  108. for y, row := range input {
  109. for x, _ := range row {
  110. if input[y][x].flashed {
  111. continue
  112. }
  113. input[y][x].value++
  114. if input[y][x].value > 9 {
  115. input[y][x].value = 0
  116. input[y][x].flashed = true
  117. flashed++
  118. flashed += doNeighbors(input, x, y)
  119. }
  120. }
  121. }
  122. if allFlashed(input) {
  123. break
  124. }
  125. count++
  126. reset(input)
  127. }
  128. return count
  129. }
  130. func main() {
  131. if len(os.Args) < 2 {
  132. log.Fatal("Please provide a file name as argument")
  133. }
  134. input := readInput(os.Args[1])
  135. fmt.Println("Part1:", part1(input))
  136. fmt.Println("Part2:", part2(input))
  137. }