Example #1
0
        private int General(int gridSize, Returner returner)
        {
            var grid = new BeamGrid(gridSize);

            var computer       = new IntcodeComputer(FileContents);
            int previousFirstY = 0;
            int previousLastY  = 0;

            for (int x = 0; x < gridSize; x++)
            {
                int firstY = -1;
                int lastY  = -1;

                for (int y = previousFirstY; y < gridSize; y++)
                {
                    int output = (int)computer.RunToHalt(null, x, y);
                    grid[x, y] = (PointType)output;
                    computer.Reset();
                    if (grid[x, y] == PointType.Beam)
                    {
                        firstY = y;
                        if (previousLastY < firstY)
                        {
                            previousLastY = firstY;
                        }
                        for (int y0 = firstY; y0 < previousLastY; y0++) // cover gaps
                        {
                            grid[x, y0] = PointType.Beam;
                        }
                        break;
                    }
                }
                for (int y = previousLastY; y < gridSize; y++)
                {
                    int output = (int)computer.RunToHalt(null, x, y);
                    grid[x, y] = (PointType)output;
                    computer.Reset();
                    if (grid[x, y] == PointType.Air)
                    {
                        lastY = y;
                        break;
                    }
                }

                if (firstY > -1)
                {
                    previousFirstY = firstY;
                }
                if (lastY > -1)
                {
                    previousLastY = lastY;
                }
            }

            return(returner(grid));
        }
Example #2
0
        private static void Part2()
        {
            Stopwatch sw = Stopwatch.StartNew();

            var             input    = File.ReadAllText("input.txt").Split(',').Select(long.Parse).ToArray();
            IntcodeComputer computer = new IntcodeComputer(input);

            int firstX = 0;
            int y      = 99;

            bool running = true;

            while (running)
            {
                for (int x = firstX; x < 10000; x++)
                {
                    computer.Reset();
                    computer.Run(x, y);
                    if (computer.OutputQueue.Dequeue() != 1)
                    {
                        continue;
                    }
                    firstX = x;
                    computer.Reset();
                    computer.Run(x + 99, y - 99);
                    if (computer.OutputQueue.Dequeue() == 1)
                    {
                        Console.WriteLine($"Part 2: {x * 10000 + y - 99}");
                        running = false;
                    }

                    break;
                }
                y++;
            }

            sw.Stop();
            Debug.WriteLine(sw.Elapsed);
        }
Example #3
0
        private static void Part1()
        {
            Stopwatch sw = Stopwatch.StartNew();

            var             input    = File.ReadAllText("input.txt").Split(',').Select(long.Parse).ToArray();
            IntcodeComputer computer = new IntcodeComputer(input);

            int count  = 0;
            int firstX = 0;
            int lastX  = 50;

            for (int y = 0; y < 50; y++)
            {
                bool found = false;
                int  curX  = lastX + 1;
                for (int x = firstX; x < lastX; x++)
                {
                    computer.Reset();
                    computer.Run(x, y);
                    if (computer.OutputQueue.Dequeue() == 1)
                    {
                        if (!found)
                        {
                            firstX = x;
                        }
                        found = true;
                        count++;
                    }
                    else if (found)
                    {
                        curX = x + 1;
                        break;
                    }
                }
                lastX = curX;
            }

            Console.WriteLine($"Part 1: {count}");

            sw.Stop();
            Debug.WriteLine(sw.Elapsed);
        }
Example #4
0
        public override object RunPart2()
        {
            var computer = new IntcodeComputer(InputAsIntcode);

            for (int noun = 0; noun < 100; noun++)
            {
                for (int verb = 0; verb < 100; verb++)
                {
                    computer.Reset();
                    computer.SetMemoryValue(1, noun);
                    computer.SetMemoryValue(2, verb);
                    computer.RunUntilHalt();

                    if (computer.GetMemoryValue(0) == 19690720)
                    {
                        return(100 * noun + verb);
                    }
                }
            }

            return(default);
Example #5
0
        public bool TestPoint(Point point)
        {
            if (point.X < 0 || point.Y < 0)
            {
                return(false);
            }

            _computer.Reset();
            _computer.Input.Enqueue(point.X);
            _computer.Input.Enqueue(point.Y);

            var result = _computer.Run();

            if (result != IntcodeResult.HALT_TERMINATE)
            {
                throw new InvalidOperationException("WTF!");
            }

            var programResult = _computer.Output.Dequeue();

            return(programResult == 1);
        }