| 
 | 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 | +}  | 
0 commit comments