day14, example two
This commit is contained in:
parent
709f4c0532
commit
149b753d22
|
@ -16,14 +16,41 @@ func Run() int {
|
||||||
// fmt.Printf("> lines for field %+v\n", field.UpIndices())
|
// fmt.Printf("> lines for field %+v\n", field.UpIndices())
|
||||||
// field.Move(field.Height(), field.UpIndices())
|
// field.Move(field.Height(), field.UpIndices())
|
||||||
cycles := 1000000000
|
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()
|
field.DoSpinCycle()
|
||||||
// fmt.Println(field.String())
|
// 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 {
|
if i % 100000 == 0 {
|
||||||
log.Print("done ", i, " cycles")
|
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
|
// north rock load
|
||||||
return field.NorthLoad()
|
return field.NorthLoad()
|
||||||
|
|
Loading…
Reference in New Issue