Created
December 24, 2020 19:52
-
-
Save ZingBallyhoo/77ee3d158e82f768bf8718c957b8d1bc to your computer and use it in GitHub Desktop.
Decode cloth data from https://santatracker.google.com/santaselfie.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string[] sprayNames = new[] | |
{ | |
"red", "orange", "yellow", "green", "cyan", "purple", "pink", "blue" | |
}; | |
string[] decorationNames = new[] | |
{ | |
"snowman", "bauble", "bow", "holly" | |
}; | |
char[] charMap = new []{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; | |
string RleDecode(string input) | |
{ | |
string rleString = string.Empty; | |
var numMatches = Regex.Matches(input, "([0-9]+)"); | |
var charMatches = Regex.Matches(input, "([A-Za-z])"); | |
Debug.Assert(numMatches.Count == charMatches.Count); | |
for (int s = 0; s < charMatches.Count; s++) | |
{ | |
char c = charMatches[s].Groups[0].Value[0]; | |
for (int n = 0; n < int.Parse(numMatches[s].Groups[0].Value); n++) | |
{ | |
rleString += c; | |
} | |
} | |
return rleString; | |
} | |
int[] Base52Decode(string input) | |
{ | |
int[] output = new int[input.Length]; | |
for (int i = 0; i < output.Length; i++) | |
{ | |
output[i] = Array.IndexOf(charMap, input[i]); | |
} | |
return output; | |
} | |
void Main() | |
{ | |
var input = "1B2G2B5G10B3G1B2G2B2G1B2G3B1G1B2G5B3G3B1G2B6G1B2G7B1G3B5G1B1G2B2G1B2G5B2G2B5G4B2G1B3G4B2G3B4G2B2G5B2G1B1G7B3G1B3G6B1G7B9G5B3G5B1G1B5G1B2G4B5G3B1G1I2G1B1G3B1G1B7G3B8G5B7G4B8G1B1G5B2G7B7G1B4G3B1G6B8G1B1G1B3G9B2G1B1G1B10G2B2G4B3G1B3G1B7G2B1G2B1A2B2G1B6G1B6G1B1G2B1A2B6G2B3G1B1G1B2G1B2G1B1A13G1B4G4B1A8G1B5G1B5G1B2A3B6G1B10G1B2A3B15G2B3A1G4B13G1B4A1G1B2G1B11G2B5A2B2G1B1G1B9G7A14B9A1B2G15B2G2B2G2B2G2B3G1B3G2B2G1B3G1B1G"; | |
string rleString = RleDecode(input); | |
Console.Out.WriteLine(rleString); | |
if (rleString.Length != 645) | |
{ | |
Console.Out.WriteLine("length should be 645"); | |
} | |
int[] output = Base52Decode(rleString); | |
//Console.Out.WriteLine(output); | |
for (int i = 0; i < output.Length; i++) | |
{ | |
var point = new ClothPoint(output[i]); | |
Console.Out.Write($"{i} - {output[i]}"); | |
if (!point.Draw()) | |
{ | |
Console.Out.WriteLine($" - hidden"); | |
continue; | |
} | |
if (point.IsPlain()) | |
{ | |
Console.Out.WriteLine($" - plain"); | |
continue; // wont be anything else | |
} | |
var spray = point.Spray(); | |
if (spray != null) | |
{ | |
Console.Out.Write($" - {sprayNames[spray.Value]} spray ({spray})"); | |
} | |
var decoration = point.Decoration(); | |
if (decoration != null) | |
{ | |
Console.Out.Write($" - {decorationNames[decoration.Value]} decoration ({decoration})"); | |
} | |
Console.Out.WriteLine(); | |
} | |
} | |
struct ClothPoint | |
{ | |
private readonly int m_value; | |
public ClothPoint(int value) | |
{ | |
m_value = value; | |
} | |
public bool IsPlain() => m_value == 1; | |
public bool Draw() => 0 < m_value; | |
public int? Spray() | |
{ | |
var sprayValue = (int)Math.Floor((decimal)(m_value - 1) / 5); // hmm decimal. most precice floor | |
if (sprayValue == 0) return null; | |
return sprayValue-1; | |
} | |
public int? Decoration() | |
{ | |
var decorationValue = (m_value - 1) % 5; | |
if (decorationValue == 0) return null; | |
return decorationValue-1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment