Day 17: Part 2
But Part 1 is gone now! Signed-off-by: James Griffin <james@unsupervised.ca>
This commit is contained in:
2
main.go
2
main.go
@@ -72,5 +72,5 @@ func main() {
|
|||||||
// fmt.Println(sixteen.PartTwo())
|
// fmt.Println(sixteen.PartTwo())
|
||||||
|
|
||||||
// Day 17
|
// Day 17
|
||||||
fmt.Println(seventeen.PartOne())
|
fmt.Println(seventeen.PartTwo())
|
||||||
}
|
}
|
||||||
|
@@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type pocket struct {
|
type pocket struct {
|
||||||
dimension [][][]bool
|
dimension [][][][]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pocket) initialize(filename string) error {
|
func (p *pocket) initialize(filename string) error {
|
||||||
@@ -20,16 +20,18 @@ func (p *pocket) initialize(filename string) error {
|
|||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
y := 0
|
y := 0
|
||||||
z := 0
|
z := 0
|
||||||
p.dimension = [][][]bool{}
|
w := 0
|
||||||
p.dimension = append(p.dimension, [][]bool{})
|
p.dimension = [][][][]bool{}
|
||||||
|
p.dimension = append(p.dimension, [][][]bool{})
|
||||||
|
p.dimension[w] = append(p.dimension[w], [][]bool{})
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
p.dimension[z] = append(p.dimension[z], []bool{})
|
p.dimension[w][z] = append(p.dimension[w][z], []bool{})
|
||||||
for _, c := range scanner.Text() {
|
for _, c := range scanner.Text() {
|
||||||
switch string(c) {
|
switch string(c) {
|
||||||
case "#":
|
case "#":
|
||||||
p.dimension[z][y] = append(p.dimension[z][y], true)
|
p.dimension[w][z][y] = append(p.dimension[w][z][y], true)
|
||||||
case ".":
|
case ".":
|
||||||
p.dimension[z][y] = append(p.dimension[z][y], false)
|
p.dimension[w][z][y] = append(p.dimension[w][z][y], false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
y++
|
y++
|
||||||
@@ -41,11 +43,13 @@ func (p *pocket) initialize(filename string) error {
|
|||||||
func (p *pocket) active() int {
|
func (p *pocket) active() int {
|
||||||
count := 0
|
count := 0
|
||||||
|
|
||||||
for z := 0; z < len(p.dimension); z++ {
|
for w := 0; w < len(p.dimension); w++ {
|
||||||
for y := 0; y < len(p.dimension[z]); y++ {
|
for z := 0; z < len(p.dimension[w]); z++ {
|
||||||
for x := 0; x < len(p.dimension[z][y]); x++ {
|
for y := 0; y < len(p.dimension[w][z]); y++ {
|
||||||
if p.dimension[z][y][x] {
|
for x := 0; x < len(p.dimension[w][z][y]); x++ {
|
||||||
count++
|
if p.dimension[w][z][y][x] {
|
||||||
|
count++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -54,29 +58,34 @@ func (p *pocket) active() int {
|
|||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
func activation(p pocket, x, y, z int) bool {
|
func activation(p pocket, x, y, z, w int) bool {
|
||||||
activeNeighbours := 0
|
activeNeighbours := 0
|
||||||
selfActive := false
|
selfActive := false
|
||||||
for zIndex := z - 1; zIndex <= z+1; zIndex++ {
|
for wIndex := w - 1; wIndex <= w+1; wIndex++ {
|
||||||
if zIndex < 0 || zIndex >= len(p.dimension) {
|
if wIndex < 0 || wIndex >= len(p.dimension) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for yIndex := y - 1; yIndex <= y+1; yIndex++ {
|
for zIndex := z - 1; zIndex <= z+1; zIndex++ {
|
||||||
if yIndex < 0 || yIndex >= len(p.dimension[zIndex]) {
|
if zIndex < 0 || zIndex >= len(p.dimension[wIndex]) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for xIndex := x - 1; xIndex <= x+1; xIndex++ {
|
for yIndex := y - 1; yIndex <= y+1; yIndex++ {
|
||||||
if xIndex < 0 || xIndex >= len(p.dimension[zIndex][yIndex]) {
|
if yIndex < 0 || yIndex >= len(p.dimension[wIndex][zIndex]) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
for xIndex := x - 1; xIndex <= x+1; xIndex++ {
|
||||||
|
if xIndex < 0 || xIndex >= len(p.dimension[wIndex][zIndex][yIndex]) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if xIndex == x && yIndex == y && zIndex == z {
|
if xIndex == x && yIndex == y && zIndex == z && wIndex == w {
|
||||||
selfActive = p.dimension[zIndex][yIndex][xIndex]
|
selfActive = p.dimension[wIndex][zIndex][yIndex][xIndex]
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.dimension[zIndex][yIndex][xIndex] {
|
if p.dimension[wIndex][zIndex][yIndex][xIndex] {
|
||||||
activeNeighbours++
|
activeNeighbours++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -96,18 +105,20 @@ func activation(p pocket, x, y, z int) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *pocket) print() {
|
func (p *pocket) print() {
|
||||||
for z := 0; z < len(p.dimension); z++ {
|
for w := 0; w < len(p.dimension); w++ {
|
||||||
fmt.Printf("\nz=%d\n", z)
|
for z := 0; z < len(p.dimension[w]); z++ {
|
||||||
for y := 0; y < len(p.dimension[z]); y++ {
|
fmt.Printf("\nz=%d, w=%d\n", z, w)
|
||||||
yString := ""
|
for y := 0; y < len(p.dimension[w][z]); y++ {
|
||||||
for x := 0; x < len(p.dimension[z][y]); x++ {
|
yString := ""
|
||||||
if p.dimension[z][y][x] {
|
for x := 0; x < len(p.dimension[w][z][y]); x++ {
|
||||||
yString += "#"
|
if p.dimension[w][z][y][x] {
|
||||||
} else {
|
yString += "#"
|
||||||
yString += "."
|
} else {
|
||||||
|
yString += "."
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
fmt.Println(yString)
|
||||||
}
|
}
|
||||||
fmt.Println(yString)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -117,15 +128,18 @@ func simulate(start pocket, count int) pocket {
|
|||||||
|
|
||||||
for round := 0; round < count; round++ {
|
for round := 0; round < count; round++ {
|
||||||
next := pocket{
|
next := pocket{
|
||||||
dimension: make([][][]bool, len(simulated.dimension)+2),
|
dimension: make([][][][]bool, len(simulated.dimension)+2),
|
||||||
}
|
}
|
||||||
|
|
||||||
for z := -1; z < len(simulated.dimension)+1; z++ {
|
for w := -1; w < len(simulated.dimension)+1; w++ {
|
||||||
next.dimension[z+1] = make([][]bool, len(simulated.dimension[0])+2)
|
next.dimension[w+1] = make([][][]bool, len(simulated.dimension[0])+2)
|
||||||
for y := -1; y < len(simulated.dimension[0])+1; y++ {
|
for z := -1; z < len(simulated.dimension[0])+1; z++ {
|
||||||
next.dimension[z+1][y+1] = make([]bool, len(simulated.dimension[0][0])+2)
|
next.dimension[w+1][z+1] = make([][]bool, len(simulated.dimension[0][0])+2)
|
||||||
for x := -1; x < len(simulated.dimension[0][0])+1; x++ {
|
for y := -1; y < len(simulated.dimension[0][0])+1; y++ {
|
||||||
next.dimension[z+1][y+1][x+1] = activation(simulated, x, y, z)
|
next.dimension[w+1][z+1][y+1] = make([]bool, len(simulated.dimension[0][0][0])+2)
|
||||||
|
for x := -1; x < len(simulated.dimension[0][0][0])+1; x++ {
|
||||||
|
next.dimension[w+1][z+1][y+1][x+1] = activation(simulated, x, y, z, w)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -136,8 +150,8 @@ func simulate(start pocket, count int) pocket {
|
|||||||
return simulated
|
return simulated
|
||||||
}
|
}
|
||||||
|
|
||||||
// PartOne How many active cubes are left after 6 cycles
|
// PartTwo How many active cubes are left after 6 cycles
|
||||||
func PartOne() string {
|
func PartTwo() string {
|
||||||
p := pocket{}
|
p := pocket{}
|
||||||
if err := p.initialize("seventeen/input.txt"); err != nil {
|
if err := p.initialize("seventeen/input.txt"); err != nil {
|
||||||
return err.Error()
|
return err.Error()
|
||||||
|
@@ -22,14 +22,13 @@ func Test_dimension_simulate1(t *testing.T) {
|
|||||||
t.Logf(err.Error())
|
t.Logf(err.Error())
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
p.print()
|
|
||||||
|
|
||||||
p = simulate(p, 1)
|
p = simulate(p, 1)
|
||||||
p.print()
|
p.print()
|
||||||
|
|
||||||
active := p.active()
|
active := p.active()
|
||||||
if active != 12 {
|
if active != 29 {
|
||||||
t.Logf("Expected 11 active after 1 rounds, found %d", active)
|
t.Logf("Expected 29 active after 1 rounds, found %d", active)
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,10 +41,10 @@ func Test_dimension_simulate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
p = simulate(p, 6)
|
p = simulate(p, 6)
|
||||||
p.print()
|
|
||||||
active := p.active()
|
active := p.active()
|
||||||
if active != 112 {
|
if active != 848 {
|
||||||
t.Logf("Expected 112 active after 6 rounds, found %d", active)
|
t.Logf("Expected 848 active after 6 rounds, found %d", active)
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user