Got part 2 in the end! The intuition that each axis is independent came to me while I was away from the keyboard!
defsimulate_until_repeat(moons:List[Moon])->int:"""
Simulate `moons` until a repeated state is seen. Returns the
number of simulation steps to get to that state
"""defgcd(x:int,y:int)->int:returnyifx==0elsegcd(y%x,x)defloop_size_for_axis(moons:List[Moon],axis)->int:defstate(m:List[Moon])->str:returnstr(list(axis(x)forxinm))states=set(state(moons))forn,minenumerate(simulate_seq(moons)):s=state(m)ifsinstates:returnnstates.add(s)defaxis_x(m):return(m.pos.x,m.velocity.x)defaxis_y(m):return(m.pos.y,m.velocity.y)defaxis_z(m):return(m.pos.z,m.velocity.z)overall_loop=1forain[axis_x,axis_y,axis_z]:loop=loop_size_for_axis(moons,a)overall_loop=overall_loop*loop//gcd(loop,overall_loop)returnoverall_loop
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Got part 2 in the end! The intuition that each axis is independent came to me while I was away from the keyboard!