public void TestOutputBaseOffset(long answer, bool jump)
        {
            List <long> program = new List <long>()
            {
                109, 3,
                2205, answer + 8, 10,
                104, 0,
                99,
                104, 1,
                99,
                0, 6,
                8
            };

            IntCodeVM2 vm = new IntCodeVM2(program);

            vm.ExecuteProgram();
            if (jump)
            {
                Assert.That(vm.GetLastOutput(), Is.EqualTo(1));
            }
            else
            {
                Assert.That(vm.GetLastOutput(), Is.EqualTo(0));
            }
        }
        public void TestLessThanBaseOffset(long a, long b, bool aIsBigger)
        {
            List <long> program = new List <long>()
            {
                109, 2,
                22207, 13, 14, 15, // 3
                5, 17, 18,         // 6
                104, 0,            // 8
                99,                // 9
                104, 1,            // 11
                99,                // 12
                a, b, -1, 12       // 15
            };

            IntCodeVM2 vm = new IntCodeVM2(program);

            vm.ExecuteProgram();
            if (aIsBigger)
            {
                Assert.That(vm.GetLastOutput(), Is.EqualTo(1));
            }
            else
            {
                Assert.That(vm.GetLastOutput(), Is.EqualTo(0));
            }
        }
        public void TestLessThanDirect(long a, long b, bool aIsBigger)
        {
            List <long> program = new List <long>()
            {
                1107, a, b, 15,   // 3
                5, 15, 16,        // 6
                104, 0,           // 8
                99,               // 9
                104, 1,           // 11
                99,               // 12
                a, b, -1, 10      // 15
            };

            IntCodeVM2 vm = new IntCodeVM2(program);

            vm.ExecuteProgram();
            if (aIsBigger)
            {
                Assert.That(vm.GetLastOutput(), Is.EqualTo(1));
            }
            else
            {
                Assert.That(vm.GetLastOutput(), Is.EqualTo(0));
            }
        }
예제 #4
0
        /// <inheritdoc />
        public string Part1()
        {
            IntCodeVM2 vm = new IntCodeVM2(longs);

            vm.ExecuteProgram();

            List <int> output = vm.GetOutputs().Select(l => (int)l).ToList();

            List <Tile> tiles = new List <Tile>();

            for (int i = 0; i < output.Count; i += 3)
            {
                tiles.Add(new Tile()
                {
                    Position = new Vector2Int(output[i], output[i + 1]),
                    TileId   = output[i + 2]
                });
            }

            int blocks = 0;

            foreach (Tile t in tiles)
            {
                if (t.TileId == 2)
                {
                    blocks++;
                }
            }

            DrawScreen(tiles);


            return($"{blocks}");
        }
        public void TestOutputAddresses(long answer, bool jump)
        {
            List <long> program = new List <long>()
            {
                5, 9, 10,
                104, 0,
                99,
                104, 1,
                99,
                answer,
                6
            };

            IntCodeVM2 vm = new IntCodeVM2(program);

            vm.ExecuteProgram();
            if (jump)
            {
                Assert.That(vm.GetLastOutput(), Is.EqualTo(1));
            }
            else
            {
                Assert.That(vm.GetLastOutput(), Is.EqualTo(0));
            }
        }
예제 #6
0
        public void TestLessThanPosition(long a, long b, bool aEqualsB)
        {
            List <long> program = new List <long>()
            {
                8, 13, 14, 15,    // 3
                5, 15, 16,        // 6
                104, 0,           // 8
                99,               // 9
                104, 1,           // 11
                99,               // 12
                a, b, -1, 10      // 15
            };

            IntCodeVM2 vm = new IntCodeVM2(program);

            vm.ExecuteProgram();
            if (aEqualsB)
            {
                Assert.That(vm.GetLastOutput(), Is.EqualTo(1));
            }
            else
            {
                Assert.That(vm.GetLastOutput(), Is.EqualTo(0));
            }
        }
예제 #7
0
        /// <inheritdoc />
        public string Part2()
        {
            IntCodeVM2 vm = new IntCodeVM2(intCode.Select(i => (long)i).ToList());

            vm.AddInput(5);
            vm.ExecuteProgram();

            return($"{vm.GetLastOutput()}");
        }
예제 #8
0
        private bool IsInTractorBeam(int x, int y, IntCodeVM2 vm)
        {
            vm.Reset();
            vm.AddInput(x);
            vm.AddInput(y);
            vm.ExecuteProgram();

            return(vm.GetLastOutput() == 1);
        }
예제 #9
0
        /// <inheritdoc />
        public string Part1()
        {
            IntCodeVM2 vm = new IntCodeVM2(intCode.Select(i => (long)i).ToList());

            vm.AddInput(1);
            vm.ExecuteProgram();

            //return $"{vm.GetMemory(4)}"; // test
            return($"{vm.GetLastOutput()}");
        }
예제 #10
0
        /// <inheritdoc />
        public string Part2()
        {
            IntCodeVM2 vm = new IntCodeVM2(program);

            // show prompt
            vm.ExecuteProgram();
            DrawASCIIOutput(vm.GetOutputs());
            vm.ClearOutput();

            // input program
            //
            //  @
            // ##ABCDEFGHI###
            //
            // if D is ground and any of A,B,C aren't then jump (p1)
            // invert T (now equal to C)
            // if ((I OR F) AND E) OR H are ground, then jump
            ASCIIHelper helper = new ASCIIHelper();

            helper.AddLine("NOT A T");
            helper.AddLine("NOT B J");
            helper.AddLine("OR T J");
            helper.AddLine("NOT C T");
            helper.AddLine("OR T J");
            helper.AddLine("AND D J");

            helper.AddLine("AND T T");

            helper.AddLine("OR I T");
            helper.AddLine("OR F T");
            helper.AddLine("AND E T");
            helper.AddLine("OR H T");
            helper.AddLine("AND T J");

            helper.AddLine("RUN");

            vm.AddInput(helper.Convert());

            // output what it showed
            vm.ResumeProgram();

            if (vm.GetLastOutput() > 255)
            {
                Console.WriteLine($"\nhull damage taken: {vm.GetLastOutput()}");
            }
            else
            {
                DrawASCIIOutput(vm.GetOutputs());
            }

            return("");
        }
        public void TestOutputDirect(long answer)
        {
            List <long> program = new List <long>()
            {
                104, answer,
                99,
                3, 4, 5
            };

            IntCodeVM2 vm = new IntCodeVM2(program);

            vm.ExecuteProgram();
            Assert.That(vm.GetLastOutput(), Is.EqualTo(answer));
        }
예제 #12
0
        public void TestAdditionAddresses(long a, long b, long answer)
        {
            List <long> program = new List <long>()
            {
                2, 5, 6, 0,
                99,
                a, b
            };

            IntCodeVM2 vm = new IntCodeVM2(program);

            vm.ExecuteProgram();
            Assert.That(vm.GetMemory(0), Is.EqualTo(answer));
        }
예제 #13
0
        public void TestAdditionDirect(long a, long b, long answer)
        {
            List <long> program = new List <long>()
            {
                1102, a, b, 0,
                99,
                2, 3
            };

            IntCodeVM2 vm = new IntCodeVM2(program);

            vm.ExecuteProgram();
            Assert.That(vm.GetMemory(0), Is.EqualTo(answer));
        }
예제 #14
0
        /// <inheritdoc />
        public string Part1()
        {
            //IntCodeVM intCodeVM = new IntCodeVM(program);
            IntCodeVM2 intCodeVM = new IntCodeVM2(program);

            //intCodeVM.ResizeMemory(int.MaxValue >> 4);
            intCodeVM.AddInput(1);

            intCodeVM.ExecuteProgram();

            return($"{intCodeVM.GetLastOutput()}");

            // 2158221668 too low
            // 1187721666102244 is too high
        }
예제 #15
0
        public void TestAdditionBaseOffset(long a, long b, long answer)
        {
            List <long> program = new List <long>()
            {
                109, 7,
                2202, 0, 1, 0,
                99,
                a, b
            };

            IntCodeVM2 vm = new IntCodeVM2(program);

            vm.ExecuteProgram();
            Assert.That(vm.GetMemory(0), Is.EqualTo(answer));
        }
예제 #16
0
        public void TestInputDirect(long answer)
        {
            List <long> program = new List <long>()
            {
                103, 5,
                99,
                3, 4, 5
            };

            IntCodeVM2 vm = new IntCodeVM2(program);

            vm.AddInput(answer);
            vm.ExecuteProgram();
            Assert.That(vm.GetMemory(1), Is.EqualTo(answer));
        }
예제 #17
0
        public void TestInputAddresses(long answer)
        {
            List <long> program = new List <long>()
            {
                3, 0,
                99,
                4, 5, 6
            };

            IntCodeVM2 vm = new IntCodeVM2(program);

            vm.AddInput(answer);
            vm.ExecuteProgram();
            Assert.That(vm.GetMemory(0), Is.EqualTo(answer));
        }
예제 #18
0
        public void TestInputBaseOffset(long answer)
        {
            List <long> program = new List <long>()
            {
                109, 3,
                203, 4,
                99,
                5, 6, 7
            };

            IntCodeVM2 vm = new IntCodeVM2(program);

            vm.AddInput(answer);
            vm.ExecuteProgram();
            Assert.That(vm.GetMemory(7), Is.EqualTo(answer));
        }
예제 #19
0
        /// <inheritdoc />
        public string Part1()
        {
            IntCodeVM2 vm = new IntCodeVM2(program);

            // show prompt
            vm.ExecuteProgram();
            DrawASCIIOutput(vm.GetOutputs());
            vm.ClearOutput();

            // input program
            //J = (NOT A OR NOT B OR NOT C) AND D
            //
            //  @
            // ##ABCD###
            //
            // if D is ground and any of A,B,C aren't then jump
            ASCIIHelper helper = new ASCIIHelper();

            helper.AddLine("NOT A T");
            helper.AddLine("NOT B J");
            helper.AddLine("OR T J");
            helper.AddLine("NOT C T");
            helper.AddLine("OR T J");
            helper.AddLine("AND D J");

            helper.AddLine("WALK");

            vm.AddInput(helper.Convert());

            // output what it showed
            vm.ResumeProgram();

            if (vm.GetLastOutput() > 255)
            {
                Console.WriteLine($"\nhull damage taken: {vm.GetLastOutput()}");
            }
            else
            {
                DrawASCIIOutput(vm.GetOutputs());
            }

            return("");
        }
예제 #20
0
        /// <inheritdoc />
        public string Part1()
        {
            IntCodeVM2 vm = new IntCodeVM2(program);

            vm.ExecuteProgram();

            List <List <char> > area = new List <List <char> >();

            int total = 0;

            for (int i = 0; i < 50; i++)
            {
                area.Add(new List <char>());

                for (int j = 0; j < 50; j++)
                {
                    vm.Reset();
                    vm.AddInput(j); // x
                    vm.AddInput(i); // y

                    vm.ResumeProgram();

                    if (vm.GetLastOutput() == 0)
                    {
                        area[i].Add('.');
                        Console.Write('.');
                    }
                    else
                    {
                        area[i].Add('#');
                        Console.Write('#');
                        total++;
                    }
                }

                Console.WriteLine();
            }

            return($"{total} points are affected");
        }
예제 #21
0
        /// <inheritdoc />
        public string Part2()
        {
            IntCodeVM2 vm = new IntCodeVM2(program);

            vm.SetMemory(0, 2); // put into different mode

            // input
            // L,12, L,12, R,12,
            // L,12, L,12, R,12
            // L,8, L,8, R,12, L,8, L,8,
            // L,10, R,8, R,12,
            // L,10, R,8, R,12,
            // L,12, L,12, R,12
            // L,8, L,8, R,12, L,8, L,8,
            // L,10, R,8, R,12,
            // L,12, L,12, R,12,
            // L,8, L,8, R,12, L,8, L,8
            //
            // A, A, B, C, C, A, B, C, A, B
            // A L,12, L,12, R,12
            // B L,8, L,8, R,12, L,8, L,8
            // C L,10, R,8, R,12,

            List <long> inputs = new List <long>()
            {
                65, 44, 65, 44, 66, 44, 67, 44, 67, 44, 65, 44, 66, 44, 67, 44, 65, 44, 66, 10,
                76, 44, 49, 50, 44, 76, 44, 49, 50, 44, 82, 44, 49, 50, 10,
                76, 44, 56, 44, 76, 44, 56, 44, 82, 44, 49, 50, 44, 76, 44, 56, 44, 76, 44, 56, 10,
                76, 44, 49, 48, 44, 82, 44, 56, 44, 82, 44, 49, 50, 10,
                110, 10
            };

            foreach (long l in inputs)
            {
                vm.AddInput(l);
            }

            vm.ExecuteProgram();

            List <long> outputs = vm.GetOutputs();

            int line      = 0;
            int posInLine = 0;

            List <Vector2Int> scaffold = new List <Vector2Int>();
            long total = 0;


            foreach (long c in outputs)
            {
                switch (c)
                {
                // new line
                case 10:
                    Console.WriteLine();
                    line++;
                    posInLine = 0;
                    continue;
                    break;

                // #
                case 35:
                    Console.Write("#");
                    scaffold.Add(new Vector2Int(posInLine, line));
                    break;

                // .
                case 46:
                    Console.Write(".");
                    break;

                // <
                case 60:
                    Console.Write("<");
                    break;

                // >
                case 62:
                    Console.Write(">");
                    break;

                // ^
                case 94:
                    Console.Write("^");
                    break;

                // v
                case 118:
                    Console.Write("v");
                    break;


                // x
                case 120:
                    Console.Write("x");
                    //Console.WriteLine("");
                    //return "It's dead Jim!";
                    break;


                default:
                    if (c < 255)
                    {
                        Console.Write((char)c);
                    }
                    else
                    {
                        Console.WriteLine($"\n{c}");
                    }
                    total += c;
                    break;
                }

                posInLine++;
            }

            // higher than 5624
            //var test = outputs.Skip(3121).ToList();

            return($"total {total}");
        }
예제 #22
0
        /// <inheritdoc />
        public string Part1()
        {
            // set to true to play the text adventure
            bool userPlay = false;


            IntCodeVM2 vm = new IntCodeVM2(program);

            vm.ExecuteProgram();

            if (userPlay)
            {
                while (true)
                {
                    // output
                    foreach (long l in vm.GetOutputs())
                    {
                        if (l < 255)
                        {
                            Console.Write((char)l);
                        }
                        else
                        {
                            Console.Write(l);
                        }
                    }

                    vm.ClearOutput();

                    // input
                    string inputLine = Console.ReadLine();

                    ASCIIHelper helper = new ASCIIHelper();
                    helper.AddLine(inputLine);

                    vm.AddInput(helper.Convert());
                    vm.ResumeProgram();
                }
            }
            else
            {
                // list of inputs to get to the end
                List <string> inputs = new List <string>()
                {
                    "south",
                    "take food ration",
                    "west",
                    "north",
                    "north",
                    "east",
                    "take astrolabe",
                    "west",
                    "south",
                    "south",
                    "east",
                    "north",
                    "east",
                    "south",
                    "take weather machine",
                    "west",
                    "take ornament",
                    "east",
                    "north",
                    "east",
                    "east",
                    "east",
                    "south"
                };

                ASCIIHelper helper = new ASCIIHelper();
                foreach (string input in inputs)
                {
                    helper.AddLine(input);
                }

                vm.AddInput(helper.Convert());
                vm.ResumeProgram();

                List <long> output = vm.GetOutputs();
                output.RemoveAt(output.Count - 1);          // remove last \n
                int lastLineStart = output.LastIndexOf(10); // get last sentence

                // convert characters to a string
                StringBuilder lastLine = new StringBuilder();
                for (int i = lastLineStart + 1; i < output.Count; i++)
                {
                    if (output[i] < 255)
                    {
                        lastLine.Append((char)output[i]);
                    }
                    else
                    {
                        lastLine.Append(output[i]);
                    }
                }

                // get the number from the last line
                return(Regex.Match(lastLine.ToString(), @"\d+").Value);
            }

            return($"");
        }
예제 #23
0
        /// <inheritdoc />
        public string Part2()
        {
            IntCodeVM2 vm = new IntCodeVM2(longs);

            vm.SetMemory(0, 2);
            vm.ExecuteProgram();

            List <int> output = vm.GetOutputs().Select(l => (int)l).ToList();

            Dictionary <Vector2Int, int> tiles = new Dictionary <Vector2Int, int>();
            int finalScore = 0;

            // draw tiles
            for (int i = 0; i < output.Count; i += 3)
            {
                // get score
                if (output[i] == -1)
                {
                    finalScore = output[i + 2];
                    continue;
                }

                // create new tiles
                tiles.Add(new Vector2Int(output[i], output[i + 1]), output[i + 2]);
            }

            // as long as breakable blocks remain
            while (tiles.Values.Count(t => t == 2) > 0)
            {
                // redraw tiles
                output = vm.GetOutputs().Select(l => (int)l).ToList();
                for (int i = 0; i < output.Count; i += 3)
                {
                    // get score
                    if (output[i] == -1)
                    {
                        finalScore = output[i + 2];
                        Console.WriteLine($"Current score {finalScore}");
                        continue;
                    }

                    Vector2Int pos = new Vector2Int(output[i], output[i + 1]);
                    if (tiles.ContainsKey(pos))
                    {
                        tiles[pos] = output[i + 2];
                    }
                    else
                    {
                        tiles.Add(pos, output[i + 2]);
                    }
                }

                // show screen
                if (humanInput)
                {
                    DrawScreen(tiles);

                    // do a move
                    switch (Console.ReadKey().Key)
                    {
                    case ConsoleKey.DownArrow:
                        vm.AddInput(0);
                        break;

                    case ConsoleKey.LeftArrow:
                        vm.AddInput(-1);
                        break;

                    case ConsoleKey.RightArrow:
                        vm.AddInput(+1);
                        break;
                    }
                }
                else
                {
                    // get paddle pos
                    Vector2Int paddle = tiles.Keys.ToList()[tiles.Values.ToList().FindIndex(i => i == 3)];

                    // get ball pos
                    Vector2Int ball = tiles.Keys.ToList()[tiles.Values.ToList().FindIndex(i => i == 4)];

                    // determine next move
                    if (ball.X > paddle.X)
                    {
                        vm.AddInput(1);
                    }
                    else if (ball.X < paddle.X)
                    {
                        vm.AddInput(-1);
                    }
                    else
                    {
                        vm.AddInput(0);
                    }
                }

                // run again
                vm.ClearOutput();
                vm.ResumeProgram();
            }



            return($"final score {finalScore}");
        }
예제 #24
0
        /// <inheritdoc />
        public string Part1()
        {
            IntCodeVM2 vm = new IntCodeVM2(program);

            vm.ExecuteProgram();

            List <long> outputs = vm.GetOutputs();

            int line      = 0;
            int posInLine = 0;

            List <Vector2Int> scaffold = new List <Vector2Int>();

            foreach (long c in outputs)
            {
                switch (c)
                {
                // new line
                case 10:
                    Console.WriteLine();
                    line++;
                    posInLine = 0;
                    continue;
                    break;

                // #
                case 35:
                    Console.Write("#");
                    scaffold.Add(new Vector2Int(posInLine, line));
                    break;

                // .
                case 46:
                    Console.Write(".");
                    break;

                // <
                case 60:
                    Console.Write("<");
                    break;

                // >
                case 62:
                    Console.Write(">");
                    break;

                // ^
                case 94:
                    Console.Write("^");
                    break;

                // v
                case 118:
                    Console.Write("v");
                    break;

                // x
                case 120:
                    Console.Write("x");
                    Console.WriteLine("");
                    return("It's dead Jim!");

                    break;
                }

                posInLine++;
            }

            // check for intersections
            int sumOfAlignment = 0;

            foreach (Vector2Int vector in scaffold)
            {
                if (scaffold.Contains(new Vector2Int(vector.X + 1, vector.Y)) &&
                    scaffold.Contains(new Vector2Int(vector.X - 1, vector.Y)) &&
                    scaffold.Contains(new Vector2Int(vector.X, vector.Y + 1)) &&
                    scaffold.Contains(new Vector2Int(vector.X, vector.Y - 1)))
                {
                    Console.WriteLine($"Intersection at {vector.X} {vector.Y}");
                    sumOfAlignment += vector.X * vector.Y;
                }
            }

            return($"Sum {sumOfAlignment}");
        }