day18: example optimized even more
This commit is contained in:
parent
b831e92e1f
commit
4fc5caf228
134
day18/lagoon.go
134
day18/lagoon.go
|
@ -184,19 +184,19 @@ func calcHeightWidth(instructions []Instruction) (height, width int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Coord struct {
|
type Coord struct {
|
||||||
X, Y int
|
Col, Row int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Coord) applyDirection(d Direction) Coord {
|
func (c Coord) applyDirection(d Direction) Coord {
|
||||||
switch d {
|
switch d {
|
||||||
case Upward:
|
case Upward:
|
||||||
c.Y -= 1
|
c.Row -= 1
|
||||||
case Downward:
|
case Downward:
|
||||||
c.Y += 1
|
c.Row += 1
|
||||||
case Leftward:
|
case Leftward:
|
||||||
c.X -= 1
|
c.Col -= 1
|
||||||
case Rightward:
|
case Rightward:
|
||||||
c.X += 1
|
c.Col += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return c
|
return c
|
||||||
|
@ -209,46 +209,48 @@ type Cell struct {
|
||||||
}
|
}
|
||||||
type Field struct {
|
type Field struct {
|
||||||
Height, Width int
|
Height, Width int
|
||||||
Cells [][]*Cell
|
// Cells [][]*Cell
|
||||||
|
Cells map[Coord]*Cell
|
||||||
|
MinRow, MaxRow, MinCol, MaxCol int
|
||||||
|
}
|
||||||
|
func (f *Field)confirmCoord(c Coord) {
|
||||||
|
if c.Row - 3 < f.MinRow {
|
||||||
|
f.MinRow = c.Row - 3
|
||||||
|
}
|
||||||
|
if c.Row + 3 > f.MaxRow {
|
||||||
|
f.MaxRow = c.Row + 3
|
||||||
|
}
|
||||||
|
if c.Col - 3 < f.MinCol {
|
||||||
|
f.MinCol = c.Col - 3
|
||||||
|
}
|
||||||
|
if c.Col + 3 > f.MaxCol {
|
||||||
|
f.MaxCol = c.Col + 3
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateField(height, width int) Field {
|
func CreateField(height, width int) Field {
|
||||||
rows := make([][]*Cell, height)
|
|
||||||
for i := 0; i < height; i++ {
|
|
||||||
row := make([]*Cell, width)
|
|
||||||
rows[i] = row
|
|
||||||
// for j := 0; j < width; j++ {
|
|
||||||
// row[j] = &Cell{}
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
return Field{
|
return Field{
|
||||||
Height: height, Width: width,
|
Height: height, Width: width,
|
||||||
Cells: rows,
|
Cells: make(map[Coord]*Cell),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Field) coordToIndices(c Coord) (row, col int) {
|
|
||||||
row = c.Y + (f.Height / 2)
|
|
||||||
col = c.X + (f.Width / 2)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Field) digByInstructions(instructions []Instruction) (borderAmount int) {
|
func (f *Field) digByInstructions(instructions []Instruction) (borderAmount int) {
|
||||||
runnerCoord := Coord{X: 0, Y: 0}
|
runnerCoord := Coord{Col: 0, Row: 0}
|
||||||
row, col := f.coordToIndices(runnerCoord)
|
f.Cells[runnerCoord] = &Cell{
|
||||||
f.Cells[row][col] = &Cell{
|
|
||||||
IsDug: true,
|
IsDug: true,
|
||||||
}
|
}
|
||||||
|
f.confirmCoord(runnerCoord)
|
||||||
// borderAmount += 1
|
// borderAmount += 1
|
||||||
|
|
||||||
for _, instruction := range instructions {
|
for _, instruction := range instructions {
|
||||||
log.Printf("starting instruction %+v", instruction)
|
log.Printf("starting instruction %+v", instruction)
|
||||||
for i := 0; i < instruction.Steps; i++ {
|
for i := 0; i < instruction.Steps; i++ {
|
||||||
runnerCoord = runnerCoord.applyDirection(instruction.Direction)
|
runnerCoord = runnerCoord.applyDirection(instruction.Direction)
|
||||||
row, col := f.coordToIndices(runnerCoord)
|
f.Cells[runnerCoord] = &Cell{
|
||||||
f.Cells[row][col] = &Cell{
|
|
||||||
IsDug: true,
|
IsDug: true,
|
||||||
}
|
}
|
||||||
|
f.confirmCoord(runnerCoord)
|
||||||
borderAmount += 1
|
borderAmount += 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -257,56 +259,19 @@ func (f *Field) digByInstructions(instructions []Instruction) (borderAmount int)
|
||||||
|
|
||||||
func (f *Field) String() string {
|
func (f *Field) String() string {
|
||||||
s := "text 15,15 \""
|
s := "text 15,15 \""
|
||||||
firstNonemptyRow, lastNonemptyRow := 0, 0
|
|
||||||
log.Print("just use", firstNonemptyRow, lastNonemptyRow)
|
|
||||||
seenInRows := false
|
|
||||||
firstNonemptyCol, lastNonemptyCol := 0, 0
|
|
||||||
seenInCols := false
|
|
||||||
|
|
||||||
for i, row := range f.Cells {
|
for row := f.MinRow; row <= f.MaxRow; row++ {
|
||||||
seenInRow := false
|
rowChars := make([]rune, f.MaxCol - f.MinCol + 1)
|
||||||
for j := 0; j < len(row); j++ {
|
for col := f.MinCol; col <= f.MaxCol; col++ {
|
||||||
if f.isCellDug(i, j) {
|
cell := f.Cells[Coord{col, row}]
|
||||||
seenInRow = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
seenInRows = seenInRows || seenInRow
|
|
||||||
if seenInRow {
|
|
||||||
lastNonemptyRow = i
|
|
||||||
}
|
|
||||||
if !seenInRows {
|
|
||||||
firstNonemptyRow = i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for col := 0; col < f.Width; col++ {
|
|
||||||
seenInCol := false
|
|
||||||
for row := 0; row < f.Height; row++ {
|
|
||||||
if f.isCellDug(row, col) {
|
|
||||||
seenInCol = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
seenInCols = seenInCols || seenInCol
|
|
||||||
if seenInCol {
|
|
||||||
lastNonemptyCol = col
|
|
||||||
}
|
|
||||||
if !seenInCols {
|
|
||||||
firstNonemptyCol = col
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rowLen := lastNonemptyCol - firstNonemptyCol + 1
|
|
||||||
log.Print(rowLen)
|
|
||||||
for i := 0; i <= f.Height-1; i++ {
|
|
||||||
rowChars := make([]rune, f.Width)
|
|
||||||
for col := 0; col <= f.Width-1; col++ {
|
|
||||||
cell := f.Cells[i][col]
|
|
||||||
if cell != nil && cell.ToBeDug {
|
if cell != nil && cell.ToBeDug {
|
||||||
rowChars[col] = '@'
|
rowChars[col - f.MinCol] = '@'
|
||||||
} else if f.isCellDug(i, col) {
|
continue
|
||||||
rowChars[col] = '#'
|
}
|
||||||
|
if f.isCellDug(row, col) {
|
||||||
|
rowChars[col - f.MinCol] = '#'
|
||||||
} else {
|
} else {
|
||||||
rowChars[col] = '.'
|
rowChars[col - f.MinCol] = '.'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -318,10 +283,10 @@ func (f *Field) String() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Field) digInsides() (countInside int) {
|
func (f *Field) digInsides() (countInside int) {
|
||||||
for row := 1; row < f.Height-1; row++ {
|
for row := f.MinRow; row < f.MaxRow; row++ {
|
||||||
isInside := false
|
isInside := false
|
||||||
seenUp, seenDown := false, false // for detecting L---7 walls
|
seenUp, seenDown := false, false // for detecting L---7 walls
|
||||||
for col := 0; col < f.Width-1; col++ {
|
for col := f.MinCol; col < f.MaxCol; col++ {
|
||||||
rightCellIsDug := f.isCellDug(row, col+1)
|
rightCellIsDug := f.isCellDug(row, col+1)
|
||||||
if f.isCellDug(row, col) {
|
if f.isCellDug(row, col) {
|
||||||
upCellIsDug := f.isCellDug(row-1, col)
|
upCellIsDug := f.isCellDug(row-1, col)
|
||||||
|
@ -335,9 +300,9 @@ func (f *Field) digInsides() (countInside int) {
|
||||||
} else {
|
} else {
|
||||||
// not a dug out cell, maybe inside and needs to be dug out
|
// not a dug out cell, maybe inside and needs to be dug out
|
||||||
if isInside {
|
if isInside {
|
||||||
f.Cells[row][col] = &Cell{
|
// f.Cells[Coord{col, row}] = &Cell{
|
||||||
ToBeDug: true,
|
// ToBeDug: true,
|
||||||
}
|
// }
|
||||||
|
|
||||||
countInside += 1
|
countInside += 1
|
||||||
log.Printf("tick count inside for %d %d", row, col)
|
log.Printf("tick count inside for %d %d", row, col)
|
||||||
|
@ -355,21 +320,10 @@ func (f *Field) digInsides() (countInside int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Field) isCellDug(row, col int) bool {
|
func (f *Field) isCellDug(row, col int) bool {
|
||||||
cell := f.Cells[row][col]
|
cell := f.Cells[Coord{col, row}]
|
||||||
return cell != nil && cell.IsDug
|
return cell != nil && cell.IsDug
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (f *Field)countDugOut() (result int) {
|
|
||||||
// for _, row := range f.Cells {
|
|
||||||
// for _, cell := range row {
|
|
||||||
// if cell.IsDug || cell.ToBeDug {
|
|
||||||
// result += 1
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
func WriteToFile(filename string, content string) {
|
func WriteToFile(filename string, content string) {
|
||||||
fileBorder, err := os.Create(filename)
|
fileBorder, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue