public AndOperation(WireValue inputA, WireValue inputB) { this.inputA = inputA; this.inputB = inputB; }
public NotOperation(WireValue input) { this.input = input; }
public RShiftOperation(WireValue input, int rshift) { this.input = input; this.rshift = rshift; }
public LShiftOperation(WireValue input, int lshift) { this.input = input; this.lshift = lshift; }
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(); }