|
|
|
|
@@ -7,12 +7,13 @@ import (
|
|
|
|
|
"slices"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Run() int {
|
|
|
|
|
log.Println("hello day 18")
|
|
|
|
|
log.Println("problem of lagoon bgins")
|
|
|
|
|
filename := "day18/example"
|
|
|
|
|
filename := "day18/input"
|
|
|
|
|
instructions := ReadInstructionas2(filename)
|
|
|
|
|
h, w := calcHeightWidth(instructions)
|
|
|
|
|
log.Printf("read %+v instructions", instructions)
|
|
|
|
|
@@ -210,8 +211,10 @@ type Cell struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type BorderSymbol rune
|
|
|
|
|
// '' always left to right
|
|
|
|
|
const (Vertical BorderSymbol = '|'
|
|
|
|
|
|
|
|
|
|
// ” always left to right
|
|
|
|
|
const (
|
|
|
|
|
Vertical BorderSymbol = '|'
|
|
|
|
|
ToDown BorderSymbol = '7'
|
|
|
|
|
ToUp BorderSymbol = 'J'
|
|
|
|
|
FromUp BorderSymbol = 'F'
|
|
|
|
|
@@ -225,19 +228,20 @@ type Field struct {
|
|
|
|
|
MinRow, MaxRow, MinCol, MaxCol int
|
|
|
|
|
BordersFromLeft map[int]map[int]BorderSymbol
|
|
|
|
|
}
|
|
|
|
|
func (f *Field)confirmCoord(c Coord) {
|
|
|
|
|
|
|
|
|
|
func (f *Field) confirmCoord(c Coord) {
|
|
|
|
|
// log.Printf("configming coord %+v", c)
|
|
|
|
|
|
|
|
|
|
if c.Row - 3 < f.MinRow {
|
|
|
|
|
if c.Row-3 < f.MinRow {
|
|
|
|
|
f.MinRow = c.Row - 3
|
|
|
|
|
}
|
|
|
|
|
if c.Row + 3 > f.MaxRow {
|
|
|
|
|
if c.Row+3 > f.MaxRow {
|
|
|
|
|
f.MaxRow = c.Row + 3
|
|
|
|
|
}
|
|
|
|
|
if c.Col - 3 < f.MinCol {
|
|
|
|
|
if c.Col-3 < f.MinCol {
|
|
|
|
|
f.MinCol = c.Col - 3
|
|
|
|
|
}
|
|
|
|
|
if c.Col + 3 > f.MaxCol {
|
|
|
|
|
if c.Col+3 > f.MaxCol {
|
|
|
|
|
f.MaxCol = c.Col + 3
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -337,7 +341,6 @@ func getTurnAsIfGoingFromLeft(directionFrom, directionTo Direction) []BorderSym
|
|
|
|
|
symbol = FromUp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// panic(fmt.Sprint("got strange from %s to %s", directionFrom.String(), directionTo.String()))
|
|
|
|
|
return []BorderSymbol{symbol}
|
|
|
|
|
}
|
|
|
|
|
@@ -346,27 +349,27 @@ func (f *Field) String() string {
|
|
|
|
|
s := "text 15,15 \""
|
|
|
|
|
|
|
|
|
|
for row := f.MinRow; row <= f.MaxRow; row++ {
|
|
|
|
|
rowChars := make([]rune, f.MaxCol - f.MinCol + 1)
|
|
|
|
|
rowChars := make([]rune, f.MaxCol-f.MinCol+1)
|
|
|
|
|
for col := f.MinCol; col <= f.MaxCol; col++ {
|
|
|
|
|
|
|
|
|
|
rowBords := f.BordersFromLeft[row]
|
|
|
|
|
if rowBords != nil {
|
|
|
|
|
bord, exists := rowBords[col]
|
|
|
|
|
if exists {
|
|
|
|
|
rowChars[col - f.MinCol] = rune(bord)
|
|
|
|
|
rowChars[col-f.MinCol] = rune(bord)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cell := f.Cells[Coord{col, row}]
|
|
|
|
|
if cell != nil && cell.ToBeDug {
|
|
|
|
|
rowChars[col - f.MinCol] = '@'
|
|
|
|
|
rowChars[col-f.MinCol] = '@'
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if f.isCellDug(row, col) {
|
|
|
|
|
rowChars[col - f.MinCol] = '#'
|
|
|
|
|
rowChars[col-f.MinCol] = '#'
|
|
|
|
|
} else {
|
|
|
|
|
rowChars[col - f.MinCol] = '.'
|
|
|
|
|
rowChars[col-f.MinCol] = '.'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -376,14 +379,36 @@ func (f *Field) String() string {
|
|
|
|
|
s += "\""
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
func (f *Field) digInsides() (countInside int) {
|
|
|
|
|
func (f *Field) digInsides() (result int) {
|
|
|
|
|
lineSum := make(chan int)
|
|
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
rowsCount := f.MaxRow - f.MinRow
|
|
|
|
|
wg.Add(rowsCount)
|
|
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
wg.Wait()
|
|
|
|
|
close(lineSum)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
for rowInternalCount := range lineSum {
|
|
|
|
|
result += rowInternalCount
|
|
|
|
|
}
|
|
|
|
|
close(done)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
for row := f.MinRow; row < f.MaxRow; row++ {
|
|
|
|
|
if row % 10000 == 0 {
|
|
|
|
|
go func(row int){
|
|
|
|
|
if row%10000 == 0 {
|
|
|
|
|
log.Printf("processed rows %d out of %d", row, f.MaxRow)
|
|
|
|
|
}
|
|
|
|
|
specialBorders := f.BordersFromLeft[row]
|
|
|
|
|
if len(specialBorders) == 0 {
|
|
|
|
|
continue
|
|
|
|
|
wg.Done()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
type BorderItem struct {
|
|
|
|
|
border BorderSymbol
|
|
|
|
|
@@ -425,13 +450,14 @@ func (f *Field) digInsides() (countInside int) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if bordersCrossed % 2 == 1 { // is in
|
|
|
|
|
for col := prevBorder.col+1; col < specialBorder.col; col++ {
|
|
|
|
|
if bordersCrossed%2 == 1 { // is in
|
|
|
|
|
for col := prevBorder.col + 1; col < specialBorder.col; col++ {
|
|
|
|
|
// f.Cells[Coord{Col: col, Row: row}] = &Cell{
|
|
|
|
|
// ToBeDug: true,
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
countInside += diff
|
|
|
|
|
lineSum <- diff
|
|
|
|
|
// countInside += diff
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if specialBorder.border == Vertical {
|
|
|
|
|
@@ -440,8 +466,14 @@ func (f *Field) digInsides() (countInside int) {
|
|
|
|
|
|
|
|
|
|
prevBorder = specialBorder
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wg.Done()
|
|
|
|
|
}(row)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
<-done
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// func (f *Field) digInsides() (countInside int) {
|
|
|
|
|
|