public void TestRequestInputAndContinue() { var program = new long[] { 3, 5, 4, 5, 99, 0 }; var computer = new IntComputer(); Assert.AreEqual(IntComputer.ExitCode.NotStarted, computer.LastExitCode); var ec1 = computer.Run(program); Assert.AreEqual(IntComputer.ExitCode.NeedInput, ec1); Assert.AreEqual(IntComputer.ExitCode.NeedInput, computer.LastExitCode); const long newInput = 321; var ec2 = computer.Continue(new[] { newInput }); Assert.AreEqual(IntComputer.ExitCode.Ended, ec2); Assert.AreEqual(IntComputer.ExitCode.Ended, computer.LastExitCode); Assert.AreEqual(newInput, computer.Memory[5]); Assert.AreEqual(1, computer.OutputCount); Assert.AreEqual(newInput, computer.PopOutput()); Assert.AreEqual(0, computer.OutputCount); }
public static long GetScoreAfterBreakingAllBlocks(IEnumerable <long> program) { var modifiedProgram = program.ToList(); modifiedProgram[0] = 2; // 2 quarters inserted var computer = new IntComputer(); long score = 0L; var bx = 0; // ball x-position, for autoplay var px = 0; // paddle x-position, for autoplay computer.Run(modifiedProgram); var screen = Enumerable.Range(0, 21).Select(i => new StringBuilder(new String('X', 44))).ToList(); Console.Clear(); do { while (computer.OutputCount >= 3) { var x = (int)computer.PopOutput(); var y = (int)computer.PopOutput(); long v = computer.PopOutput(); if (x == -1 && y == 0) { score = v; } else { screen[y][x] = v == 0 ? ' ' : v == 1 ? 'W' : v == 2 ? '#' : v == 3 ? '=' : v == 4 ? 'O' : '?'; } if (v == 3 && x >= 0) { px = x; } if (v == 4 && x >= 0) { bx = x; } } // display // Console.SetCursorPosition(0, 0); // screen.ForEach(r => Console.WriteLine(r)); // Console.WriteLine("Score: " + score); //System.Threading.Thread.Sleep(150); // input joystick var joystick = Math.Sign(bx - px); // if ( Console.KeyAvailable ) // switch(Console.ReadKey(true).Key) // { // case ConsoleKey.LeftArrow: joystick = -1; break; // case ConsoleKey.RightArrow: joystick = 1; break; // } computer.AddInput(joystick); } while(computer.Continue(Enumerable.Empty <long>()) != IntComputer.ExitCode.Ended); // final score while (computer.OutputCount >= 3) { var x = (int)computer.PopOutput(); var y = (int)computer.PopOutput(); long v = computer.PopOutput(); if (x == -1 && y == 0) { score = v; } } return(score); }
// Attempts // semiconductor X XX // loom X XX // mutex X X // sand X // asterisk X // wreath X // dark matter X // ornament X // LLLLLLLHLL public static long Part1(IEnumerable <long> program) { var computer = new IntComputer(false); // helpers void ContinueWith(string input) { computer.Continue(input.Select(c => (long)c)); computer.Continue(new[] { 10L }); } string OutputsToString() { var s = new string(computer.Outputs.Select(o => (char)o).ToArray()); computer.ClearOutputs(); return(s); } var ec = computer.Run(program); var i = 0; while (true) { // render output Console.Write(OutputsToString()); // take and feed input string input; if (i < _initInstructions.Count) { input = _initInstructions[i]; Console.WriteLine(input); } else { break; // move to phase 2 } //input = Console.ReadLine(); ContinueWith(input); i++; } // Phase 2 - search matching combination // we have 8 items, so it is a search from 0 to 255 using binary rep. var items = new[] { "semiconductor", "loom", "mutex", "sand", "asterisk", "wreath", "dark matter", "ornament" }; for (var c = 0; c < 256; c++) { var picks = items.Where((s, p) => (c & (1 << p)) != 0).ToList(); // pick up picks.ForEach(item => ContinueWith($"take {item}")); var o1 = OutputsToString(); Console.WriteLine($"{c,3} picked " + string.Join(", ", picks)); // test ContinueWith("east"); var output = OutputsToString(); if (output.Contains("heavier")) { Console.WriteLine("too light"); } else if (output.Contains("lighter")) { Console.WriteLine("too heavy"); } else { // win? Console.WriteLine(output); return(0); } // drop / reset picks.ForEach(item => ContinueWith($"drop {item}")); } return(1); }