From b196009d5a317806d9a6e668cd2cf1062bf8aa86 Mon Sep 17 00:00:00 2001 From: Gregory Date: Sun, 3 Apr 2022 22:38:47 +0300 Subject: [PATCH] d11 --- d11/common.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ d11/d11_test.go | 37 ++++++++++++++++++++++++++++++++ d11/p1.go | 28 +++++++++++++++++++++++++ d11/p2.go | 35 +++++++++++++++++++++++++++++++ main.go | 3 +++ 5 files changed, 159 insertions(+) create mode 100644 d11/common.go create mode 100644 d11/d11_test.go create mode 100644 d11/p1.go create mode 100644 d11/p2.go diff --git a/d11/common.go b/d11/common.go new file mode 100644 index 0000000..e185408 --- /dev/null +++ b/d11/common.go @@ -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 +} diff --git a/d11/d11_test.go b/d11/d11_test.go new file mode 100644 index 0000000..56691ac --- /dev/null +++ b/d11/d11_test.go @@ -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)) +} diff --git a/d11/p1.go b/d11/p1.go new file mode 100644 index 0000000..ffb3e64 --- /dev/null +++ b/d11/p1.go @@ -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) +} diff --git a/d11/p2.go b/d11/p2.go new file mode 100644 index 0000000..bd60fad --- /dev/null +++ b/d11/p2.go @@ -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) +} diff --git a/main.go b/main.go index 44a02ed..e0fe069 100644 --- a/main.go +++ b/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))) }