Skip to content

Instantly share code, notes, and snippets.

@benwillkommen
Created May 24, 2016 14:48
Show Gist options
  • Save benwillkommen/5b97018c5a5d429f4148f1afbefff798 to your computer and use it in GitHub Desktop.
Save benwillkommen/5b97018c5a5d429f4148f1afbefff798 to your computer and use it in GitHub Desktop.
Availability Hash Tables
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace AvailabilityMatrix
{
class Program
{
static void Main(string[] args)
{
var jsonString = "{\"3263\": [\"OMF\", \"LDF\", \"LDB\"],\"3227\": [\"OMF\", \"LDF\"],\"3401\": [\"OMF\"],\"3428\": [\"OMF\", \"OMB\"]}";
Dictionary<string, List<string>> colorToType, typeToColor;
GetAvailabilityDictionaries(jsonString, out colorToType, out typeToColor);
var fakeFullyShotType = "OMF";
var fakeDisplayColor = "3227";
if (colorToType.ContainsKey(fakeDisplayColor) && colorToType[fakeDisplayColor].Contains(fakeFullyShotType))
{
Console.WriteLine("Image exists for color {0}, type {1}", fakeDisplayColor, fakeFullyShotType);
}
}
public static void GetAvailabilityDictionaries(string jsonString, out Dictionary<string, List<string>> colorToType,
out Dictionary<string, List<string>> typeToColor)
{
colorToType = new Dictionary<string, List<string>>();
typeToColor = new Dictionary<string, List<string>>();
var colors = new List<string>();
var json = JObject.Parse(jsonString);
foreach (var color in json.Children())
{
var colorId = color.Path;
colors.Add(colorId);
var typesList = new List<string>();
typesList.AddRange(color.Values().Select(c => c.Value<string>()));
colorToType.Add(colorId, typesList);
foreach (var type in typesList)
{
if (typeToColor.ContainsKey(type))
{
var currentList = typeToColor[type];
if (!currentList.Contains(colorId))
{
currentList.Add(colorId);
}
}
else
{
typeToColor.Add(type, new List<string>() { colorId });
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment