Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program g853brute;
- {$APPTYPE CONSOLE}
- uses
- Windows, Messages;
- const
- capacity : array [1..3] of byte = (8, 5, 3);
- var
- F : TextFile;
- Finished : bool = False;
- val0 : byte = 0;
- val1 : byte = 0;
- canisters: array [1..3] of byte = (8, 0, 0);
- procedure ProcessMessages;
- var
- Msg: TMsg;
- function ProcessMsg(var Msg: TMsg): Boolean;
- begin
- Result := False;
- if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
- begin
- Result := True;
- if Msg.Message <> WM_QUIT then
- begin
- TranslateMessage(Msg);
- DispatchMessage(Msg);
- end
- else
- DispatchMessage(Msg);
- end;
- end;
- begin
- while ProcessMsg(Msg) do;
- end;
- function MoveFluid(MoveFrom, MoveTo: byte): bool;
- var
- summ: byte;
- begin
- Result := False;
- if (MoveFrom <> MoveTo)and(capacity[MoveTo] <> canisters[MoveTo])
- and(canisters[MoveFrom] <> 0) then
- begin
- summ := canisters[MoveTo] + canisters[MoveFrom];
- if canisters[MoveFrom] <= (capacity[MoveTo] - canisters[MoveTo]) then
- begin
- canisters[MoveTo] := summ;
- canisters[MoveFrom] := 0;
- end else begin
- canisters[MoveTo] := capacity[MoveTo];
- canisters[MoveFrom] := summ - canisters[MoveTo];
- end;
- Result := True;
- if (canisters[1] = 8)and(canisters[2] = 0)and(canisters[3] = 0) then
- Rewrite(F)
- else
- Write(F, MoveFrom, ' -> ', MoveTo, ' ');
- Write(MoveFrom, ' -> ', MoveTo, ' ');
- for summ := 1 to 3 do begin
- Write('[', canisters[summ], '] ');
- Write(F, '[', canisters[summ], '] ');
- end;
- WriteLn;
- end;
- Finished := (canisters[1] = 4)and(canisters[2] = 4)and(canisters[3] = 0);
- end;
- begin
- Randomize;
- AssignFile(F, 'result.log');
- Rewrite(F);
- repeat
- repeat
- if Finished then break;
- val0 := Random(3) + 1;
- val1 := val0;
- while val0 = val1 do
- val1 := Random(3) + 1;
- until MoveFluid(val0, val1);
- ProcessMessages;
- until Finished;
- CloseFile(F);
- MessageBox(0, 'Finished', 'Info', MB_ICONINFORMATION);
- ExitProcess(0);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement