day18, struggling
This commit is contained in:
121
day18/lagoon.go
121
day18/lagoon.go
@@ -12,21 +12,22 @@ import (
|
||||
func Run() int {
|
||||
log.Println("hello day 18")
|
||||
log.Println("problem of lagoon bgins")
|
||||
filename := "day18/example"
|
||||
instructions := ReadInstructionas(filename)
|
||||
filename := "day18/example2"
|
||||
instructions := ReadInstructionas2(filename)
|
||||
h, w := calcHeightWidth(instructions)
|
||||
log.Printf("read %+v instructions", instructions)
|
||||
|
||||
field := CreateField(h, w)
|
||||
log.Println("created field")
|
||||
|
||||
// fmt.Println(field.String())
|
||||
fmt.Println(field.String())
|
||||
borderAmount := field.digByInstructions(instructions)
|
||||
// log.Println("created field", field.BorderCellCols)
|
||||
|
||||
fmt.Println(field.String())
|
||||
// WriteToFile("borders.txt", field.String())
|
||||
// convert -size 3000x6000 xc:white -font "FreeMono" -pointsize 13 -fill black -draw @borders.txt borders.png
|
||||
|
||||
log.Printf("starting dig inside for cols %d-%d and rows %d-%d ", field.MinCol, field.MaxCol, field.MinRow, field.MaxRow)
|
||||
insideAmount := field.digInsides()
|
||||
|
||||
log.Printf("border is %d; inside is %d", borderAmount, insideAmount)
|
||||
@@ -212,8 +213,13 @@ type Field struct {
|
||||
// Cells [][]*Cell
|
||||
Cells map[Coord]*Cell
|
||||
MinRow, MaxRow, MinCol, MaxCol int
|
||||
// TODO - make this map[int]map[int]any (for the set)
|
||||
BorderCellCols map[int][]int // known row -> col
|
||||
}
|
||||
func (f *Field)confirmCoord(c Coord) {
|
||||
// log.Printf("configming coord %+v", c)
|
||||
f.BorderCellCols[c.Row] = append(f.BorderCellCols[c.Row], c.Col)
|
||||
|
||||
if c.Row - 3 < f.MinRow {
|
||||
f.MinRow = c.Row - 3
|
||||
}
|
||||
@@ -232,6 +238,7 @@ func CreateField(height, width int) Field {
|
||||
return Field{
|
||||
Height: height, Width: width,
|
||||
Cells: make(map[Coord]*Cell),
|
||||
BorderCellCols: make(map[int][]int),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,11 +247,11 @@ func (f *Field) digByInstructions(instructions []Instruction) (borderAmount int)
|
||||
f.Cells[runnerCoord] = &Cell{
|
||||
IsDug: true,
|
||||
}
|
||||
f.confirmCoord(runnerCoord)
|
||||
// f.confirmCoord(runnerCoord) // should be confirmed when the cycle is closed on last step
|
||||
// borderAmount += 1
|
||||
|
||||
for _, instruction := range instructions {
|
||||
log.Printf("starting instruction %+v", instruction)
|
||||
fmt.Printf("starting instruction %+v", instruction)
|
||||
for i := 0; i < instruction.Steps; i++ {
|
||||
runnerCoord = runnerCoord.applyDirection(instruction.Direction)
|
||||
f.Cells[runnerCoord] = &Cell{
|
||||
@@ -281,44 +288,86 @@ func (f *Field) String() string {
|
||||
s += "\""
|
||||
return s
|
||||
}
|
||||
|
||||
func (f *Field) digInsides() (countInside int) {
|
||||
for row := f.MinRow; row < f.MaxRow; row++ {
|
||||
isInside := false
|
||||
seenUp, seenDown := false, false // for detecting L---7 walls
|
||||
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)
|
||||
downCellIsDug := f.isCellDug(row+1, col)
|
||||
if !rightCellIsDug {
|
||||
if (upCellIsDug && seenDown) || (downCellIsDug && seenUp) {
|
||||
isInside = !isInside
|
||||
}
|
||||
seenUp, seenDown = false, false
|
||||
}
|
||||
} else {
|
||||
// not a dug out cell, maybe inside and needs to be dug out
|
||||
if isInside {
|
||||
// f.Cells[Coord{col, row}] = &Cell{
|
||||
// ToBeDug: true,
|
||||
// }
|
||||
|
||||
countInside += 1
|
||||
log.Printf("tick count inside for %d %d", row, col)
|
||||
// cellPtr.ToBeDug = true
|
||||
}
|
||||
if rightCellIsDug {
|
||||
seenUp = f.isCellDug(row-1, col+1)
|
||||
seenDown = f.isCellDug(row+1, col+1)
|
||||
}
|
||||
|
||||
if row % 10000 == 0 {
|
||||
log.Printf("processed rows %d out of %d", row, f.MaxRow)
|
||||
}
|
||||
thisRowBorderCols := f.BorderCellCols[row]
|
||||
slices.Sort(thisRowBorderCols)
|
||||
// log.Printf("cols for row %d are %+v", row, thisRowBorderCols)
|
||||
if len(thisRowBorderCols) == 0 {
|
||||
continue
|
||||
}
|
||||
isInside := true
|
||||
prevCol := thisRowBorderCols[0]
|
||||
for _, col := range thisRowBorderCols[1:] {
|
||||
gap := (col - prevCol - 1)
|
||||
if gap == 0 {
|
||||
prevCol = col
|
||||
continue
|
||||
}
|
||||
// log.Printf("found gap in row %d. is inside %t. between col %d and %d of length %d",
|
||||
// row, isInside, col, prevCol, gap)
|
||||
if isInside {
|
||||
for coll := prevCol+1; coll < col; coll++ {
|
||||
f.Cells[Coord{Col: coll, Row: row}] = &Cell{
|
||||
ToBeDug: true,
|
||||
}
|
||||
|
||||
}
|
||||
countInside += gap
|
||||
}
|
||||
|
||||
isInside = !isInside
|
||||
prevCol = col
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// func (f *Field) digInsides() (countInside int) {
|
||||
// for row := f.MinRow; row < f.MaxRow; row++ {
|
||||
// if row % 10000 == 0 {
|
||||
// log.Printf("processed rows %d out of %d", row, f.MaxRow)
|
||||
// }
|
||||
// isInside := false
|
||||
// seenUp, seenDown := false, false // for detecting L---7 walls
|
||||
// for col := f.MinCol; col < f.MaxCol; col++ {
|
||||
// // TODO next optimization - for each row, store indices of cols with border cells
|
||||
// // so that count of inside would be done by many at a time
|
||||
// rightCellIsDug := f.isCellDug(row, col+1)
|
||||
// if f.isCellDug(row, col) {
|
||||
// upCellIsDug := f.isCellDug(row-1, col)
|
||||
// downCellIsDug := f.isCellDug(row+1, col)
|
||||
// if !rightCellIsDug {
|
||||
// if (upCellIsDug && seenDown) || (downCellIsDug && seenUp) {
|
||||
// isInside = !isInside
|
||||
// }
|
||||
// seenUp, seenDown = false, false
|
||||
// }
|
||||
// } else {
|
||||
// // not a dug out cell, maybe inside and needs to be dug out
|
||||
// if isInside {
|
||||
// // f.Cells[Coord{col, row}] = &Cell{
|
||||
// // ToBeDug: true,
|
||||
// // }
|
||||
|
||||
// countInside += 1
|
||||
// // log.Printf("tick count inside for %d %d", row, col)
|
||||
// // cellPtr.ToBeDug = true
|
||||
// }
|
||||
// if rightCellIsDug {
|
||||
// seenUp = f.isCellDug(row-1, col+1)
|
||||
// seenDown = f.isCellDug(row+1, col+1)
|
||||
// }
|
||||
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
func (f *Field) isCellDug(row, col int) bool {
|
||||
cell := f.Cells[Coord{col, row}]
|
||||
return cell != nil && cell.IsDug
|
||||
|
||||
Reference in New Issue
Block a user