public object Answer() { var code = File.ReadAllText("21.in").Split(',').Select(s => BigInteger.Parse(s)).ToArray(); while (true) { computer = new IntCodeSync(code.ToArray()); try { while (true) { ReadPrompt(); var instructions = new System.Text.StringBuilder(); var input = Console.ReadLine().ToUpper(); while (!input.Equals("RUN") && !input.Equals("")) { instructions.Append(input); instructions.Append('\n'); input = Console.ReadLine().ToUpper(); } instructions.Append("RUN"); instructions.Append('\n'); computer.Run(new Queue <BigInteger>(instructions.ToString().Select(c => (BigInteger)c))); } } catch (ComputerHaltedException) { } } return("error"); }
BigInteger val(int x, int y) { var computer = new IntCodeSync(code.ToArray()); computer.Run(x); computer.Run(y); return(computer.Run().Value); }
public object Answer() { var code = File.ReadAllText("23.in").Split(',').Select(s => BigInteger.Parse(s)).ToArray(); var computers = new Dictionary <int, IntCodeSync>(); Q = new Dictionary <int, Queue <BigInteger> >(); for (int i = 0; i < 50; i++) { var computer = new IntCodeSync(code.ToArray()); computers.Add(i, computer); computer.Run(i); Q[i] = new Queue <BigInteger>(); } while (true) { foreach (var computerAndId in computers) { var computer = computerAndId.Value; var q = Q[computerAndId.Key]; var dest = computer.Run(); if (dest == null) { if (q.Count == 0) { computer.Run(-1); } else { DeliverMessages(computer, q); } dest = computer.Run(); } ReceiveMessages(dest, computer); } if (Q.Values.All(q => q.Count == 0)) { if (lastY == NatY) { Console.WriteLine(NatY); throw new Exception(); } Q[0].Enqueue(NatX); Q[0].Enqueue(NatY); lastY = NatY; } } return("error"); }
private static void DeliverMessages(IntCodeSync computer, Queue <BigInteger> q) { computer.Run(q); //while (q.Count > 0) //{ // var snapshot = computer.CreateSnapshot(); // for (int i = 0; i < 2; i++) // { // var input = q.Peek(); // var res = computer.Run(input); // if (res != null) // { // computer.RestoreFromSnapshot(snapshot); // break; // } // q.Dequeue(); // } //} }
public string Answer() { var computerMemory = File.ReadAllText("d13.txt").Split(',').Select(x => BigInteger.Parse(x)).ToArray(); computerMemory[0] = 2; // Free plays! var computer = new IntCodeSync(computerMemory); Thread.Sleep(100); int score = -1; var game = new Dictionary <(int, int), int>(); var lastBall = (x : 0, y : 0); var lastPaddle = (x : 0, y : 0); try { while (true) { int ballX = -1; int paddleX = -1; while (true) { var x = computer.Run(); if (x == null) { break; } var y = computer.Run(); var t = computer.Run(); if (x == -1) { score = (int)t; continue; } else if (t == 4) { ballX = (int)x; game[(lastBall.x, lastBall.y)] = 0;
private void ReceiveMessages(BigInteger?dest, IntCodeSync computer) { while (dest != null) { if (dest == 255) { NatX = computer.Run().Value; NatY = computer.Run().Value; } else { var x = computer.Run().Value; var y = computer.Run().Value; Q[(int)dest].Enqueue(x); Q[(int)dest].Enqueue(y); } dest = computer.Run(); } }