diff --git a/day14/dayFourteen.go b/day14/dayFourteen.go index f10aa98..3b50663 100644 --- a/day14/dayFourteen.go +++ b/day14/dayFourteen.go @@ -16,14 +16,41 @@ func Run() int { // fmt.Printf("> lines for field %+v\n", field.UpIndices()) // field.Move(field.Height(), field.UpIndices()) cycles := 1000000000 - for i := 0; i < cycles; i++ { + states := make(map[string]int) + // 2023/12/14 11:50:32 >>> found loop. known state after 10 equal to one after 3 + + var loopLen, initialStretch int + + for i := 1; i <= cycles; i++ { field.DoSpinCycle() // fmt.Println(field.String()) + + stringRepr := field.String() + prevIter, known := states[stringRepr] + if known { + log.Printf(">>> found loop. known state after %d equal to one after %d", i, prevIter) + initialStretch = prevIter + loopLen = i - prevIter + break + } + + states[stringRepr] = i + if i % 100000 == 0 { log.Print("done ", i, " cycles") } } + // field is already in a 'loop' state. + // so we've already done 'initial stretch' so to make field in same state as after 'cycles' + // i only need to check rest of (cycles - initialStretch) + movesToMake := (cycles - initialStretch)%loopLen + log.Printf(">>> data: initial steps %d, loop len %d. to do same as %d iterations i need %d", initialStretch, loopLen, cycles, movesToMake) + + for i := 1; i <= movesToMake; i++ { + field.DoSpinCycle() + // fmt.Println(field.String()) + } // north rock load return field.NorthLoad()