-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (103 loc) · 2.1 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"fmt"
"os"
"github.com/fbegyn/aoc2021/go/helpers"
)
func main() {
file := os.Args[1]
lines := helpers.InputToLines(file)
energyLevels := [100]int{}
for y, line := range lines {
for x, r := range line {
energyLevels[y*10+x] = int(r - '0')
}
}
levels2 := [100]int{}
copy(levels2[:], energyLevels[:])
part1 := 0
run(energyLevels[:], func(step, flashes int) bool {
part1 += flashes
return step == 99
})
var part2 int
run(levels2[:], func(step, flashes int) bool {
if flashes == 100 {
part2 = step + 1
return true
}
return false
})
fmt.Printf("solution for part 1: %d\n", part1)
fmt.Printf("solution for part 2: %d\n", part2)
}
var coord = [8][2]int{
{0, 1},
{1, 0},
{0, -1},
{-1, 0},
{1, 1},
{-1, -1},
{1, -1},
{-1, 1},
}
func run(energyLevels []int, stop func(step, flashes int) bool) int {
for i := 0; ; i++ {
// increase energy levels by 1
for y := 0; y < 10; y++ {
for x := 0; x < 10; x++ {
energyLevels[y*10+x]++
}
}
// levels > 9 flash
flashes := 0
for y := 0; y < 10; y++ {
for x := 0; x < 10; x++ {
if energyLevels[y*10+x] > 9 {
flashes += flash(energyLevels, x, y)
}
}
}
// all levels that flashes reset to 0
for y := 0; y < 10; y++ {
for x := 0; x < 10; x++ {
if energyLevels[y*10+x] <= -1 {
energyLevels[y*10+x] = 0
}
}
}
if stop(i, flashes) {
return flashes
}
}
}
func flash(levels []int, x, y int) int {
// if already flashed, return 0
if levels[y*10+x] == -1 {
return 0
}
// flash the current octopus
flashes := 1
levels[y*10+x] = -1
for _, sel := range coord {
xsel, ysel := x+sel[0], y+sel[1]
// if we go out of range, skip
if xsel < 0 || xsel >= 10 || ysel < 0 || ysel >= 10 {
continue
}
// if the selected octopus already flashed, skip
if levels[ysel*10+xsel] == -1 {
continue
}
// increase the level of the octopus by 1
levels[ysel*10+xsel]++
// if the octopus energy level goes > 9, flash it as well
if levels[ysel*10+xsel] > 9 {
flashes += flash(levels, xsel, ysel)
}
}
return flashes
}
func Part2() int {
return 0
}