public void Do(MemoryDecoder md) { var mask = md.CurrentMask.Mask; var sb = new StringBuilder(); var valueB = Convert.ToString(Value, 2); valueB = new string('0', mask.Length - valueB.Length) + valueB; var memLoc = Convert.ToString(MemoryLocation, 2); memLoc = new string('0', mask.Length - memLoc.Length) + memLoc; //Part 1 //for (int i = mask.Length - 1; i >= 0; i--) //{ // var c = mask[i]; // if (c == 'X') // sb.Append(valueB[i]); // else // sb.Append(mask[i]); //} //valueB = Reverse(sb.ToString()); //Part 2 for (int i = mask.Length - 1; i >= 0; i--) { var c = mask[i]; if (c == '0') { sb.Append(memLoc[i]); } else { sb.Append(mask[i]); } } memLoc = Reverse(sb.ToString()); var memoryList = new List <string>(); ReccReplaceXWithValuesAndGetSum(memLoc, memoryList); memoryList.ForEach(a => md.Add(a, valueB)); void ReccReplaceXWithValuesAndGetSum(string value, List <string> resultList) { int firstPosOfX = value.IndexOf('X'); if (firstPosOfX == -1) { resultList.Add(value); return; } var newValue0 = value.Substring(0, firstPosOfX) + '0' + value.Substring(firstPosOfX + 1); var newValue1 = value.Substring(0, firstPosOfX) + '1' + value.Substring(firstPosOfX + 1); ReccReplaceXWithValuesAndGetSum(newValue0, resultList); ReccReplaceXWithValuesAndGetSum(newValue1, resultList); } }
public override double Main_Double() { var path = @".\Days\Day14\input.txt"; var str = System.IO.File.ReadAllLines(path); var md = new MemoryDecoder(str); md.Do(); return(md.SumOfValuesInMemory()); }
public void Do(MemoryDecoder md) { md.CurrentMask = this; }