From 37174b18db2c9c12a21703f3c982275a8a75ad28 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Sat, 11 Dec 2021 23:46:45 +0000 Subject: [PATCH] day 11a incomplete for some reason, the code just doesn't give the right answer --- day11/day 11a/go.mod | 3 + day11/day 11a/main.go | 132 ++++++++++++++++++++++++++++++++++++++++ day11/day 11a/testInput | 10 +++ day11/input | 10 +++ 4 files changed, 155 insertions(+) create mode 100644 day11/day 11a/go.mod create mode 100644 day11/day 11a/main.go create mode 100644 day11/day 11a/testInput create mode 100644 day11/input diff --git a/day11/day 11a/go.mod b/day11/day 11a/go.mod new file mode 100644 index 0000000..e938e2e --- /dev/null +++ b/day11/day 11a/go.mod @@ -0,0 +1,3 @@ +module PWD + +go 1.17 diff --git a/day11/day 11a/main.go b/day11/day 11a/main.go new file mode 100644 index 0000000..4ae6419 --- /dev/null +++ b/day11/day 11a/main.go @@ -0,0 +1,132 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" +) + +func main() { + content := returnContent("../input") + //content := returnContent("testInput") + + octopuses := map[Coordinate]int{} + + answer := 0 + + width := 0 + height := len(*content) + + //Insert octopuses into a map + for y, row := range *content { + width = len(row) + for x, val := range row { + octopuses[Coordinate{X: x, Y: y}] = val + } + } + + for i := 0; i < 100; i++ { + flashers := map[Coordinate]bool{} + + for coord := range octopuses { + octopuses[coord]++ + } + + for { + complete := true + + for coord, energy := range octopuses { + if energy > 9 && !flashers[coord] { + flashers[coord] = true + answer++ + for _, neighbor := range coord.Neighbours(width, height, true) { + octopuses[neighbor]++ + } + } + } + + if complete { + break + } + } + + for coord, flashed := range flashers { + if flashed { + octopuses[coord] = 0 + } + } + } + + fmt.Println(*content) + fmt.Println(answer) +} + +//Coordinate Class +type Coordinate struct { + X int + Y int +} + +func (c *Coordinate) Neighbours(gridWidth int, gridHeight int, diagonal bool) (out []Coordinate) { + spaceLeft := c.X > 0 + spaceRight := c.X < gridWidth-1 + spaceUp := c.Y > 0 + spaceDown := c.Y < gridHeight-1 + + if spaceLeft { + out = append(out, Coordinate{c.X - 1, c.Y}) + } + if spaceRight { + out = append(out, Coordinate{c.X + 1, c.Y}) + } + if spaceUp { + out = append(out, Coordinate{c.X, c.Y - 1}) + } + if spaceDown { + out = append(out, Coordinate{c.X, c.Y + 1}) + } + + if diagonal { + if spaceUp && spaceLeft { + out = append(out, Coordinate{c.X - 1, c.Y - 1}) + } + if spaceUp && spaceRight { + out = append(out, Coordinate{c.X + 1, c.Y - 1}) + } + if spaceDown && spaceLeft { + out = append(out, Coordinate{c.X - 1, c.Y + 1}) + } + if spaceDown && spaceRight { + out = append(out, Coordinate{c.X + 1, c.Y + 1}) + } + } + + return +} + +func returnContent(path string) *[][]int { + //read file and return it as an array of integers + + file, err := os.Open(path) + var content [][]int + + if err != nil { + fmt.Println("Unlucky, the file didn't open") + return &content + } + defer file.Close() + + scanner := bufio.NewScanner(file) + + for scanner.Scan() { + nums := []int{} + for _, char := range scanner.Text() { + num, _ := strconv.Atoi(string(char)) + nums = append(nums, num) + } + content = append(content, nums) + } + + return &content +} diff --git a/day11/day 11a/testInput b/day11/day 11a/testInput new file mode 100644 index 0000000..a3819c9 --- /dev/null +++ b/day11/day 11a/testInput @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 \ No newline at end of file diff --git a/day11/input b/day11/input new file mode 100644 index 0000000..67b8a5a --- /dev/null +++ b/day11/input @@ -0,0 +1,10 @@ +5421451741 +3877321568 +7583273864 +3451717778 +2651615156 +6377167526 +5182852831 +4766856676 +3437187583 +3633371586 \ No newline at end of file