private static long FindResult(List<int> digits) { List<Dictionary<ModuloCount, long>> results = new List<Dictionary<ModuloCount, long>>(); results.Add(new Dictionary<ModuloCount, long>()); for (int i = 0; i < digits.Count; i++) { Dictionary<ModuloCount, long> newResult = new Dictionary<ModuloCount, long>(); for (int j = i; j >= 0; j--) { List<int> startNumber = digits.GetRange(digits.Count - 1 - i, j + 1); ModuloCount startModulo = new ModuloCount(startNumber); Process(startModulo, results[i - j], newResult); } results.Add(newResult); } long result = 0; foreach (var entry in results[results.Count - 1]) { if (entry.Key.IsUgly()) result += entry.Value; } return result; }
public static void Increment(this Dictionary<ModuloCount, long> dictionary, ModuloCount modulo, long value) { if (!dictionary.ContainsKey(modulo)) dictionary.Add(modulo, value); else dictionary[modulo] = dictionary[modulo] + value; }
public ModuloCount Subtract(ModuloCount rest) { ModuloCount result = new ModuloCount(); result.mod2 = (this.mod2 - rest.mod2 + 2) % 2; result.mod3 = (this.mod3 - rest.mod3 + 3) % 3; result.mod5 = (this.mod5 - rest.mod5 + 5) % 5; result.mod7 = (this.mod7 - rest.mod7 + 7) % 7; return result; }
public ModuloCount Add(ModuloCount rest) { ModuloCount result = new ModuloCount(); result.mod2 = (this.mod2 + rest.mod2) % 2; result.mod3 = (this.mod3 + rest.mod3) % 3; result.mod5 = (this.mod5 + rest.mod5) % 5; result.mod7 = (this.mod7 + rest.mod7) % 7; return result; }
private static void Process(ModuloCount startModulo, Dictionary<ModuloCount, long> previousResult, Dictionary<ModuloCount, long> newResult) { foreach (var entry in previousResult) { ModuloCount newAddModulo = startModulo.Add(entry.Key); newResult.Increment(newAddModulo, entry.Value); ModuloCount newSubModulo = startModulo.Subtract(entry.Key); newResult.Increment(newSubModulo, entry.Value); } if (previousResult.Count == 0) { newResult.Increment(startModulo, 1); } }
public override bool Equals(object obj) { ModuloCount other = (ModuloCount)obj; return mod2 == other.mod2 && mod3 == other.mod3 && mod5 == other.mod5 && mod7 == other.mod7; }