day18: example optimized even more

This commit is contained in:
efim 2023-12-18 12:09:44 +00:00
parent b831e92e1f
commit 4fc5caf228
1 changed files with 44 additions and 90 deletions

View File

@ -184,19 +184,19 @@ func calcHeightWidth(instructions []Instruction) (height, width int) {
}
type Coord struct {
X, Y int
Col, Row int
}
func (c Coord) applyDirection(d Direction) Coord {
switch d {
case Upward:
c.Y -= 1
c.Row -= 1
case Downward:
c.Y += 1
c.Row += 1
case Leftward:
c.X -= 1
c.Col -= 1
case Rightward:
c.X += 1
c.Col += 1
}
return c
@ -209,46 +209,48 @@ type Cell struct {
}
type Field struct {
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 {
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{
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) {
runnerCoord := Coord{X: 0, Y: 0}
row, col := f.coordToIndices(runnerCoord)
f.Cells[row][col] = &Cell{
runnerCoord := Coord{Col: 0, Row: 0}
f.Cells[runnerCoord] = &Cell{
IsDug: true,
}
f.confirmCoord(runnerCoord)
// borderAmount += 1
for _, instruction := range instructions {
log.Printf("starting instruction %+v", instruction)
for i := 0; i < instruction.Steps; i++ {
runnerCoord = runnerCoord.applyDirection(instruction.Direction)
row, col := f.coordToIndices(runnerCoord)
f.Cells[row][col] = &Cell{
f.Cells[runnerCoord] = &Cell{
IsDug: true,
}
f.confirmCoord(runnerCoord)
borderAmount += 1
}
}
@ -257,56 +259,19 @@ func (f *Field) digByInstructions(instructions []Instruction) (borderAmount int)
func (f *Field) String() string {
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 {
seenInRow := false
for j := 0; j < len(row); j++ {
if f.isCellDug(i, j) {
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]
for row := f.MinRow; row <= f.MaxRow; row++ {
rowChars := make([]rune, f.MaxCol - f.MinCol + 1)
for col := f.MinCol; col <= f.MaxCol; col++ {
cell := f.Cells[Coord{col, row}]
if cell != nil && cell.ToBeDug {
rowChars[col] = '@'
} else if f.isCellDug(i, col) {
rowChars[col] = '#'
rowChars[col - f.MinCol] = '@'
continue
}
if f.isCellDug(row, col) {
rowChars[col - f.MinCol] = '#'
} else {
rowChars[col] = '.'
rowChars[col - f.MinCol] = '.'
}
}
@ -318,10 +283,10 @@ func (f *Field) String() string {
}
func (f *Field) digInsides() (countInside int) {
for row := 1; row < f.Height-1; row++ {
for row := f.MinRow; row < f.MaxRow; row++ {
isInside := false
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)
if f.isCellDug(row, col) {
upCellIsDug := f.isCellDug(row-1, col)
@ -335,9 +300,9 @@ func (f *Field) digInsides() (countInside int) {
} else {
// not a dug out cell, maybe inside and needs to be dug out
if isInside {
f.Cells[row][col] = &Cell{
ToBeDug: true,
}
// f.Cells[Coord{col, row}] = &Cell{
// ToBeDug: true,
// }
countInside += 1
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 {
cell := f.Cells[row][col]
cell := f.Cells[Coord{col, row}]
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) {
fileBorder, err := os.Create(filename)
if err != nil {