9
main.go
9
main.go
@@ -3,7 +3,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/thatguygriff/aoc2020/sixteen"
|
||||
"github.com/thatguygriff/aoc2020/seventeen"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -68,6 +68,9 @@ func main() {
|
||||
// fmt.Println(fifteen.PartTwo())
|
||||
|
||||
// Day 16
|
||||
fmt.Println(sixteen.PartOne())
|
||||
fmt.Println(sixteen.PartTwo())
|
||||
// fmt.Println(sixteen.PartOne())
|
||||
// fmt.Println(sixteen.PartTwo())
|
||||
|
||||
// Day 17
|
||||
fmt.Println(seventeen.PartOne())
|
||||
}
|
||||
|
148
seventeen/day_seventeen.go
Normal file
148
seventeen/day_seventeen.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package seventeen
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type pocket struct {
|
||||
dimension [][][]bool
|
||||
}
|
||||
|
||||
func (p *pocket) initialize(filename string) error {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
y := 0
|
||||
z := 0
|
||||
p.dimension = [][][]bool{}
|
||||
p.dimension = append(p.dimension, [][]bool{})
|
||||
for scanner.Scan() {
|
||||
p.dimension[z] = append(p.dimension[z], []bool{})
|
||||
for _, c := range scanner.Text() {
|
||||
switch string(c) {
|
||||
case "#":
|
||||
p.dimension[z][y] = append(p.dimension[z][y], true)
|
||||
case ".":
|
||||
p.dimension[z][y] = append(p.dimension[z][y], false)
|
||||
}
|
||||
}
|
||||
y++
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *pocket) active() int {
|
||||
count := 0
|
||||
|
||||
for z := 0; z < len(p.dimension); z++ {
|
||||
for y := 0; y < len(p.dimension[z]); y++ {
|
||||
for x := 0; x < len(p.dimension[z][y]); x++ {
|
||||
if p.dimension[z][y][x] {
|
||||
count++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
func activation(p pocket, x, y, z int) bool {
|
||||
activeNeighbours := 0
|
||||
selfActive := false
|
||||
for zIndex := z - 1; zIndex <= z+1; zIndex++ {
|
||||
if zIndex < 0 || zIndex >= len(p.dimension) {
|
||||
continue
|
||||
}
|
||||
for yIndex := y - 1; yIndex <= y+1; yIndex++ {
|
||||
if yIndex < 0 || yIndex >= len(p.dimension[zIndex]) {
|
||||
continue
|
||||
}
|
||||
for xIndex := x - 1; xIndex <= x+1; xIndex++ {
|
||||
if xIndex < 0 || xIndex >= len(p.dimension[zIndex][yIndex]) {
|
||||
continue
|
||||
}
|
||||
|
||||
if xIndex == x && yIndex == y && zIndex == z {
|
||||
selfActive = p.dimension[zIndex][yIndex][xIndex]
|
||||
continue
|
||||
}
|
||||
|
||||
if p.dimension[zIndex][yIndex][xIndex] {
|
||||
activeNeighbours++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if selfActive {
|
||||
if activeNeighbours == 2 || activeNeighbours == 3 {
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
if activeNeighbours == 3 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *pocket) print() {
|
||||
for z := 0; z < len(p.dimension); z++ {
|
||||
fmt.Printf("\nz=%d\n", z)
|
||||
for y := 0; y < len(p.dimension[z]); y++ {
|
||||
yString := ""
|
||||
for x := 0; x < len(p.dimension[z][y]); x++ {
|
||||
if p.dimension[z][y][x] {
|
||||
yString += "#"
|
||||
} else {
|
||||
yString += "."
|
||||
}
|
||||
}
|
||||
fmt.Println(yString)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func simulate(start pocket, count int) pocket {
|
||||
simulated := start
|
||||
|
||||
for round := 0; round < count; round++ {
|
||||
next := pocket{
|
||||
dimension: make([][][]bool, len(simulated.dimension)+2),
|
||||
}
|
||||
|
||||
for z := -1; z < len(simulated.dimension)+1; z++ {
|
||||
next.dimension[z+1] = make([][]bool, len(simulated.dimension[0])+2)
|
||||
for y := -1; y < len(simulated.dimension[0])+1; y++ {
|
||||
next.dimension[z+1][y+1] = make([]bool, len(simulated.dimension[0][0])+2)
|
||||
for x := -1; x < len(simulated.dimension[0][0])+1; x++ {
|
||||
next.dimension[z+1][y+1][x+1] = activation(simulated, x, y, z)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
simulated = next
|
||||
}
|
||||
|
||||
return simulated
|
||||
}
|
||||
|
||||
// PartOne How many active cubes are left after 6 cycles
|
||||
func PartOne() string {
|
||||
p := pocket{}
|
||||
if err := p.initialize("seventeen/input.txt"); err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
p = simulate(p, 6)
|
||||
return fmt.Sprintf("There are %d active cubes after 6 rounds", p.active())
|
||||
}
|
51
seventeen/day_seventeen_test.go
Normal file
51
seventeen/day_seventeen_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package seventeen
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_dimension_init(t *testing.T) {
|
||||
p := pocket{}
|
||||
if err := p.initialize("sample.txt"); err != nil {
|
||||
t.Logf(err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
active := p.active()
|
||||
if active != 5 {
|
||||
t.Logf("Expected 5 active after 0 rounds, found %d", active)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func Test_dimension_simulate1(t *testing.T) {
|
||||
p := pocket{}
|
||||
if err := p.initialize("sample.txt"); err != nil {
|
||||
t.Logf(err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
p.print()
|
||||
|
||||
p = simulate(p, 1)
|
||||
p.print()
|
||||
|
||||
active := p.active()
|
||||
if active != 12 {
|
||||
t.Logf("Expected 11 active after 1 rounds, found %d", active)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func Test_dimension_simulate(t *testing.T) {
|
||||
p := pocket{}
|
||||
if err := p.initialize("sample.txt"); err != nil {
|
||||
t.Logf(err.Error())
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
p = simulate(p, 6)
|
||||
p.print()
|
||||
active := p.active()
|
||||
if active != 112 {
|
||||
t.Logf("Expected 112 active after 6 rounds, found %d", active)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
8
seventeen/input.txt
Normal file
8
seventeen/input.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
###...#.
|
||||
.##.####
|
||||
.####.##
|
||||
###.###.
|
||||
.##.####
|
||||
#.##..#.
|
||||
##.####.
|
||||
.####.#.
|
3
seventeen/sample.txt
Normal file
3
seventeen/sample.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
.#.
|
||||
..#
|
||||
###
|
Reference in New Issue
Block a user