SimSet begin boolean Debugging = false; Head class Group(Capacity, Suc); ref(Group) Suc; integer Capacity; begin procedure Clear; begin while Last =/= none do Last.Into(Unused); end Clear; end Group; Link class Ball(N); integer N; begin procedure PutInto(G); ref(Group) G; begin if G.Cardinal < G.Capacity then begin Into(G); if Debugging then Dump; end else begin G.Clear; if G.Suc == none then begin Into(Unused); Days := Days+1; if Debugging then Dump; if Sorted(Unused) then Done := true; end else begin PutInto(G.Suc); end if; end if; end PutInto; end Ball; procedure Dump; begin procedure DumpQueue(H, Title); ref(Head) H; text Title; begin ref(Ball) B; OutText(Title & ": "); B :- H.First; while B =/= none do begin OutText(" "); OutInt(B.N, 0); B :- B.Suc; end while; OutImage; end DumpQueue; OutImage; DumpQueue(Hours, " Hours"); DumpQueue(Min5, " 5 min"); DumpQueue(Min1, " 1 min"); DumpQueue(Unused, "unused"); end Dump; boolean procedure Sorted(H); ref(Head) H; begin ref(Ball) B1, B2; Sorted := true; B1 :- H.First; B2 :- if B1=/=none then B1.Suc else none; while B2 =/= none do begin if B1.N > B2.N then Sorted := false; B1 :- B2; B2 :- B2.Suc; end while; end Sorted; ref(Head) Unused; ref(Group) Hours, Min5, Min1; integer Days; boolean Done; integer procedure Solve(N_balls); integer N_balls; begin ref(Ball) B; integer I; Unused :- new Head; Hours :- new Group(11,none); Min5 :- new Group(11,Hours); Min1 :- new Group(4,Min5); for I := 1 step 1 until N_balls do new Ball(I).Into(Unused); Done := false; Days := 0; while not Done do begin B :- Unused.First; B.Out; B.PutInto(Min1); end while; Solve := Days; end Solve; inspect new InFile("balls.in") do begin Open(Blanks(256)); inspect new OutFile("balls.out") do begin integer N, I, B, Res; Open(Blanks(80)); for B := InInt while B > 0 do begin Res := Solve(B); OutInt(B, 0); OutText(" balls cycle after "); OutInt(Res//2, 0); OutText(" days."); OutImage; if Debugging then begin inspect SysOut do begin OutText("Solution found for "); OutInt(B, 0); OutText(" balls after "); OutInt(Res, 0); OutText(" 12-hour periods."); OutImage; end inspect; end if; end for; Close; end inspect; Close; end inspect; end