Code test

July 30, 2009

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5:  
   6: namespace EnigmaPaintings
   7: {
   8:     public class PaintingEnigma
   9:     {
  10:         string[,] iter = new string[3,3];
  11:         public PaintingEnigma()
  12:         {
  13:             int[] al = new int[512];
  14:             for (int j = 0; j < 512; j++)
  15:             {
  16:  
  17:                 string l = string.Format("000000000{0}", Convert.ToString(j, 2));
  18:                 l = l.Substring(l.Length - 9, 9);
  19:                 Console.WriteLine(l.Substring(0,3));
  20:                 Console.WriteLine(l.Substring(3, 3));
  21:                 Console.WriteLine(l.Substring(6, 3));
  22:                 Console.WriteLine(getScore(l));
  23:                 al[j] = getScore(l);
  24:             }
  25:  
  26:             int countis1 = 0;
  27:             int countis4 = 0;
  28:             int countis6 = 0;
  29:  
  30:             for (int j = 0; j < 512; j++)
  31:             {
  32:                 if (al[j] == 1)
  33:                     countis1++;
  34:  
  35:                 if (al[j] == 4)
  36:                     countis4++;
  37:  
  38:                 if (al[j] == 6)
  39:                     countis6++;
  40:             }
  41:  
  42:             Console.WriteLine("paintings that have a score of 1:{0}", countis1);
  43:             Console.WriteLine("paintings that have a score of 4:{0}", countis4);
  44:             Console.WriteLine("paintings that have a score of 6:{0}", countis6);
  45:  
  46:  
  47:         }
  48:         private int getScore(string g)
  49:         {
  50:             string r1 = g.Substring(0, 3);
  51:             string r2 = g.Substring(3, 3);
  52:             string r3 = g.Substring(6, 3);
  53:             return getScore(r1, r2, r3);
  54:         }
  55:         private int getScore(string r1, string r2, string r3)
  56:         {
  57:             int a = Convert.ToInt32(r1.Substring(0, 1));
  58:             int b = Convert.ToInt32(r1.Substring(1, 1));
  59:             int c = Convert.ToInt32(r1.Substring(2, 1));
  60:             int d = Convert.ToInt32(r2.Substring(0, 1));
  61:             int e = Convert.ToInt32(r2.Substring(1, 1));
  62:             int f = Convert.ToInt32(r2.Substring(2, 1));
  63:             int g = Convert.ToInt32(r3.Substring(0, 1));
  64:             int h = Convert.ToInt32(r3.Substring(1, 1));
  65:             int i = Convert.ToInt32(r3.Substring(2, 1));
  66:             return getScore(a, b, c, d, e, f, g, h, i);
  67:  
  68:         }
  69:         private int getScore(int a, int b, int c, int d, int e, int f, int g, int h, int i)
  70:         {
  71:             string[,] s = new string[3, 3];
  72:             s[0, 0] = a.ToString();
  73:             s[0, 1] = b.ToString();
  74:             s[0, 2] = c.ToString();
  75:             s[1, 0] = d.ToString();
  76:             s[1, 1] = e.ToString();
  77:             s[1, 2] = f.ToString();
  78:             s[2, 0] = g.ToString();
  79:             s[2, 1] = h.ToString();
  80:             s[2, 2] = i.ToString();
  81:             return getScore(s);
  82:         }
  83:         private int getScore(string[,] s)
  84:         {
  85:             int [] scores = new int [6];
  86:  
  87:             scores[0] = Convert.ToInt32(string.Format("{0}{1}{2}", s[0, 0], s[0, 1], s[0, 2]), 2);
  88:             scores[1] = Convert.ToInt32(string.Format("{0}{1}{2}", s[1, 0], s[1, 1], s[1, 2]), 2);
  89:             scores[2] = Convert.ToInt32(string.Format("{0}{1}{2}", s[2, 0], s[2, 1], s[2, 2]), 2);
  90:  
  91:             scores[3] = Convert.ToInt32(string.Format("{0}{1}{2}", s[0, 0], s[1, 0], s[2, 0]), 2);
  92:             scores[4] = Convert.ToInt32(string.Format("{0}{1}{2}", s[0, 1], s[1, 1], s[2, 1]), 2);
  93:             scores[5] = Convert.ToInt32(string.Format("{0}{1}{2}", s[0, 2], s[1, 2], s[2, 2]), 2);
  94:  
  95:  
  96:             return findDistinct(scores);
  97:         }
  98:  
  99:         private static int findDistinct(int[] i)
 100:         {
 101:             List<int> rtn = new List<int>();
 102:             for (int j = 0; j < i.Length; j++)
 103:             {
 104:                 if (!rtn.Contains(i[j]))
 105:                     rtn.Add(i[j]);
 106:             }
 107:             return rtn.Count;
 108:         }
 109:  
 110:         private string[] parse(string s)
 111:         {
 112:             Console.WriteLine("Parsing " + s);
 113:             string [] rtn = new string [3];
 114:             rtn[0] = s.Substring(0,1);
 115:             rtn[1] = s.Substring(1,1);
 116:             rtn[2] = s.Substring(2,1);
 117:             return rtn;
 118:         }
 119:         private static int rnd()
 120:         {
 121:             System.Threading.Thread.Sleep(DateTime.Now.Millisecond);
 122:             Random r = new Random(DateTime.Now.Millisecond);
 123:             return r.Next(0, 7);
 124:         }
 125:         private static string getBinary(int j)
 126:         {
 127:             if (j > 7)
 128:                 throw new OverflowException(string.Format("{0} is out of range"));
 129:  
 130:             string l = string.Format("000{0}", Convert.ToString(j, 2));
 131:             return l.Substring(l.Length - 3, 3);
 132:         }
 133:  
 134:     }
 135: }

Image test

July 30, 2009

jollyguy   gordon-brown-404_667800c

Demos

July 30, 2009

Image resizer