Advertisement
HEX0x29A

g853brute

Aug 16th, 2013
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.09 KB | None | 0 0
  1. program g853brute;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   Windows, Messages;
  7.  
  8. const
  9.   capacity : array [1..3] of byte = (8, 5, 3);
  10.  
  11. var
  12.   F        : TextFile;
  13.   Finished : bool = False;
  14.   val0     : byte = 0;
  15.   val1     : byte = 0;
  16.   canisters: array [1..3] of byte = (8, 0, 0);
  17.  
  18. procedure ProcessMessages;
  19. var
  20.   Msg: TMsg;
  21.   function ProcessMsg(var Msg: TMsg): Boolean;
  22.   begin
  23.     Result := False;
  24.     if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
  25.     begin
  26.       Result := True;
  27.       if Msg.Message <> WM_QUIT then
  28.       begin
  29.         TranslateMessage(Msg);
  30.         DispatchMessage(Msg);
  31.       end
  32.       else
  33.         DispatchMessage(Msg);
  34.     end;
  35.   end;
  36. begin
  37.   while ProcessMsg(Msg) do;
  38. end;
  39.  
  40. function MoveFluid(MoveFrom, MoveTo: byte): bool;
  41. var
  42.   summ: byte;
  43. begin
  44.   Result := False;
  45.   if (MoveFrom <> MoveTo)and(capacity[MoveTo] <> canisters[MoveTo])
  46.      and(canisters[MoveFrom] <> 0) then
  47.   begin
  48.     summ := canisters[MoveTo] + canisters[MoveFrom];
  49.     if canisters[MoveFrom] <= (capacity[MoveTo] - canisters[MoveTo]) then
  50.     begin
  51.       canisters[MoveTo] := summ;
  52.       canisters[MoveFrom] := 0;
  53.     end else begin
  54.       canisters[MoveTo] := capacity[MoveTo];
  55.       canisters[MoveFrom] := summ - canisters[MoveTo];
  56.     end;
  57.     Result := True;
  58.     if (canisters[1] = 8)and(canisters[2] = 0)and(canisters[3] = 0) then
  59.       Rewrite(F)
  60.     else
  61.       Write(F, MoveFrom, ' -> ', MoveTo, ' ');
  62.     Write(MoveFrom, ' -> ', MoveTo, ' ');
  63.     for summ := 1 to 3 do begin
  64.       Write('[', canisters[summ], '] ');
  65.       Write(F, '[', canisters[summ], '] ');
  66.     end;
  67.     WriteLn;
  68.   end;
  69.   Finished := (canisters[1] = 4)and(canisters[2] = 4)and(canisters[3] = 0);
  70. end;
  71.  
  72. begin
  73.   Randomize;
  74.   AssignFile(F, 'result.log');
  75.   Rewrite(F);
  76.   repeat
  77.     repeat
  78.       if Finished then break;
  79.       val0 := Random(3) + 1;
  80.       val1 := val0;
  81.       while val0 = val1 do
  82.         val1 := Random(3) + 1;
  83.     until MoveFluid(val0, val1);
  84.     ProcessMessages;
  85.   until Finished;
  86.   CloseFile(F);
  87.   MessageBox(0, 'Finished', 'Info', MB_ICONINFORMATION);
  88.   ExitProcess(0);
  89. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement