Skip to content

Commit 5c88cf5

Browse files
committed
Day 11
1 parent 2ffebf6 commit 5c88cf5

File tree

4 files changed

+196
-0
lines changed

4 files changed

+196
-0
lines changed

day11/input.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
6111821767
2+
1763611615
3+
3512683131
4+
8582771473
5+
8214813874
6+
2325823217
7+
2222482823
8+
5471356782
9+
3738671287
10+
8675226574

day11/main.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"strconv"
8+
)
9+
10+
func main() {
11+
fmt.Printf("Part 1: %d\n", numFlashes("input.txt", 100))
12+
fmt.Printf("Part 2: %d\n", allFlash("input.txt"))
13+
}
14+
15+
type Coord struct {
16+
i, j int
17+
}
18+
19+
type Octopi struct {
20+
grid [][]int
21+
flashes int
22+
}
23+
24+
// allFlash returns the step at which all octopi flash
25+
func allFlash(path string) int {
26+
octopi := parseOctopi(path)
27+
prev := octopi.flashes
28+
29+
for i := 0; ; i++ {
30+
octopi.step()
31+
if octopi.flashes-prev == 100 {
32+
return i + 1
33+
} else {
34+
prev = octopi.flashes
35+
}
36+
}
37+
}
38+
39+
// numFlashes returns the number of flashes for a given octopi grid after some steps
40+
func numFlashes(path string, steps int) int {
41+
octopi := parseOctopi(path)
42+
43+
for i := 0; i < steps; i++ {
44+
octopi.step()
45+
}
46+
47+
return octopi.flashes
48+
}
49+
50+
func (o *Octopi) step() {
51+
// Increment each octopus in the grid by 1
52+
for i := range o.grid {
53+
for j := range o.grid[i] {
54+
o.grid[i][j]++
55+
}
56+
}
57+
58+
// For each octopus, if it's level is > 9, increment its neighbors
59+
// up/down/diagonally by 1, and continue until the grid is stable
60+
prev := 0
61+
flashed := map[Coord]bool{}
62+
for {
63+
for i := range o.grid {
64+
for j := range o.grid[i] {
65+
sweep := []Coord{
66+
{i - 1, j - 1},
67+
{i - 1, j},
68+
{i - 1, j + 1},
69+
{i, j - 1},
70+
{i, j + 1},
71+
{i + 1, j - 1},
72+
{i + 1, j},
73+
{i + 1, j + 1},
74+
}
75+
if o.grid[i][j] > 9 {
76+
if _, ok := flashed[Coord{i, j}]; !ok {
77+
for _, coord := range sweep {
78+
if coord.i >= 0 && coord.i < len(o.grid) && coord.j >= 0 && coord.j < len(o.grid[i]) {
79+
o.grid[coord.i][coord.j]++
80+
}
81+
}
82+
flashed[Coord{i, j}] = true
83+
}
84+
}
85+
}
86+
}
87+
if prev == len(flashed) {
88+
break
89+
} else {
90+
prev = len(flashed)
91+
}
92+
}
93+
94+
// For each octopus, if its level is > 9, it flashes and its level becomes 0
95+
for i := range o.grid {
96+
for j := range o.grid[i] {
97+
if o.grid[i][j] > 9 {
98+
o.grid[i][j] = 0
99+
o.flashes++
100+
}
101+
}
102+
}
103+
}
104+
105+
func parseOctopi(path string) *Octopi {
106+
var octopi [][]int
107+
108+
file, err := os.Open(path)
109+
if err != nil {
110+
panic(err)
111+
}
112+
defer file.Close()
113+
114+
scanner := bufio.NewScanner(file)
115+
for scanner.Scan() {
116+
var line []int
117+
for _, c := range scanner.Text() {
118+
level, err := strconv.Atoi(string(c))
119+
if err != nil {
120+
panic(err)
121+
}
122+
line = append(line, level)
123+
}
124+
octopi = append(octopi, line)
125+
}
126+
127+
return &Octopi{grid: octopi, flashes: 0}
128+
}

day11/main_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import "testing"
4+
5+
func TestNumFlashes(t *testing.T) {
6+
tests := []struct {
7+
input string
8+
steps int
9+
expected int
10+
}{
11+
{
12+
input: "test.txt",
13+
steps: 10,
14+
expected: 204,
15+
},
16+
{
17+
input: "test.txt",
18+
steps: 100,
19+
expected: 1656,
20+
},
21+
}
22+
23+
for _, test := range tests {
24+
actual := numFlashes(test.input, test.steps)
25+
if actual != test.expected {
26+
t.Errorf("Expected %d, but got %d", test.expected, actual)
27+
}
28+
}
29+
}
30+
31+
func TestAllFlash(t *testing.T) {
32+
tests := []struct {
33+
input string
34+
expected int
35+
}{
36+
{
37+
input: "test.txt",
38+
expected: 195,
39+
},
40+
}
41+
42+
for _, test := range tests {
43+
actual := allFlash(test.input)
44+
if actual != test.expected {
45+
t.Errorf("Expected %d, but got %d", test.expected, actual)
46+
}
47+
}
48+
}

day11/test.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
5483143223
2+
2745854711
3+
5264556173
4+
6141336146
5+
6357385478
6+
4167524645
7+
2176841721
8+
6882881134
9+
4846848554
10+
5283751526

0 commit comments

Comments
 (0)