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));
            }
        }
        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 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));
            }
        }
        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));
            }
        }
Exemple #5
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));
            }
        }
Exemple #6
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("");
        }
Exemple #7
0
        /// <inheritdoc />
        public string Part2()
        {
            IntCodeVM2 vm = new IntCodeVM2(intCode.Select(i => (long)i).ToList());

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

            return($"{vm.GetLastOutput()}");
        }
Exemple #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);
        }
Exemple #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()}");
        }
Exemple #10
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("");
        }
        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));
        }
Exemple #12
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
        }
Exemple #13
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");
        }
Exemple #14
0
        /// <inheritdoc />
        public string Part1()
        {
            IntCodeVM2 vm = new IntCodeVM2(program);

            // used for drawing
            tiles = new Dictionary <Vector2Int, Tile>
            {
                { Vector2Int.Zero, Tile.Empty }
            };

            // all visited places
            places = new List <Place>();
            places.Add(new Place()
            {
                Position    = Vector2Int.Zero,
                PreviousDir = Direction.East,
                UnExplored  = new List <Direction>()
                {
                    Direction.East, Direction.North, Direction.West, Direction.South
                }
            });

            Vector2Int currentPos   = Vector2Int.Zero;
            Vector2Int lastValidPos = Vector2Int.Zero;

            while (places.Count(p => p.UnExplored.Count > 0) > 0)
            {
                // generate next move
                Direction dir = NextMove(currentPos, places);
                vm.AddInput((int)dir);
                currentPos = Move(currentPos, dir);


                /*
                 * // bump into every wall and pray
                 * switch (random.Next(0, 4))
                 * {
                 *  case 0:
                 *      vm.AddInput((int)Direction.North);
                 *      currentPos = Move(currentPos, Direction.North);
                 *      break;
                 *
                 *  case 1:
                 *      vm.AddInput((int)Direction.South);
                 *      currentPos = Move(currentPos, Direction.South);
                 *      break;
                 *
                 *  case 2:
                 *      vm.AddInput((int)Direction.West);
                 *      currentPos = Move(currentPos, Direction.West);
                 *      break;
                 *
                 *  case 3:
                 *      vm.AddInput((int)Direction.East);
                 *      currentPos = Move(currentPos, Direction.East);
                 *      break;
                 * }
                 */

                // get input

                /*
                 * switch (Console.ReadKey().Key)
                 * {
                 *  case ConsoleKey.UpArrow:
                 *      vm.AddInput((int)Direction.North);
                 *      currentPos = Move(currentPos, Direction.North);
                 *      break;
                 *
                 *  case ConsoleKey.DownArrow:
                 *      vm.AddInput((int)Direction.South);
                 *      currentPos = Move(currentPos, Direction.South);
                 *      break;
                 *
                 *  case ConsoleKey.LeftArrow:
                 *      vm.AddInput((int)Direction.West);
                 *      currentPos = Move(currentPos, Direction.West);
                 *      break;
                 *
                 *  case ConsoleKey.RightArrow:
                 *      vm.AddInput((int)Direction.East);
                 *      currentPos = Move(currentPos, Direction.East);
                 *      break;
                 * }
                 */


                // resume
                vm.ResumeProgram();

                // get status
                switch (vm.GetLastOutput())
                {
                case 0:
                    if (!tiles.ContainsKey(currentPos))
                    {
                        tiles.Add(currentPos, Tile.Wall);
                    }

                    currentPos = lastValidPos;
                    break;

                case 1:
                    lastValidPos = currentPos;

                    if (!tiles.ContainsKey(currentPos))
                    {
                        tiles.Add(currentPos, Tile.Empty);

                        // add surrounding tiles to explore
                        Place newPlace = new Place()
                        {
                            Position    = currentPos,
                            PreviousDir = OppositeDir(dir),
                        };

                        if (!tiles.ContainsKey(currentPos + new Vector2Int(0, -1)))
                        {
                            newPlace.UnExplored.Add(Direction.North);
                        }
                        if (!tiles.ContainsKey(currentPos + new Vector2Int(0, +1)))
                        {
                            newPlace.UnExplored.Add(Direction.South);
                        }
                        if (!tiles.ContainsKey(currentPos + new Vector2Int(-1, 0)))
                        {
                            newPlace.UnExplored.Add(Direction.West);
                        }
                        if (!tiles.ContainsKey(currentPos + new Vector2Int(+1, 0)))
                        {
                            newPlace.UnExplored.Add(Direction.East);
                        }

                        places.Add(newPlace);
                    }

                    break;

                case 2:
                    lastValidPos = currentPos;

                    if (!tiles.ContainsKey(currentPos))
                    {
                        tiles.Add(currentPos, Tile.Oxygen);

                        // add surrounding tiles to explore
                        Place newPlace = new Place()
                        {
                            Position    = currentPos,
                            PreviousDir = OppositeDir(dir),
                        };

                        if (!tiles.ContainsKey(currentPos + new Vector2Int(0, -1)))
                        {
                            newPlace.UnExplored.Add(Direction.North);
                        }
                        if (!tiles.ContainsKey(currentPos + new Vector2Int(0, +1)))
                        {
                            newPlace.UnExplored.Add(Direction.South);
                        }
                        if (!tiles.ContainsKey(currentPos + new Vector2Int(-1, 0)))
                        {
                            newPlace.UnExplored.Add(Direction.West);
                        }
                        if (!tiles.ContainsKey(currentPos + new Vector2Int(+1, 0)))
                        {
                            newPlace.UnExplored.Add(Direction.East);
                        }

                        places.Add(newPlace);
                    }
                    break;
                }

                // draw screen
                if (visualize)
                {
                    DrawScreen(tiles, currentPos);
                }
            }

            // find out where oxygen is
            Vector2Int oxygenLocation = tiles.Where(t => t.Value == Tile.Oxygen).Select(t => t.Key).First();
            Place      oxygenPlace    = places.Find(p => p.Position.Equals(oxygenLocation));

            int   distance     = 0;
            Place currentPlace = oxygenPlace;

            while (!currentPlace.Position.Equals(Vector2Int.Zero))
            {
                distance++;
                var currentPosition = Move(currentPlace.Position, currentPlace.PreviousDir);
                currentPlace = places.Find(p => p.Position.Equals(currentPosition));
            }

            // push output down a bit
            if (visualize)
            {
                Console.SetCursorPosition(0, 50);
            }

            return($"Distance to travel: {distance} tiles");
        }