From 93103bd18ab2e34472a6e480ef45249c3c7df173 Mon Sep 17 00:00:00 2001 From: James Griffin Date: Thu, 17 Dec 2020 20:58:11 -0400 Subject: [PATCH] Day 17: Part 1 Signed-off-by: James Griffin --- main.go | 9 +- seventeen/day_seventeen.go | 148 ++++++++++++++++++++++++++++++++ seventeen/day_seventeen_test.go | 51 +++++++++++ seventeen/input.txt | 8 ++ seventeen/sample.txt | 3 + 5 files changed, 216 insertions(+), 3 deletions(-) create mode 100644 seventeen/day_seventeen.go create mode 100644 seventeen/day_seventeen_test.go create mode 100644 seventeen/input.txt create mode 100644 seventeen/sample.txt diff --git a/main.go b/main.go index 835f526..6f71704 100644 --- a/main.go +++ b/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()) } diff --git a/seventeen/day_seventeen.go b/seventeen/day_seventeen.go new file mode 100644 index 0000000..7a220e4 --- /dev/null +++ b/seventeen/day_seventeen.go @@ -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()) +} diff --git a/seventeen/day_seventeen_test.go b/seventeen/day_seventeen_test.go new file mode 100644 index 0000000..217dcbf --- /dev/null +++ b/seventeen/day_seventeen_test.go @@ -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() + } +} diff --git a/seventeen/input.txt b/seventeen/input.txt new file mode 100644 index 0000000..8cd8482 --- /dev/null +++ b/seventeen/input.txt @@ -0,0 +1,8 @@ +###...#. +.##.#### +.####.## +###.###. +.##.#### +#.##..#. +##.####. +.####.#. \ No newline at end of file diff --git a/seventeen/sample.txt b/seventeen/sample.txt new file mode 100644 index 0000000..17630fd --- /dev/null +++ b/seventeen/sample.txt @@ -0,0 +1,3 @@ +.#. +..# +### \ No newline at end of file