day14, too slow. need memo?
This commit is contained in:
@@ -2,6 +2,7 @@ package day14
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
@@ -13,8 +14,16 @@ func Run() int {
|
||||
fmt.Println(field.String())
|
||||
|
||||
// fmt.Printf("> lines for field %+v\n", field.UpIndices())
|
||||
field.MoveUp(field.Height())
|
||||
fmt.Println(field.String())
|
||||
// field.Move(field.Height(), field.UpIndices())
|
||||
cycles := 1000000000
|
||||
for i := 0; i < cycles; i++ {
|
||||
field.DoSpinCycle()
|
||||
// fmt.Println(field.String())
|
||||
if i % 100000 == 0 {
|
||||
log.Print("done ", i, " cycles")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// north rock load
|
||||
return field.NorthLoad()
|
||||
@@ -28,10 +37,10 @@ type Platform struct {
|
||||
Rocks [][]rune
|
||||
}
|
||||
|
||||
func (p *Platform)Height() int {
|
||||
func (p *Platform) Height() int {
|
||||
return len(p.Rocks)
|
||||
}
|
||||
func (p *Platform)Width() int {
|
||||
func (p *Platform) Width() int {
|
||||
return len(p.Rocks[0])
|
||||
}
|
||||
|
||||
@@ -64,10 +73,11 @@ func (p *Platform) String() string {
|
||||
return text
|
||||
}
|
||||
|
||||
type Coord struct { Row, Col int }
|
||||
type Coord struct{ Row, Col int }
|
||||
|
||||
// indices for moving UP, from down to up
|
||||
func (p *Platform) UpIndices() [][]Coord {
|
||||
lines := make([][]Coord,0)
|
||||
lines := make([][]Coord, 0)
|
||||
for col := 0; col < p.Width(); col++ {
|
||||
line := make([]Coord, 0)
|
||||
for row := 0; row < p.Height(); row++ {
|
||||
@@ -78,8 +88,9 @@ func (p *Platform) UpIndices() [][]Coord {
|
||||
return lines
|
||||
}
|
||||
|
||||
// indices for moving DOWN, from up to down
|
||||
func (p *Platform) DownIndices() [][]Coord {
|
||||
lines := make([][]Coord,0)
|
||||
lines := make([][]Coord, 0)
|
||||
for col := 0; col < p.Width(); col++ {
|
||||
line := make([]Coord, 0)
|
||||
for row := p.Height() - 1; row >= 0; row-- {
|
||||
@@ -90,16 +101,40 @@ func (p *Platform) DownIndices() [][]Coord {
|
||||
return lines
|
||||
}
|
||||
|
||||
func (p *Platform)SymbAt(coord Coord) rune {
|
||||
// indices for moving RIGHT from right to left
|
||||
func (p *Platform) RightIndices() [][]Coord {
|
||||
lines := make([][]Coord, 0)
|
||||
for row := 0; row < p.Height(); row++ {
|
||||
line := make([]Coord, 0)
|
||||
for col := p.Width() - 1; col >= 0; col-- {
|
||||
line = append(line, Coord{Row: row, Col: col})
|
||||
}
|
||||
lines = append(lines, line)
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
// indices for moving LEFT, from left to right
|
||||
func (p *Platform) LeftIndices() [][]Coord {
|
||||
lines := make([][]Coord, 0)
|
||||
for row := 0; row < p.Height(); row++ {
|
||||
line := make([]Coord, 0)
|
||||
for col := 0; col < p.Width(); col++ {
|
||||
line = append(line, Coord{Row: row, Col: col})
|
||||
}
|
||||
lines = append(lines, line)
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
func (p *Platform) SymbAt(coord Coord) rune {
|
||||
return p.Rocks[coord.Row][coord.Col]
|
||||
}
|
||||
func (p *Platform)SetSymbAt(coord Coord, symb rune) {
|
||||
func (p *Platform) SetSymbAt(coord Coord, symb rune) {
|
||||
p.Rocks[coord.Row][coord.Col] = symb
|
||||
}
|
||||
|
||||
func (p *Platform) MoveUp(n int) {
|
||||
lines := p.UpIndices()
|
||||
|
||||
func (p *Platform) Move(n int, lines [][]Coord) {
|
||||
for _, line := range lines {
|
||||
moveSize := 0
|
||||
for i, coord := range line {
|
||||
@@ -119,7 +154,7 @@ func (p *Platform) MoveUp(n int) {
|
||||
// get coord for moveSize back. and set that to 'o'
|
||||
// and set current to '.'
|
||||
// panic if that place is not '.' i guess
|
||||
moveTo := line[i - moveSize]
|
||||
moveTo := line[i-moveSize]
|
||||
symbAtTarget := p.SymbAt(moveTo)
|
||||
if symbAtTarget != Space {
|
||||
panic(fmt.Sprintf("attempting to move %+v to %+v, target symbol is %s, not '.'",
|
||||
@@ -132,7 +167,7 @@ func (p *Platform) MoveUp(n int) {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Platform)NorthLoad() int {
|
||||
func (p *Platform) NorthLoad() int {
|
||||
total := 0
|
||||
height := p.Height()
|
||||
for i, row := range p.Rocks {
|
||||
@@ -144,3 +179,11 @@ func (p *Platform)NorthLoad() int {
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func (p *Platform) DoSpinCycle() {
|
||||
// north, west, south, east - till the end
|
||||
p.Move(p.Height(), p.UpIndices())
|
||||
p.Move(p.Width(), p.LeftIndices())
|
||||
p.Move(p.Height(), p.DownIndices())
|
||||
p.Move(p.Width(), p.RightIndices())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user