Пример #1
0
        public void TestLessThan8PositionMode(long input, long expected)
        {
            var computer = new IntComputer();

            computer.Run(new long[] { 3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8 }, new[] { input });
            Assert.AreEqual(expected, computer.Outputs[0]);
        }
Пример #2
0
        public void Test1(long[] program, int position, long value)
        {
            var computer = new IntComputer();

            computer.Run(program);
            Assert.AreEqual(value, computer.Memory[position]);
        }
Пример #3
0
        public void JumpTestPositionMode(long input, long expected)
        {
            var computer = new IntComputer();

            computer.Run(new long[] { 3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9 }, new[] { input });
            Assert.AreEqual(expected, computer.Outputs[0]);
        }
Пример #4
0
        public void TestLessThan8ImmediateMode(long input, long expected)
        {
            var computer = new IntComputer();

            computer.Run(new long[] { 3, 3, 1107, -1, 8, 3, 4, 3, 99 }, new[] { input });
            Assert.AreEqual(expected, computer.Outputs[0]);
        }
Пример #5
0
        public void JumpTestImmediateMode(long input, long expected)
        {
            var computer = new IntComputer();

            computer.Run(new long[] { 3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1 }, new[] { input });
            Assert.AreEqual(expected, computer.Outputs[0]);
        }
Пример #6
0
        public void Test1_1()
        {
            var program  = new long[] { 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 };
            var computer = new IntComputer();

            computer.Run(program, new long[] {});
            Assert.AreEqual(program, computer.Outputs.ToArray());
        }
Пример #7
0
        public void Test1_3()
        {
            var program  = new long[] { 104, 1125899906842624, 99 };
            var computer = new IntComputer();

            computer.Run(program, new long[] {});
            Assert.AreEqual(1125899906842624L, computer.PopOutput());
        }
Пример #8
0
        public void Test1()
        {
            var input    = 54321;
            var computer = new IntComputer();

            computer.Run(new long[] { 3, 0, 4, 0, 99 }, new long[] { input });
            Assert.AreEqual(input, computer.Outputs[0]);
        }
Пример #9
0
        public void TestComparisonTo8(long input, long expected)
        {
            var computer = new IntComputer();

            computer.Run(new long[] { 3, 21, 1008, 21, 8, 20, 1005, 20, 22, 107, 8, 21, 20, 1006, 20, 31,
                                      1106, 0, 36, 98, 0, 0, 1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104,
                                      999, 1105, 1, 46, 1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99 }, new[] { input });
            Assert.AreEqual(expected, computer.Outputs[0]);
        }
Пример #10
0
        public void TestReadOutOfBounds()
        {
            var program = new long[]   {
                1, 10, 11, 7, 4, 7, 99, 321
            };                                                // add[10]+[11]->[7], output [7] -> expect [7] to be zero (0+0)
            var computer = new IntComputer();

            computer.Run(program);
            Assert.AreEqual(0, computer.PopOutput());
        }
Пример #11
0
        public void Test1_2()
        {
            var program  = new long[] { 1102, 34915192, 34915192, 7, 4, 7, 99, 0 };
            var computer = new IntComputer();

            computer.Run(program, new long[] {});
            var outputStr = computer.PopOutput().ToString(CultureInfo.InvariantCulture);

            Assert.AreEqual(16, outputStr.Length);
        }
Пример #12
0
        public void TestWriteOutOfBounds()
        {
            var program = new long[]   {
                1, 7, 8, 1024, 4, 1024, 99, 321, 123
            };                                                        // add[7]+[8]->[1024], output [1024] -> expect [1024] to be 444 (321+123)
            var computer = new IntComputer();

            computer.Run(program);
            Assert.AreEqual(444, computer.PopOutput());
        }
Пример #13
0
        public static long ExecuteAndGetPosition0(IEnumerable <long> input)
        {
            var modifiedInput = input.ToList();

            modifiedInput[1] = 12;
            modifiedInput[2] = 2;
            var computer = new IntComputer();

            computer.Run(modifiedInput);
            return(computer.Memory[0]);
        }
Пример #14
0
        public static long RunSensorBoost(IEnumerable <long> program)
        {
            var computer = new IntComputer();

            computer.Run(program, new long[] { 2 }); // 2 is for Actual Boost Mode
            if (computer.OutputCount > 1)
            {
                throw new Exception("there should be only one output");
            }
            return(computer.PopOutput());
        }
Пример #15
0
        public static long RunCode(IEnumerable <long> program, string code)
        {
            // execute script and get results
            var computer = new IntComputer();
            var ec       = computer.Run(program, code.Select(c => (long)c));

            Console.WriteLine("Exit code: " + ec);
            Console.WriteLine("Outputs: " + String.Join(',', computer.Outputs));
            Render(computer.Outputs);
            return(computer.Outputs.Last());
        }
Пример #16
0
        public static long RunBoostDiagnostics(IEnumerable <long> program)
        {
            var computer = new IntComputer();

            computer.Run(program, new long[] { 1 }); // 1 is for Test Mode
            if (computer.OutputCount > 1)
            {
                throw new Exception("self-diagnostic failed, error: " + string.Join(",", computer.Outputs));
            }
            return(computer.PopOutput());
        }
Пример #17
0
        public static long ThrusterSignal(List <long> program, List <long> phases)
        {
            var signal = 0L;

            for (var i = 0; i < phases.Count; i++)
            {
                var phase    = phases[i];
                var computer = new IntComputer();
                computer.Run(program, new[] { phase, signal });
                signal = computer.Outputs[0];
            }
            return(signal);
        }
Пример #18
0
        public static int CountBlockTiles(IEnumerable <long> program)
        {
            var computer = new IntComputer();

            computer.Run(program);
            var blockTilesCount = 0;

            for (var i = 2; i < computer.OutputCount; i += 3)
            {
                if (computer.Outputs[i] == 2 /*Block*/)
                {
                    blockTilesCount++;
                }
            }
            return(blockTilesCount);
        }
Пример #19
0
        public static long CheckTestsAndGetDiagnosticCode(List <long> program, long input)
        {
            var computer = new IntComputer();

            computer.Run(program, new[] { input });
            var outputs = computer.Outputs;

            for (var i = 0; i < outputs.Count - 1; i++)
            {
                if (outputs[i] != 0)
                {
                    throw new Exception($"test {i} failed");
                }
            }
            return(outputs.Last());
        }
Пример #20
0
        // In step1 we (manually) found the instructions
        // Step2 applies the program and gets the result
        public static long Part2_Step2(IEnumerable <long> program)
        {
            var instructions = "A,B,B,C,C,A,A,B,B,C\n" +
                               "L,12,R,4,R,4\n" +
                               "R,12,R,4,L,12\n" +
                               "R,12,R,4,L,6,L,8,L,8\n" +
                               "n\n";
            // execute and get results
            var modifiedProgram = program.ToList();

            modifiedProgram[0] = 2;
            var computer = new IntComputer();
            var ec       = computer.Run(modifiedProgram, instructions.Select(c => (long)c));

            Console.WriteLine("Exit code: " + ec);
            Console.WriteLine("Outputs: " + String.Join(',', computer.Outputs));
            return(computer.Outputs.Last());
        }
Пример #21
0
        private static List <string> GetMap(IEnumerable <long> program)
        {
            // execute the program
            var computer = new IntComputer();
            var ec       = computer.Run(program);
            var outputs  = computer.Outputs.ToList();

            Console.WriteLine("Exit code: " + ec);

            // map to a list of strings
            var sb = new StringBuilder();

            foreach (var output in outputs)
            {
                sb.Append((char)output);
            }
            var map = sb.ToString().Split('\n', StringSplitOptions.RemoveEmptyEntries).ToList();

            return(map);
        }
Пример #22
0
 public static (int, int) GuessNounAndVerb(IEnumerable <long> input, long target)
 {
     for (var noun = 0; noun < 100; noun++)
     {
         for (var verb = 0; verb < 100; verb++)
         {
             var modifiedInput = input.ToList();
             modifiedInput[1] = noun;
             modifiedInput[2] = verb;
             var computer = new IntComputer();
             try
             {
                 computer.Run(modifiedInput);
                 if (computer.Memory[0] == target)
                 {
                     return(noun, verb);
                 }
             } catch (Exception) { continue; }
         }
     }
     return(0, 0);
 }
Пример #23
0
        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);
        }
Пример #24
0
        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);
        }
Пример #25
0
        // 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);
        }