day21: wrong answer AND slow. need to scrap

This commit is contained in:
efim 2023-12-21 12:36:07 +00:00
parent 4cb35dca33
commit b10a6250b1
1 changed files with 12 additions and 11 deletions

View File

@ -9,7 +9,7 @@ import (
func Run() (result int) { func Run() (result int) {
fmt.Print("hello day21") fmt.Print("hello day21")
filename := "day21/example" filename := "day21/input"
field := ReadField(filename) field := ReadField(filename)
log.Print(field) log.Print(field)
@ -25,14 +25,14 @@ func Run() (result int) {
// field.PrintCoord(reachableBySteps, 1) // field.PrintCoord(reachableBySteps, 1)
// } // }
steps := 50 steps := 26501365
reachableBySteps := field.ReachableBySteps( reachableBySteps := field.ReachableBySteps(
steps, steps,
map[FieldPoint]any{ map[FieldPoint]any{
FieldPoint{ FieldPoint{
InField: Coord{Row: field.RowStart, Col: field.ColStart}, InField: Coord{Row: field.RowStart, Col: field.ColStart},
}: struct{}{}}, }: struct{}{}},
make(map[Coord]any), make(map[Coord]int),
steps) steps)
result = reachableBySteps result = reachableBySteps
log.Print("reachable after steps : ", steps, result) log.Print("reachable after steps : ", steps, result)
@ -60,7 +60,7 @@ type FieldPoint struct {
MetaField Coord MetaField Coord
} }
func (f Field) ReachableBySteps(n int, startingAt map[FieldPoint]any, saturatedFields map[Coord]any, initialSteps int) (countReachable int) { func (f Field) ReachableBySteps(n int, startingAt map[FieldPoint]any, saturatedFields map[Coord]int, initialSteps int) (countReachable int) {
if n%100 == 0 { if n%100 == 0 {
log.Println("going step: ", n) log.Println("going step: ", n)
} }
@ -78,7 +78,7 @@ func (f Field) ReachableBySteps(n int, startingAt map[FieldPoint]any, saturatedF
} }
} }
return sizeOfUnsaturated return sizeOfUnsaturated + sizeOfSaturated
} }
// else collect directly available // else collect directly available
@ -97,21 +97,22 @@ func (f Field) ReachableBySteps(n int, startingAt map[FieldPoint]any, saturatedF
for workedUponFieldCoord, amount := range metaFields { for workedUponFieldCoord, amount := range metaFields {
isEven := FieldIsInEven(initialSteps, n, workedUponFieldCoord) isEven := FieldIsInEven(initialSteps, n, workedUponFieldCoord)
if workedUponFieldCoord.Col == 0 && workedUponFieldCoord.Row == 0 { if workedUponFieldCoord.Col == 0 && workedUponFieldCoord.Row == 0 {
log.Printf("checking %+v : %d as worked fields for saturation. isEven %t", workedUponFieldCoord, amount, isEven) // log.Printf("checking %+v : %d as worked fields for saturation. isEven %t", workedUponFieldCoord, amount, isEven)
} }
if isEven && amount == f.SaturatedEvenCount { if isEven && amount == f.SaturatedEvenCount {
log.Printf(">>> adding %+v to saturated, with amount %d\n", workedUponFieldCoord, amount) log.Printf(">>> adding %+v to saturated, with amount %d\n", workedUponFieldCoord, amount)
saturatedFields[workedUponFieldCoord] = struct{}{} saturatedFields[workedUponFieldCoord] = n
} }
if !isEven && amount == f.SaturatedOddCount { if !isEven && amount == f.SaturatedOddCount {
log.Printf(">>> adding %+v to saturated, with amount %d\n", workedUponFieldCoord, amount) log.Printf(">>> adding %+v to saturated, with amount %d\n", workedUponFieldCoord, amount)
saturatedFields[workedUponFieldCoord] = struct{}{} saturatedFields[workedUponFieldCoord] = n
} }
} }
for point := range oneStepExpanded { for point := range oneStepExpanded {
_, fromSaturated := saturatedFields[point.MetaField] saturatedAtStep, fromSaturated := saturatedFields[point.MetaField]
if fromSaturated { // hack. to not remove points from saturated fields too early
if fromSaturated && (saturatedAtStep - n > 200) {
delete(oneStepExpanded, point) delete(oneStepExpanded, point)
} }
} }
@ -139,7 +140,7 @@ func FieldIsInEven(initialSteps, currentSteps int, metaCoord Coord) bool {
} }
} }
func (f Field) Neighbors(c FieldPoint, saturatedFields map[Coord]any) (resut []FieldPoint) { func (f Field) Neighbors(c FieldPoint, saturatedFields map[Coord]int) (resut []FieldPoint) {
closeCoords := []FieldPoint{ closeCoords := []FieldPoint{
{InField: Coord{Row: c.InField.Row + 1, Col: c.InField.Col}, MetaField: c.MetaField}, {InField: Coord{Row: c.InField.Row + 1, Col: c.InField.Col}, MetaField: c.MetaField},
{InField: Coord{Row: c.InField.Row - 1, Col: c.InField.Col}, MetaField: c.MetaField}, {InField: Coord{Row: c.InField.Row - 1, Col: c.InField.Col}, MetaField: c.MetaField},