From 9a22efd4b377706881872a1028c6cdb9b80e401f Mon Sep 17 00:00:00 2001 From: efim Date: Thu, 21 Dec 2023 09:27:41 +0000 Subject: [PATCH] day21: choking on example --- day21/stepCounter.go | 96 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 76 insertions(+), 20 deletions(-) diff --git a/day21/stepCounter.go b/day21/stepCounter.go index af48cb1..7178fb4 100644 --- a/day21/stepCounter.go +++ b/day21/stepCounter.go @@ -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? @@ -33,10 +44,14 @@ type Field struct { } type Coord struct { - Row, Col int + 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 := "" - for rowNum, row := range f.symbols { - for colNum, col := range row { - _, marked := coords[Coord{Row: rowNum, Col: colNum}] - if marked { - result += "O" - } else { - result += string(col) +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, + FieldRow: fieldRow, FieldCol: fieldCol}] + if marked { + lines[rowNum] += "O" + } else { + lines[rowNum] += string(col) + } + } } + } - result += "\n" + for _, line := range lines { + fmt.Println(line) + } + } - return result + return }