d11
This commit is contained in:
parent
72762f3d08
commit
b196009d5a
5 changed files with 159 additions and 0 deletions
56
d11/common.go
Normal file
56
d11/common.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package d11
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type octopus struct {
|
||||
energy int
|
||||
flashed bool
|
||||
}
|
||||
|
||||
type grid [][]octopus
|
||||
|
||||
func createGrid(in string) grid {
|
||||
lines := strings.Split(strings.TrimSpace(in), "\n")
|
||||
grid := make(grid, len(lines))
|
||||
|
||||
for i, line := range lines {
|
||||
energies := strings.Split(line, "")
|
||||
grid[i] = make([]octopus, len(energies))
|
||||
for j, energy := range energies {
|
||||
num, _ := strconv.Atoi(energy)
|
||||
grid[i][j] = octopus{num, false}
|
||||
}
|
||||
}
|
||||
|
||||
return grid
|
||||
}
|
||||
|
||||
func incr(x, y int, grid grid) int {
|
||||
if x < 0 || x >= len(grid[0]) || y < 0 || y >= len(grid) {
|
||||
return 0
|
||||
}
|
||||
|
||||
if grid[y][x].flashed {
|
||||
return 0
|
||||
}
|
||||
|
||||
grid[y][x].energy++
|
||||
|
||||
if grid[y][x].energy == 10 {
|
||||
grid[y][x].energy = 0
|
||||
grid[y][x].flashed = true
|
||||
return incr(x-1, y-1, grid) +
|
||||
incr(x, y-1, grid) +
|
||||
incr(x+1, y-1, grid) +
|
||||
incr(x-1, y, grid) +
|
||||
incr(x+1, y, grid) +
|
||||
incr(x-1, y+1, grid) +
|
||||
incr(x, y+1, grid) +
|
||||
incr(x+1, y+1, grid) + 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
37
d11/d11_test.go
Normal file
37
d11/d11_test.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package d11
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestP1(t *testing.T) {
|
||||
input := `5483143223
|
||||
2745854711
|
||||
5264556173
|
||||
6141336146
|
||||
6357385478
|
||||
4167524645
|
||||
2176841721
|
||||
6882881134
|
||||
4846848554
|
||||
5283751526`
|
||||
|
||||
assert.EqualValues(t, 1656, P1(input))
|
||||
}
|
||||
|
||||
func TestP2(t *testing.T) {
|
||||
input := `5483143223
|
||||
2745854711
|
||||
5264556173
|
||||
6141336146
|
||||
6357385478
|
||||
4167524645
|
||||
2176841721
|
||||
6882881134
|
||||
4846848554
|
||||
5283751526`
|
||||
|
||||
assert.EqualValues(t, 195, P2(input))
|
||||
}
|
28
d11/p1.go
Normal file
28
d11/p1.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package d11
|
||||
|
||||
func reset(grid grid) {
|
||||
for y, line := range grid {
|
||||
for x := range line {
|
||||
grid[y][x].flashed = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func flash(grid grid) int {
|
||||
lights := 0
|
||||
for i := 0; i < 100; i++ {
|
||||
for y, line := range grid {
|
||||
for x := range line {
|
||||
lights += incr(x, y, grid)
|
||||
}
|
||||
}
|
||||
reset(grid)
|
||||
}
|
||||
|
||||
return lights
|
||||
}
|
||||
|
||||
func P1(in string) int {
|
||||
grid := createGrid(in)
|
||||
return flash(grid)
|
||||
}
|
35
d11/p2.go
Normal file
35
d11/p2.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package d11
|
||||
|
||||
func isAllFlashed(grid grid) bool {
|
||||
|
||||
for y, line := range grid {
|
||||
for x := range line {
|
||||
if !grid[y][x].flashed {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func flash2(grid grid) int {
|
||||
step := 0
|
||||
for {
|
||||
for y, line := range grid {
|
||||
for x := range line {
|
||||
incr(x, y, grid)
|
||||
}
|
||||
}
|
||||
step++
|
||||
if isAllFlashed(grid) {
|
||||
return step
|
||||
}
|
||||
reset(grid)
|
||||
}
|
||||
}
|
||||
|
||||
func P2(in string) int {
|
||||
grid := createGrid(in)
|
||||
return flash2(grid)
|
||||
}
|
3
main.go
3
main.go
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/fotonmoton/aoc2021/client"
|
||||
"github.com/fotonmoton/aoc2021/d1"
|
||||
"github.com/fotonmoton/aoc2021/d10"
|
||||
"github.com/fotonmoton/aoc2021/d11"
|
||||
"github.com/fotonmoton/aoc2021/d2"
|
||||
"github.com/fotonmoton/aoc2021/d3"
|
||||
"github.com/fotonmoton/aoc2021/d4"
|
||||
|
@ -49,4 +50,6 @@ func main() {
|
|||
fmt.Printf("day 9 part 2: %v\n", d9.P2(client.Day(9)))
|
||||
fmt.Printf("day 10: %v\n", d10.P1(client.Day(10)))
|
||||
fmt.Printf("day 10 part 2: %v\n", d10.P2(client.Day(10)))
|
||||
fmt.Printf("day 11: %v\n", d11.P1(client.Day(11)))
|
||||
fmt.Printf("day 11 part 2: %v\n", d11.P2(client.Day(11)))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue