Day 24: Part 2
The search is _not_ fast since it scales as the tiles move away from each other. Should been more clever about the search space ¯\_(ツ)_/¯ Signed-off-by: James Griffin <james@unsupervised.ca>
This commit is contained in:
1
main.go
1
main.go
@@ -101,4 +101,5 @@ func main() {
|
|||||||
|
|
||||||
// Day 24
|
// Day 24
|
||||||
fmt.Println(twentyfour.PartOne())
|
fmt.Println(twentyfour.PartOne())
|
||||||
|
fmt.Println(twentyfour.PartTwo())
|
||||||
}
|
}
|
||||||
|
@@ -83,6 +83,79 @@ func (l *lobby) countTiles() (black, white int) {
|
|||||||
return black, white
|
return black, white
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *lobby) dailyFlip(days int) {
|
||||||
|
for i := 0; i < days; i++ {
|
||||||
|
fmt.Println("Day", i+1)
|
||||||
|
newGrid := map[string]bool{}
|
||||||
|
|
||||||
|
xMin, yMin, zMin, xMax, yMax, zMax := 0, 0, 0, 0, 0, 0
|
||||||
|
for index := range l.tiles {
|
||||||
|
var x, y, z int
|
||||||
|
fmt.Sscanf(index, "%d,%d,%d", &x, &y, &z)
|
||||||
|
if x < xMin {
|
||||||
|
xMin = x
|
||||||
|
}
|
||||||
|
if x > xMax {
|
||||||
|
xMax = x
|
||||||
|
}
|
||||||
|
|
||||||
|
if y < yMin {
|
||||||
|
yMin = y
|
||||||
|
}
|
||||||
|
|
||||||
|
if y > yMax {
|
||||||
|
yMax = y
|
||||||
|
}
|
||||||
|
|
||||||
|
if z < zMin {
|
||||||
|
zMin = z
|
||||||
|
}
|
||||||
|
|
||||||
|
if z > zMax {
|
||||||
|
zMax = z
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for x := xMin - 1; x <= xMax+1; x++ {
|
||||||
|
for y := yMin - 1; y <= yMax+1; y++ {
|
||||||
|
for z := zMin - 1; z <= zMax+1; z++ {
|
||||||
|
blackNeighbours := 0
|
||||||
|
if l.tiles[fmt.Sprintf("%d,%d,%d", x+1, y, z-1)] {
|
||||||
|
blackNeighbours++
|
||||||
|
}
|
||||||
|
if l.tiles[fmt.Sprintf("%d,%d,%d", x+1, y-1, z)] {
|
||||||
|
blackNeighbours++
|
||||||
|
}
|
||||||
|
if l.tiles[fmt.Sprintf("%d,%d,%d", x, y-1, z+1)] {
|
||||||
|
blackNeighbours++
|
||||||
|
}
|
||||||
|
if l.tiles[fmt.Sprintf("%d,%d,%d", x-1, y, z+1)] {
|
||||||
|
blackNeighbours++
|
||||||
|
}
|
||||||
|
if l.tiles[fmt.Sprintf("%d,%d,%d", x-1, y+1, z)] {
|
||||||
|
blackNeighbours++
|
||||||
|
}
|
||||||
|
if l.tiles[fmt.Sprintf("%d,%d,%d", x, y+1, z-1)] {
|
||||||
|
blackNeighbours++
|
||||||
|
}
|
||||||
|
|
||||||
|
index := fmt.Sprintf("%d,%d,%d", x, y, z)
|
||||||
|
if l.tiles[index] {
|
||||||
|
if blackNeighbours > 0 && blackNeighbours < 3 {
|
||||||
|
newGrid[index] = true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if blackNeighbours == 2 {
|
||||||
|
newGrid[index] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
l.tiles = newGrid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// PartOne How many tiles are left black
|
// PartOne How many tiles are left black
|
||||||
func PartOne() string {
|
func PartOne() string {
|
||||||
l := lobby{}
|
l := lobby{}
|
||||||
@@ -93,3 +166,15 @@ func PartOne() string {
|
|||||||
|
|
||||||
return fmt.Sprintf("There are %d black tiles", black)
|
return fmt.Sprintf("There are %d black tiles", black)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PartTwo How many tiles are black after 100 days
|
||||||
|
func PartTwo() string {
|
||||||
|
l := lobby{}
|
||||||
|
l.load("twentyfour/input.txt")
|
||||||
|
|
||||||
|
l.layout()
|
||||||
|
l.dailyFlip(100)
|
||||||
|
black, _ := l.countTiles()
|
||||||
|
|
||||||
|
return fmt.Sprintf("There are %d black tiles", black)
|
||||||
|
}
|
||||||
|
@@ -23,7 +23,7 @@ func Test_layout_tiles(t *testing.T) {
|
|||||||
|
|
||||||
l.layout()
|
l.layout()
|
||||||
black, white := l.countTiles()
|
black, white := l.countTiles()
|
||||||
if black + white != 15 {
|
if black+white != 15 {
|
||||||
t.Logf("Expected 15 tiles, got %d", black+white)
|
t.Logf("Expected 15 tiles, got %d", black+white)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
@@ -38,3 +38,72 @@ func Test_layout_tiles(t *testing.T) {
|
|||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_daily_flip(t *testing.T) {
|
||||||
|
l := lobby{}
|
||||||
|
l.load("sample.txt")
|
||||||
|
|
||||||
|
l.layout()
|
||||||
|
black, white := l.countTiles()
|
||||||
|
if black+white != 15 {
|
||||||
|
t.Logf("Expected 15 tiles, got %d", black+white)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
if black != 10 {
|
||||||
|
t.Logf("Expected 10 black tiles, got %d", black)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
l.dailyFlip(1)
|
||||||
|
black, _ = l.countTiles()
|
||||||
|
if black != 15 {
|
||||||
|
t.Logf("Expected 15 black tiles, got %d", black)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
l.dailyFlip(1)
|
||||||
|
black, _ = l.countTiles()
|
||||||
|
if black != 12 {
|
||||||
|
t.Logf("Expected 12 black tiles, got %d", black)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
l.dailyFlip(1)
|
||||||
|
black, _ = l.countTiles()
|
||||||
|
if black != 25 {
|
||||||
|
t.Logf("Expected 25 black tiles, got %d", black)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
l.dailyFlip(1)
|
||||||
|
black, _ = l.countTiles()
|
||||||
|
if black != 14 {
|
||||||
|
t.Logf("Expected 14 black tiles, got %d", black)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_flip(t *testing.T) {
|
||||||
|
l := lobby{}
|
||||||
|
l.load("sample.txt")
|
||||||
|
|
||||||
|
l.layout()
|
||||||
|
black, white := l.countTiles()
|
||||||
|
if black+white != 15 {
|
||||||
|
t.Logf("Expected 15 tiles, got %d", black+white)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
if black != 10 {
|
||||||
|
t.Logf("Expected 10 black tiles, got %d", black)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
l.dailyFlip(100)
|
||||||
|
black, _ = l.countTiles()
|
||||||
|
if black != 2208 {
|
||||||
|
t.Logf("Expected 2208 black tiles, got %d", black)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user