Hamikadze

Untitled

Dec 26th, 2018
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using RetroGUI;
  6.  
  7. /* Duparc's method, as it's meant to be done.
  8.  * Starting from all possible positions ( a list of length 2^(2*width+4))
  9.  * additional lines are generated that match.
  10.  * Supports patterns with width up to 31-2!
  11.  * Additionally, TODO: Version with own checker of patterns.
  12.  * Probably could be sped up in the generating of next line by knowing the first thing about the Game of Life.
  13.  */
  14.  
  15. /* Amount of memory starts at 4*2^(2*width+4) bytes*/
  16.  
  17. namespace RetroParlor
  18. {
  19.    
  20.     public class real_Duparc:FormRoutines //DOESN'T inherit from BaseRoutines!
  21.     {
  22.         byte[,] goal;
  23.         bool hasOrphans=true; //not used yet
  24.         byte testwidth;
  25.         byte width;
  26.         int numRows; //why would anyone try to atavise anything longer than 256?
  27.         //stupid question, EVERYBODY wants to!
  28.         uint maxnum;
  29.         uint maxResults;
  30.         uint numResults;
  31.         Method touse;
  32.  
  33.         public List<byte[,]> foundPositions;
  34.  
  35.         public List<uint[]> lastRow = new List<uint[]>();
  36.         public List<uint[]> thisRow = new List<uint[]>();
  37.  
  38.         public real_Duparc()
  39.         {
  40.         }
  41.  
  42.         public void TestMe(byte[,] goalpos, bool OrphanQ, Method method, uint maxresults)
  43.         {
  44.             FullInitialize(goalpos,OrphanQ,method,maxresults);
  45.             Atavise();
  46.         }
  47.  
  48.         public void FullInitialize(byte[,] goalpos, bool OrphanQ,Method method,uint maxresults)
  49.         {
  50.             hasOrphans = OrphanQ;
  51.             goal = goalpos;
  52.             width = (byte)goal.GetLength(0);
  53.             testwidth = (byte)(width + 2);
  54.             numRows = goal.GetLength(1);
  55.             maxnum = Pow2(testwidth);
  56.             numResults = 0;
  57.             maxResults = maxresults;
  58.             touse = method;
  59.         }
  60.  
  61.         public void Atavise()
  62.         {
  63.             foundPositions = new List<byte[,]>();
  64.             if (touse == Method.BFS)
  65.             {
  66.                 AtaviseBFS();
  67.             }
  68.             else
  69.             {
  70.                 AtaviseDFS();
  71.             }
  72.         }
  73.  
  74.         public void AtaviseBFS()
  75.         {
  76.             //prepare for row1
  77.             AppendLineToBuffer("Preparing...");
  78.             lastRow = new List<uint[]>();
  79.             thisRow = new List<uint[]>();
  80.             for (uint foo = 0; foo < maxnum; foo++)
  81.             {
  82.                 for (uint bar = 0; bar < maxnum; bar++)
  83.                 {
  84.                     thisRow.Add(new uint[2] { foo, bar });
  85.                 }
  86.             }
  87.  
  88.             AppendLineToBuffer("Running...");
  89.             byte[] row;
  90.             int num;
  91.             for (int l = 0; l < numRows; l++)
  92.             {
  93.                 AppendLineToBuffer("Row +"+ l.ToString());
  94.                 lastRow = new List<uint[]>(thisRow);
  95.                 thisRow = new List<uint[]>();
  96.                 row = GetRow(l);
  97.                 num = 0;
  98.                 foreach (uint[] testcase in lastRow)
  99.                 {
  100.                     GenerateNexts(testcase,Unencode(testcase[l]), Unencode(testcase[l + 1]),row);
  101.                     num++;
  102.                     SetProgress((float)l / numRows + (float)num / (lastRow.Count * numRows));
  103.                 }
  104.                 AppendLineToBuffer("Finished line" + l.ToString());
  105.             }
  106.             AppendLineToBuffer("Reconstructing lines...");
  107.             foreach (uint[] path in thisRow)
  108.             {
  109.                 ReconstructPath(path);
  110.             }
  111.         }
  112.  
  113.         public void AtaviseDFS()
  114.         {
  115.             //prepare for row1
  116.             AppendLineToBuffer("Preparing...");
  117.             for (uint foo = 0; foo < maxnum; foo++)
  118.             {
  119.                 for (uint bar = 0; bar < maxnum; bar++)
  120.                 {
  121.                     ComputeDFS(new uint[2] { foo, bar },0);
  122.                     SetProgress((float)(foo*maxnum+ bar) / (maxnum * maxnum));
  123.                 }
  124.             }
  125.         }
  126.  
  127.         private void ComputeDFS(uint[] path, int level)
  128.         {
  129.             if (numResults == maxResults)
  130.             {
  131.                 return;
  132.             }
  133.             if (level == numRows)
  134.             {
  135.                 ReconstructPath(path);
  136.                 return;
  137.             }
  138.             List<uint[]> myRows = new List<uint[]>();
  139.             byte[] row = GetRow(level);
  140.             myRows = GenerateNexts4DFS(path, Unencode(path[level]), Unencode(path[level + 1]),row);
  141.             foreach (uint[] newpath in myRows)
  142.             {
  143.                 if (numResults == maxResults)
  144.                 {
  145.                     return;
  146.                 }
  147.                 ComputeDFS(newpath, level + 1);
  148.             }
  149.         }
  150.  
  151.         /// <summary>
  152.         /// Reconstructs a pattern from a path and adds it to foundResults
  153.         /// </summary>
  154.         /// <param name="path"></param>
  155.         private void ReconstructPath(uint[] path)
  156.         {
  157.             byte[,] temp = new byte[testwidth, path.Length];
  158.             byte[] bits;
  159.             for (int y = 0; y < path.Length; y++)
  160.             {
  161.                 bits = Unencode(path[y]);
  162.                 for (int x = 0; x < testwidth; x++)
  163.                 {
  164.                     temp[x, y] = bits[x];
  165.                 }
  166.             }
  167.             foundPositions.Add(temp);
  168.             numResults++;
  169.         }
  170.  
  171.  
  172.         /// <summary>
  173.         /// Generates additions to thisRow given the previous result and the one before that.
  174.         /// </summary>
  175.         /// <param name="lastRow"></param>
  176.         /// <param name="thisRow"></param>
  177.         private void GenerateNexts(uint[] previousRows, byte[] lRow, byte[] tRow, byte[] row)
  178.         {
  179.             //TODO: Make this use a BFS or DFS search!
  180.             //Right now it's *really* dumb!
  181.  
  182.             byte[] testrow;
  183.             int nsum;
  184.             bool isgood; //yes I'm actually trying to avoid gotos here isn't this good?
  185.             int c; //WHYYYYY
  186.  
  187.             for (uint i = 0; i < maxnum; i++)
  188.             {
  189.                 testrow = Unencode(i);
  190.                 //custom version of run1step
  191.                 isgood=true;
  192.                 for (int t = 0; t < width ; t++)
  193.                 {
  194.                     nsum = lRow[t] + lRow[t + 1] + lRow[t + 2] + tRow[t] + tRow[t + 2] + testrow[t] + testrow[t + 1] + testrow[t + 2];
  195.                     c = ((nsum == 3 || (tRow[t + 1] == 1 && nsum == 2)) ? 1 : 0);
  196.                     if(c !=row[t]){
  197.                         isgood=false;
  198.                         break;
  199.                     }
  200.                 }
  201.                 if(isgood){
  202.                     thisRow.Add(AppendOnto(previousRows, i));
  203.                 }
  204.             }
  205.         }
  206.  
  207.         private List<uint[]> GenerateNexts4DFS(uint[] previousRows, byte[] lRow, byte[] tRow, byte[] row)
  208.         {
  209.             //TODO: Make this use a BFS or DFS search!
  210.             //Right now it's *really* dumb!
  211.  
  212.             byte[] testrow;
  213.             int nsum;
  214.             bool isgood; //yes I'm actually trying to avoid gotos here isn't this good?
  215.             int c; //WHYYYYY
  216.             List<uint[]> resultRows = new List<uint[]>();
  217.  
  218.             for (uint i = 0; i < maxnum; i++)
  219.             {
  220.                 testrow = Unencode(i);
  221.                 //custom version of run1step
  222.                 isgood = true;
  223.                 for (int t = 0; t < width; t++)
  224.                 {
  225.                     nsum = lRow[t] + lRow[t + 1] + lRow[t + 2] + tRow[t] + tRow[t + 2] + testrow[t] + testrow[t + 1] + testrow[t + 2];
  226.                     c = ((nsum == 3 || (tRow[t + 1] == 1 && nsum == 2)) ? 1 : 0);
  227.                     if (c != row[t])
  228.                     {
  229.                         isgood = false;
  230.                         break;
  231.                     }
  232.                 }
  233.                 if (isgood)
  234.                 {
  235.                     resultRows.Add(AppendOnto(previousRows, i));
  236.                 }
  237.             }
  238.             return resultRows;
  239.         }
  240.  
  241.         public uint Pow2(byte pow)
  242.         {
  243.             return (uint)(1 << pow);
  244.         }
  245.  
  246.         public byte[] Unencode(uint num)
  247.         {
  248.             //also, reverses!
  249.             byte[] result = new byte[32];
  250.             for (int l = 0; l <32; l++)
  251.             {
  252.                 result[l] = (byte)(num % 2);
  253.                 num=num >> 1;
  254.             }
  255.             return result;
  256.         }
  257.  
  258.         public uint Encode(byte[] pat)
  259.         {
  260.             uint num = 0;
  261.             for (int n = 0; n < 32; n++)
  262.             {
  263.                 num = (num << 1) + pat[n];
  264.             }
  265.             return num;
  266.         }
  267.  
  268.         public byte[] GetRow(int i)
  269.         {
  270.             byte[] result = new byte[width];
  271.             for (int x = 0; x < width; x++)
  272.             {
  273.                 result[x] = goal[x, i];
  274.             }
  275.             return result;
  276.         }
  277.  
  278.         public uint[] AppendOnto(uint[] a, uint b)
  279.         {
  280.             uint[] res = new uint[a.GetLength(0) + 1];
  281.             a.CopyTo(res, 0);
  282.             res[a.GetLength(0)] = b;
  283.             return res;
  284.         }
  285.  
  286.        
  287.     }
  288. }
  289.  
  290.  
  291. /*        ____________________________________________
  292.  *        |                                          |
  293.  *        |  ______________________________________  |
  294.  *        | |                                      | |
  295.  *        | |  ___________________________________ | |
  296.  *        | | |                                  | | |
  297.  *        | | |  ______________________________  | | |
  298.  *        | | |  | TOO MANY BOXES             |  | | |
  299.  *        | | |  |____________________________|  | | |
  300.  *        |__------------------------------------_____
  301.  */
Advertisement
Add Comment
Please, Sign In to add comment