day21: choking on example

This commit is contained in:
efim 2023-12-21 09:27:41 +00:00
parent f5ea9e725e
commit 9a22efd4b3
1 changed files with 76 additions and 20 deletions

View File

@ -13,13 +13,24 @@ func Run() int {
field := ReadField(filename)
log.Print(field)
reachableBySixtyFour := field.ReachableBySteps(64, map[Coord]any{
// for i := 6; i <= 10; i++ {
// reachableBySteps := field.ReachableBySteps(i, map[Coord]any{
// Coord{Row: field.RowStart, Col: field.ColStart}: struct{}{},
// })
// log.Print("reachable after steps : ", i, len(reachableBySteps))
// field.PrintCoord(reachableBySteps, 1)
// }
steps := 1000
reachableBySteps := field.ReachableBySteps(steps, map[Coord]any{
Coord{Row: field.RowStart, Col: field.ColStart}: struct{}{},
})
fmt.Println(field.PrintCoord(reachableBySixtyFour))
log.Print("reachable after steps : ", steps, len(reachableBySteps))
field.PrintCoord(reachableBySteps, 1)
return len(reachableBySixtyFour)
return len(reachableBySteps)
}
// let's do dijkstra?
@ -34,9 +45,13 @@ type Field struct {
type Coord struct {
Row, Col int
FieldRow, FieldCol int // 0 by default is good
}
func (f Field) ReachableBySteps(n int, startingAt map[Coord]any) map[Coord]any {
if n % 100 == 0 {
log.Println("going step: ", n)
}
if n == 0 {
return startingAt
}
@ -54,10 +69,35 @@ func (f Field) ReachableBySteps(n int, startingAt map[Coord]any) map[Coord]any {
func (f Field) Neighbors(c Coord) (resut []Coord) {
closeCoords := []Coord{
{Row: c.Row + 1, Col: c.Col},
{Row: c.Row - 1, Col: c.Col},
{Row: c.Row, Col: c.Col + 1},
{Row: c.Row, Col: c.Col - 1},
{Row: c.Row + 1, Col: c.Col, FieldRow: c.FieldRow, FieldCol: c.FieldCol},
{Row: c.Row - 1, Col: c.Col, FieldRow: c.FieldRow, FieldCol: c.FieldCol},
{Row: c.Row, Col: c.Col + 1, FieldRow: c.FieldRow, FieldCol: c.FieldCol},
{Row: c.Row, Col: c.Col - 1, FieldRow: c.FieldRow, FieldCol: c.FieldCol},
}
for i, close := range closeCoords {
height := len(f.symbols)
width := len(f.symbols[0])
if close.Row == height {
close.Row = 0
close.FieldRow += 1
}
if close.Row == -1 {
close.Row = height - 1
close.FieldRow -= 1
}
if close.Col == width {
close.Col = 0
close.FieldCol += 1
}
if close.Col == -1 {
// log.Printf("moving COL to lefter field from %d to %d", close.Col, width-1)
close.Col = width - 1
close.FieldCol -= 1
}
closeCoords[i] = close
// but this is not it. i need to store the XX and YY
// so that points in other 'fields' would count separately. yuk
}
for _, close := range closeCoords {
@ -78,7 +118,12 @@ func (f Field) Neighbors(c Coord) (resut []Coord) {
func (f Field) ValidCoord(row, col int) bool {
// log.Print("check valid ", row, col, row >= 0 && row < len(f.symbols) && col >= 0 && col < len(f.symbols[0]))
return row >= 0 && row < len(f.symbols) && col >= 0 && col < len(f.symbols[0])
valid := row >= 0 && row < len(f.symbols) && col >= 0 && col < len(f.symbols[0])
if !valid {
panic(fmt.Sprint("getting invalid coord: ", row, col))
}
return valid
}
func (f Field) String() (result string) {
@ -113,19 +158,30 @@ func ReadField(filename string) (result Field) {
return
}
func (f Field) PrintCoord(coords map[Coord]any) string {
result := ""
func (f Field) PrintCoord(coords map[Coord]any, expandByField int) {
for fieldRow := -expandByField; fieldRow <= expandByField; fieldRow++ {
lines := make([]string, len(f.symbols))
for fieldCol := -expandByField; fieldCol <= expandByField; fieldCol++ {
for rowNum, row := range f.symbols {
for colNum, col := range row {
_, marked := coords[Coord{Row: rowNum, Col: colNum}]
_, marked := coords[Coord{Row: rowNum, Col: colNum,
FieldRow: fieldRow, FieldCol: fieldCol}]
if marked {
result += "O"
lines[rowNum] += "O"
} else {
result += string(col)
lines[rowNum] += string(col)
}
}
result += "\n"
}
return result
}
for _, line := range lines {
fmt.Println(line)
}
}
return
}