-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.go
73 lines (62 loc) · 1.37 KB
/
day11.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"strconv"
)
func calcFlashes(lines []string) (int, int) {
octos := extractOctos(lines)
var flashes int = 0
var steps int = 0
var allGood bool = false
for ; !allGood; steps++ {
for column := range octos {
for row := range octos[column] {
increaseOcto(octos, column, row)
}
}
allGood = true
for column := range octos {
for row := range octos[column] {
if octos[column][row] == -1 {
octos[column][row] = 0
if steps < 100 {
flashes++
}
} else {
allGood = false
}
}
}
}
return flashes, steps
}
func increaseOcto(octos [][]int, row, column int) {
if row < 0 || row == len(octos) || column < 0 || column == len(octos[row]) {
return
}
switch octos[column][row] {
case -1:
return
case 9:
octos[column][row] = -1
increaseOcto(octos, row-1, column-1)
increaseOcto(octos, row-1, column)
increaseOcto(octos, row-1, column+1)
increaseOcto(octos, row, column-1)
increaseOcto(octos, row, column+1)
increaseOcto(octos, row+1, column)
increaseOcto(octos, row+1, column+1)
increaseOcto(octos, row+1, column-1)
default:
octos[column][row]++
}
}
func extractOctos(lines []string) [][]int {
octos := make([][]int, len(lines))
for i, v := range lines {
for j := 0; j < len(v); j++ {
octo, _ := strconv.Atoi(string(v[j]))
octos[i] = append(octos[i], octo)
}
}
return octos
}