New Shared Library

This commit is contained in:
2021-12-16 21:11:39 +00:00
parent 1a2247044a
commit 6111f86a04
24 changed files with 559 additions and 392 deletions

View File

@ -1,6 +1,7 @@
package main
import (
"AdventOfCode2021/shared"
"bufio"
"fmt"
"os"
@ -11,7 +12,7 @@ func main() {
content := returnContent("../input")
//content := returnContent("../testInput")
octopuses := map[Coordinate]int{}
octopuses := map[shared.Coordinate]int{}
answer := 0
@ -22,12 +23,12 @@ func main() {
for y, row := range *content {
width = len(row)
for x, val := range row {
octopuses[Coordinate{X: x, Y: y}] = val
octopuses[shared.Coordinate{X: x, Y: y}] = val
}
}
for i := 0; i < 100; i++ {
flashers := map[Coordinate]bool{}
flashers := map[shared.Coordinate]bool{}
for coord := range octopuses {
octopuses[coord]++
@ -63,49 +64,6 @@ func main() {
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

View File

@ -1,6 +1,7 @@
package main
import (
"AdventOfCode2021/shared"
"bufio"
"fmt"
"os"
@ -11,7 +12,7 @@ func main() {
content := returnContent("../input")
//content := returnContent("../testInput")
octopuses := map[Coordinate]int{}
octopuses := map[shared.Coordinate]int{}
answer := 0
@ -22,14 +23,14 @@ func main() {
for y, row := range *content {
width = len(row)
for x, val := range row {
octopuses[Coordinate{X: x, Y: y}] = val
octopuses[shared.Coordinate{X: x, Y: y}] = val
}
}
i := 0
for {
i++
flashers := map[Coordinate]bool{}
flashers := map[shared.Coordinate]bool{}
for coords, energy := range octopuses {
octopuses[coords] = energy + 1
}
@ -61,7 +62,7 @@ func main() {
allFlashed := true
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
if !flashers[Coordinate{
if !flashers[shared.Coordinate{
X: x,
Y: y,
}] {
@ -79,49 +80,6 @@ func main() {
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