static void Main(string[] args) { Console.WriteLine("Day 7"); Console.WriteLine("Star 1"); Console.WriteLine(); string[] lines = File.ReadAllLines(inputFile); HashSet <WireValue> operations = new HashSet <WireValue>(); foreach (string line in lines) { int arrowIndex = line.IndexOf("->"); string outputName = line.Substring(arrowIndex + 3); WireValue input = null; string[] splitInput = line.Substring(0, arrowIndex - 1).Split(' '); if (splitInput.Length == 1) { //123 -> Y //X -> Y input = GetInput(splitInput[0]); } else if (splitInput.Length == 2) { //NOT X -> Y if (splitInput[0] != "NOT") { throw new Exception(); } input = new NotOperation(GetInput(splitInput[1])); } else if (splitInput[1] == "AND") { input = new AndOperation(GetInput(splitInput[0]), GetInput(splitInput[2])); } else if (splitInput[1] == "OR") { input = new OrOperation(GetInput(splitInput[0]), GetInput(splitInput[2])); } else if (splitInput[1] == "RSHIFT") { input = new RShiftOperation(GetInput(splitInput[0]), int.Parse(splitInput[2])); } else if (splitInput[1] == "LSHIFT") { input = new LShiftOperation(GetInput(splitInput[0]), int.Parse(splitInput[2])); } else { throw new Exception(); } SafeGetWire(outputName).input = input; operations.Add(input); } Wire wireA = SafeGetWire("a"); int output1 = wireA.GetValue(); Console.WriteLine($"The answer is: {output1}"); Console.WriteLine(); Console.WriteLine("Star 2"); Console.WriteLine(); //Reset cached values foreach (WireValue wireValue in wires.Values) { wireValue.Reset(); } foreach (WireValue wireValue in operations) { wireValue.Reset(); } wires["b"].input = new FixedWireValue(output1); Console.WriteLine($"The answer is: {wireA.GetValue()}"); Console.WriteLine(); Console.ReadKey(); }
private static IHasValue ParseElementaryElementAndAddToNetwork(LogicNetwork logicNetwork, string sourceElementaryStr) { IHasValue source; ConstantValue constantValue; if (!ConstantValue.TryParse(sourceElementaryStr, out constantValue)) { var sourceWireName = sourceElementaryStr; var wire = logicNetwork.GetByName(sourceWireName); if (wire == null) { wire = new Wire(sourceWireName); logicNetwork.Elements.Add(wire); } source = wire; } else { logicNetwork.Elements.Add(constantValue); source = constantValue; } return source; }