private static NumAnswer[] SplitNumber(long i) { string num = i.ToString(); num += num.Substring(0, 2); // ensure tail to head NumAnswer[] nums = new NumAnswer[6]; nums[0] = new NumAnswer(Int32.Parse(num.Substring(0, 4))); nums[1] = new NumAnswer(Int32.Parse(num.Substring(2, 4))); nums[2] = new NumAnswer(Int32.Parse(num.Substring(4, 4))); nums[3] = new NumAnswer(Int32.Parse(num.Substring(6, 4))); nums[4] = new NumAnswer(Int32.Parse(num.Substring(8, 4))); nums[5] = new NumAnswer(Int32.Parse(num.Substring(10, 4))); return nums; }
private static long RebuildNumber(NumAnswer[] nums) { // rebuild number take 0,2,4 elements 1 10000 100000000 long result = nums[4].num; result = checked(nums[2].num * 10000L + result); result = checked(nums[0].num * 100000000L + result); return result; }
public static void Solve() { NumAnswer[] nums = new NumAnswer[6]; int currentNum = TOSTART; do { NumAnswer nextOne = GetNextPolygonal(currentNum); currentNum = nextOne.num; known[currentNum] = nextOne; } while (currentNum < BIGGEST); // 2882, 8281, 8128 // 28828128 // need 6 x 4 // 1234 3456 5678 7890 9012 1212 // 12 34 56 78 90 12 == 12 characters long for (int i = 0; i < 5; i += 2) nums[i] = SkipToNextPoly(TOSTART); bool endOfTheLine = false; do { //reconstitute nums array long number = RebuildNumber(nums); nums = SplitNumber(number); if (EachIsUniqueAnswer(nums)) { //if (EachIsPolygonal(nums)) //{ if (EveryPolygonalAccountedFor(nums)) { Console.WriteLine(); Console.WriteLine("FOUND!!"); int sum = 0; for (int i = 0; i < 6; i++) { Console.Write("{0,4} :", nums[i].num); for (int sides = 3; sides < 9; sides++) { Console.Write(" {0,-6}", nums[i].isTypes[sides - 3]); } Console.WriteLine(); sum += nums[i].num; } Console.WriteLine("======="); Console.WriteLine(sum); endOfTheLine = true; } //} } // move next since no match found nums[4] = SkipToNextPoly(nums[4].num); if (nums[4].num > BIGGEST) { nums[4] = SkipToNextPoly(TOSTART); nums[2] = SkipToNextPoly(nums[2].num); if (nums[2].num > BIGGEST) { nums[2] = SkipToNextPoly(TOSTART); nums[0] = SkipToNextPoly(nums[0].num); if (nums[0].num > BIGGEST) { endOfTheLine = true; } } } } while (!endOfTheLine); Console.WriteLine("UH OH! NOT FOUND!!!"); return; }
private static bool EveryPolygonalAccountedFor(NumAnswer[] nums) { for (int p0 = 0; p0 < 6;p0++ ) { bool[] foundPoly0 = new bool[6]; if(nums[0].isTypes[p0]) { foundPoly0[p0] = true; for (int p1 = 0; p1 < 6; p1++) { bool[] foundPoly1 = new bool[6]; Array.Copy(foundPoly0, foundPoly1, 6); if (nums[1].isTypes[p1]) { if(foundPoly1[p1]) continue; foundPoly1[p1] = true; for (int p2 = 0; p2 < 6; p2++) { bool[] foundPoly2 = new bool[6]; Array.Copy(foundPoly1, foundPoly2, 6); if (nums[2].isTypes[p2]) { if (foundPoly2[p2]) continue; foundPoly2[p2] = true; for (int p3 = 0; p3 < 6; p3++) { bool[] foundPoly3 = new bool[6]; Array.Copy(foundPoly2, foundPoly3, 6); if (nums[3].isTypes[p3]) { if (foundPoly3[p3]) continue; foundPoly3[p3] = true; for (int p4 = 0; p4 < 6; p4++) { bool[] foundPoly4 = new bool[6]; Array.Copy(foundPoly3, foundPoly4, 6); if (nums[4].isTypes[p4]) { if (foundPoly4[p4]) continue; foundPoly4[p4] = true; for (int p5 = 0; p5 < 6; p5++) { bool[] foundPoly5 = new bool[6]; Array.Copy(foundPoly4, foundPoly5, 6); if (nums[5].isTypes[p5]) { if (foundPoly5[p5]) continue; foundPoly5[p5] = true; // now check to see if all six are true bool found = true; for(int finds = 0; finds<6;finds++) { found = found && foundPoly5[finds]; } if (found) return true; } } } } } } } } } } } } return false; }
private static bool EachIsUniqueAnswer(NumAnswer[] nums) { for (int i = 0; i < nums.Length-1; i++) { for (int j = i + 1; j < nums.Length; j++) { if (ArraysMatch(nums[i].isTypes, nums[j].isTypes)) return false; } } return true; }
private static bool EachIsPolygonal(NumAnswer[] nums) { for (int i = 0; i < nums.Length; i++) { if (!nums[i].isPolygonal) return false; } return true; }