aboutsummaryrefslogtreecommitdiff
path: root/2021
diff options
context:
space:
mode:
authorJulien Dessaux2021-12-11 16:34:05 +0100
committerJulien Dessaux2021-12-11 16:34:05 +0100
commit26b9d8e224d45831adbb859b2dd31ca0c0f9218e (patch)
tree3dfe337faa18c4dcefc3a1c008e58c56ac8c220c /2021
parentAdded solutions for 10th day : syntax scoring (diff)
downloadadvent-of-code-26b9d8e224d45831adbb859b2dd31ca0c0f9218e.tar.gz
advent-of-code-26b9d8e224d45831adbb859b2dd31ca0c0f9218e.tar.bz2
advent-of-code-26b9d8e224d45831adbb859b2dd31ca0c0f9218e.zip
Added solutions for 11th day: octopus flashes
Diffstat (limited to '2021')
-rw-r--r--2021/11/example10
-rw-r--r--2021/11/first.go57
-rw-r--r--2021/11/input10
-rw-r--r--2021/11/second.go63
4 files changed, 140 insertions, 0 deletions
diff --git a/2021/11/example b/2021/11/example
new file mode 100644
index 0000000..03743f6
--- /dev/null
+++ b/2021/11/example
@@ -0,0 +1,10 @@
+5483143223
+2745854711
+5264556173
+6141336146
+6357385478
+4167524645
+2176841721
+6882881134
+4846848554
+5283751526
diff --git a/2021/11/first.go b/2021/11/first.go
new file mode 100644
index 0000000..28428e4
--- /dev/null
+++ b/2021/11/first.go
@@ -0,0 +1,57 @@
+package main
+
+import (
+ "bufio"
+ "fmt"
+ "os"
+)
+
+var (
+ grid = make([][]byte, 0)
+ score = 0
+)
+
+func powerup(x, y int) {
+ if x < 0 || x > 9 || y < 0 || y > 9 || grid[x][y] > 9 {
+ return
+ }
+ grid[x][y]++
+ if grid[x][y] > 9 {
+ score++
+ powerup(x-1, y-1)
+ powerup(x, y-1)
+ powerup(x+1, y-1)
+ powerup(x-1, y)
+ powerup(x+1, y)
+ powerup(x-1, y+1)
+ powerup(x, y+1)
+ powerup(x+1, y+1)
+ }
+}
+func main() {
+ scanner := bufio.NewScanner(os.Stdin)
+ for scanner.Scan() {
+ line := scanner.Text()
+ l := make([]byte, len(line))
+ for i := 0; i < len(line); i++ {
+ l[i] = line[i] - '0'
+ }
+ grid = append(grid, l)
+ }
+
+ for i := 0; i < 100; i++ {
+ for y := 0; y < 10; y++ {
+ for x := 0; x < 10; x++ {
+ powerup(x, y)
+ }
+ }
+ for y := 0; y < 10; y++ {
+ for x := 0; x < 10; x++ {
+ if grid[x][y] > 9 {
+ grid[x][y] = 0
+ }
+ }
+ }
+ }
+ fmt.Println(score)
+}
diff --git a/2021/11/input b/2021/11/input
new file mode 100644
index 0000000..e3a6f6b
--- /dev/null
+++ b/2021/11/input
@@ -0,0 +1,10 @@
+8448854321
+4447645251
+6542573645
+4725275268
+6442514153
+4515734868
+5513676158
+3257376185
+2172424467
+6775163586
diff --git a/2021/11/second.go b/2021/11/second.go
new file mode 100644
index 0000000..34e9971
--- /dev/null
+++ b/2021/11/second.go
@@ -0,0 +1,63 @@
+package main
+
+import (
+ "bufio"
+ "fmt"
+ "os"
+)
+
+var (
+ grid = make([][]byte, 0)
+ score = 0
+)
+
+func powerup(x, y int) {
+ if x < 0 || x > 9 || y < 0 || y > 9 || grid[x][y] > 9 {
+ return
+ }
+ grid[x][y]++
+ if grid[x][y] > 9 {
+ score++
+ powerup(x-1, y-1)
+ powerup(x, y-1)
+ powerup(x+1, y-1)
+ powerup(x-1, y)
+ powerup(x+1, y)
+ powerup(x-1, y+1)
+ powerup(x, y+1)
+ powerup(x+1, y+1)
+ }
+}
+func main() {
+ scanner := bufio.NewScanner(os.Stdin)
+ for scanner.Scan() {
+ line := scanner.Text()
+ l := make([]byte, len(line))
+ for i := 0; i < len(line); i++ {
+ l[i] = line[i] - '0'
+ }
+ grid = append(grid, l)
+ }
+
+ i := 0
+ for {
+ score = 0
+ for y := 0; y < 10; y++ {
+ for x := 0; x < 10; x++ {
+ powerup(x, y)
+ }
+ }
+ for y := 0; y < 10; y++ {
+ for x := 0; x < 10; x++ {
+ if grid[x][y] > 9 {
+ grid[x][y] = 0
+ }
+ }
+ }
+ i++
+ if score == 100 {
+ break
+ }
+ }
+ fmt.Println(i)
+}