~talos/advent-of-code

001b80e6698350b76d627be4b1ab524faab7db53 — Jordan Newport 11 months ago 68cd523
Day 11
4 files changed, 180 insertions(+), 0 deletions(-)

A 2021/11/input
A 2021/11/input2
A 2021/11/input3
A 2021/11/solution.go
A 2021/11/input => 2021/11/input +10 -0
@@ 0,0 1,10 @@
4871252763
8533428173
7182186813
2128441541
3722272272
8751683443
3135571153
5816321572
2651347271
7788154252

A 2021/11/input2 => 2021/11/input2 +10 -0
@@ 0,0 1,10 @@
5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526

A 2021/11/input3 => 2021/11/input3 +5 -0
@@ 0,0 1,5 @@
11111
19991
19191
19991
11111

A 2021/11/solution.go => 2021/11/solution.go +155 -0
@@ 0,0 1,155 @@
package main

import (
	"bufio"
	"fmt"
	// "math"
	"os"
	// "sort"
	// "strconv"
	// "strings"
)

type Octopus struct {
	energy int
	flashed bool
}

func get_input() []string {
	input_file := ""
	if len(os.Args) >= 2 {
		input_file = os.Args[1]
	} else {
		input_file = "input"
	}
	file, err := os.Open(input_file)
	if err != nil {
		panic(err)
	}
	defer file.Close()

	strs := make([]string, 0)
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		value := scanner.Text()
		if err != nil {
			panic(err)
		}
		strs = append(strs, value)
	}
	if err :=scanner.Err(); err != nil {
		panic(err)
	}
	return strs
}

func main() {
	strs := get_input()
	nums := make([][]int, len(strs) + 2)
	flashed := make([][]bool, len(strs) + 2)

	nums[0] = make([]int, len(strs[0]) + 2)
	for i, _ := range(nums[0]) {
		nums[0][i] = -100000
	}
	nums[len(strs)+1] = make([]int, len(strs[0]) + 2)
	for i, _ := range(nums[len(strs)+1]) {
		nums[len(strs)+1][i] = -100000
	}

	for i, str := range strs {
		nums[i+1] = make([]int, len(str) + 2)
		nums[i+1][0] = -100000
		nums[i+1][len(str)+1] = -100000
		for j, r := range str {
			nums[i+1][j+1] = int(r - '0')
		}
	}

	for i, line := range nums {
		flashed[i] = make([]bool, len(line))
		for j, _ := range line {
			flashed[i][j] = false
		}
	}

	flashes := 0
	/*
	// steps
	for step := 0; step < 100; step++ {
		for i := 1; i <= len(strs); i++ {
			for j := 1; j <= len(strs[0]); j++ {
				nums[i][j] += 1
				flashed[i][j] = false
			}
		}

		for i := 1; i <= len(strs); i++ {
			for j := 1; j <= len(strs[0]); j++ {
				if nums[i][j] > 9 && !flashed[i][j] {
					nums, flashed = flash(nums, i, j, flashed, &flashes)
				}
			}
		}

		for i := 1; i <= len(strs); i++ {
			for j := 1; j <= len(strs[0]); j++ {
				if flashed[i][j] {
					nums[i][j] = 0
				}
			}
		}
	}
	*/

	// steps
	for step := 1; step < 10000000; step++ {
		stepflashes := flashes
		for i := 1; i <= len(strs); i++ {
			for j := 1; j <= len(strs[0]); j++ {
				nums[i][j] += 1
				flashed[i][j] = false
			}
		}

		for i := 1; i <= len(strs); i++ {
			for j := 1; j <= len(strs[0]); j++ {
				if nums[i][j] > 9 && !flashed[i][j] {
					nums, flashed = flash(nums, i, j, flashed, &flashes)
				}
			}
		}

		for i := 1; i <= len(strs); i++ {
			for j := 1; j <= len(strs[0]); j++ {
				if flashed[i][j] {
					nums[i][j] = 0
				}
			}
		}
		if flashes - stepflashes == 100 {
			fmt.Println("step", step)
			break
		}
	}

	for _, line := range nums {
		fmt.Println(line)
	}

	fmt.Println(flashes)
}

func flash(nums [][]int, i, j int, flashed [][]bool, flashes *int) ([][]int, [][]bool) {
	*flashes++
	flashed[i][j] = true
	for k := i - 1; k <= i + 1; k++ {
		for l := j - 1; l <= j + 1; l++ {
			nums[k][l]++
			if nums[k][l] > 9 && !flashed[k][l] {
				nums, flashed = flash(nums, k, l, flashed, flashes)
			}
		}
	}
	return nums, flashed
}