Skip to content

Instantly share code, notes, and snippets.

@mjrousos
Created January 2, 2020 20:40
Show Gist options
  • Save mjrousos/ee57a112954f8733252f7c4e6d0f00a4 to your computer and use it in GitHub Desktop.
Save mjrousos/ee57a112954f8733252f7c4e6d0f00a4 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
namespace TilePuzzle
{
enum TileColors { Red, Blue, Yellow, Green }
class Program
{
const int ColorCount = 4;
static void Main(string[] args)
{
var Permutations = new List<byte[]>();
for (byte a = 0; a < ColorCount; a++)
for (byte b = 0; b < ColorCount; b++)
for (byte c = 0; c < ColorCount; c++)
for (byte d = 0; d < ColorCount; d++)
{
Permutations.Add(new byte[] { a, b, c, d});
}
Console.WriteLine($"All possible tiles: {Permutations.Count}");
Console.WriteLine();
Console.WriteLine("Removing rotational duplicates...");
var Combinations = new List<byte[]>();
foreach (var p in Permutations)
{
var rotations = new[] { Turn90(p), Turn180(p), Turn270(p) };
var dup = rotations.FirstOrDefault(r => Combinations.Any(c => c[0] == r[0] && c[1] == r[1] && c[2] == r[2] && c[3] == r[3]));
if (dup != null)
{
Console.WriteLine($" Removing {TileToString(p)} because it duplicates {TileToString(dup)}");
continue;
}
Combinations.Add(p);
}
Console.WriteLine();
Console.WriteLine($"All combinations without duplicates:");
foreach(var tile in Combinations)
{
Console.WriteLine($" {TileToString(tile)}");
}
Console.WriteLine();
Console.WriteLine($"All combinations (without duplicates): {Combinations.Count}");
}
static byte[] Rotate(byte[] original) => new byte[] { original[1], original[2], original[3], original[0] };
static byte[] Turn90(byte[] original) => Rotate(original);
static byte[] Turn180(byte[] original) => Rotate(Rotate(original));
static byte[] Turn270(byte[] original) => Rotate(Rotate(Rotate(original)));
static string TileToString(byte[] tile) => $"[{string.Join(", ", tile.Select(b => (TileColors)b))}]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment