day21: wrong answer AND slow. need to scrap
This commit is contained in:
parent
4cb35dca33
commit
b10a6250b1
|
@ -9,7 +9,7 @@ import (
|
|||
|
||||
func Run() (result int) {
|
||||
fmt.Print("hello day21")
|
||||
filename := "day21/example"
|
||||
filename := "day21/input"
|
||||
field := ReadField(filename)
|
||||
log.Print(field)
|
||||
|
||||
|
@ -25,14 +25,14 @@ func Run() (result int) {
|
|||
// field.PrintCoord(reachableBySteps, 1)
|
||||
// }
|
||||
|
||||
steps := 50
|
||||
steps := 26501365
|
||||
reachableBySteps := field.ReachableBySteps(
|
||||
steps,
|
||||
map[FieldPoint]any{
|
||||
FieldPoint{
|
||||
InField: Coord{Row: field.RowStart, Col: field.ColStart},
|
||||
}: struct{}{}},
|
||||
make(map[Coord]any),
|
||||
make(map[Coord]int),
|
||||
steps)
|
||||
result = reachableBySteps
|
||||
log.Print("reachable after steps : ", steps, result)
|
||||
|
@ -60,7 +60,7 @@ type FieldPoint struct {
|
|||
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 {
|
||||
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
|
||||
|
||||
|
@ -97,21 +97,22 @@ func (f Field) ReachableBySteps(n int, startingAt map[FieldPoint]any, saturatedF
|
|||
for workedUponFieldCoord, amount := range metaFields {
|
||||
isEven := FieldIsInEven(initialSteps, n, workedUponFieldCoord)
|
||||
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 {
|
||||
log.Printf(">>> adding %+v to saturated, with amount %d\n", workedUponFieldCoord, amount)
|
||||
saturatedFields[workedUponFieldCoord] = struct{}{}
|
||||
saturatedFields[workedUponFieldCoord] = n
|
||||
}
|
||||
if !isEven && amount == f.SaturatedOddCount {
|
||||
log.Printf(">>> adding %+v to saturated, with amount %d\n", workedUponFieldCoord, amount)
|
||||
saturatedFields[workedUponFieldCoord] = struct{}{}
|
||||
saturatedFields[workedUponFieldCoord] = n
|
||||
}
|
||||
}
|
||||
|
||||
for point := range oneStepExpanded {
|
||||
_, fromSaturated := saturatedFields[point.MetaField]
|
||||
if fromSaturated {
|
||||
saturatedAtStep, fromSaturated := saturatedFields[point.MetaField]
|
||||
// hack. to not remove points from saturated fields too early
|
||||
if fromSaturated && (saturatedAtStep - n > 200) {
|
||||
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{
|
||||
{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},
|
||||
|
|
Loading…
Reference in New Issue