Example #1
0
        private static void Part2()
        {
            Stopwatch sw = Stopwatch.StartNew();

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

            input[0] = 2;
            IntcodeComputer computer = new IntcodeComputer(input);

            var screen = new Dictionary <Point, long>();

            bool running = true;

            while (running)
            {
                running = computer.RunNext() == long.MinValue;
                if (computer.OutputQueue.Count == 3)
                {
                    long x  = computer.OutputQueue.Dequeue();
                    long y  = computer.OutputQueue.Dequeue();
                    long id = computer.OutputQueue.Dequeue();

                    Point currentPoint = new Point(x, y);
                    if (!screen.ContainsKey(currentPoint))
                    {
                        screen.Add(currentPoint, 0);
                    }
                    screen[currentPoint] = id;
                }

                if (computer.AwaitingInput())
                {
                    //code for the display
                    //for (long y = screen.Keys.Max(p => p.Y); y >= screen.Keys.Min(p => p.Y); y--)
                    //{
                    //    for (long x = screen.Keys.Min(p => p.X); x <= screen.Keys.Max(p => p.X); x++)
                    //    {
                    //        Point point = new Point(x,y);
                    //        if (!screen.ContainsKey(point)) continue;
                    //        switch (screen[point])
                    //        {
                    //            case 0:
                    //                Console.Write(" ");
                    //                break;
                    //            case 1:
                    //                Console.Write("#");
                    //                break;
                    //            case 2:
                    //                Console.Write("$");
                    //                break;
                    //            case 3:
                    //                Console.Write("_");
                    //                break;
                    //            case 4:
                    //                Console.Write("*");
                    //                break;
                    //            default:
                    //                break;
                    //        }
                    //    }
                    //    Console.WriteLine();
                    //}
                    Point ballPos   = screen.First(v => v.Value == 4).Key;
                    Point paddlePos = screen.First(v => v.Value == 3).Key;
                    if (ballPos.X < paddlePos.X)
                    {
                        computer.InputQueue.Enqueue(-1);
                    }
                    else if (ballPos.X > paddlePos.X)
                    {
                        computer.InputQueue.Enqueue(1);
                    }
                    else
                    {
                        computer.InputQueue.Enqueue(0);
                    }
                }
            }

            Console.WriteLine($"Part 2: {screen[new Point(-1, 0)]}");

            sw.Stop();
            Debug.WriteLine(sw.Elapsed);
        }
Example #2
0
        private static void Part2()
        {
            var sw = System.Diagnostics.Stopwatch.StartNew();

            long max = long.MinValue;

            foreach (var settings in GetPermutations(Enumerable.Range(5, 5), 5))
            {
                IntcodeComputer computerA = new IntcodeComputer(rom);
                IntcodeComputer computerB = new IntcodeComputer(rom);
                IntcodeComputer computerC = new IntcodeComputer(rom);
                IntcodeComputer computerD = new IntcodeComputer(rom);
                IntcodeComputer computerE = new IntcodeComputer(rom);

                computerA.InputQueue = computerE.OutputQueue;
                computerB.InputQueue = computerA.OutputQueue;
                computerC.InputQueue = computerB.OutputQueue;
                computerD.InputQueue = computerC.OutputQueue;
                computerE.InputQueue = computerD.OutputQueue;

                var settingsArray = settings as int[] ?? settings.ToArray();
                computerA.InputQueue.Enqueue(settingsArray[0]);
                computerB.InputQueue.Enqueue(settingsArray[1]);
                computerC.InputQueue.Enqueue(settingsArray[2]);
                computerD.InputQueue.Enqueue(settingsArray[3]);
                computerE.InputQueue.Enqueue(settingsArray[4]);

                computerA.InputQueue.Enqueue(0);

                bool running = true;
                while (running)
                {
                    if (!computerA.AwaitingInput())
                    {
                        computerA.RunNext();
                    }
                    if (!computerB.AwaitingInput())
                    {
                        computerB.RunNext();
                    }
                    if (!computerC.AwaitingInput())
                    {
                        computerC.RunNext();
                    }
                    if (!computerD.AwaitingInput())
                    {
                        computerD.RunNext();
                    }
                    if (!computerE.AwaitingInput())
                    {
                        running = computerE.RunNext() == long.MinValue;
                    }
                }

                long signal = computerE.OutputQueue.Dequeue();
                max = Math.Max(max, signal);
            }

            Console.WriteLine($"Part 2: {max}");

            sw.Stop();
            System.Diagnostics.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);
            IntcodeComputer computer         = new IntcodeComputer(input);
            var             screen           = new Dictionary <Point, long>();
            Point           droidPos         = new Point(0, 0);
            Point           nextPos          = new Point(0, 0);
            long            currentDirection = 1;

            bool running = true;

            while (running)
            {
                running = computer.RunNext() == long.MinValue;
                if (computer.OutputQueue.Count == 1)
                {
                    long result = computer.OutputQueue.Dequeue();
                    screen[nextPos] = result;
                    switch (result)
                    {
                    case 0:
                        nextPos = droidPos;
                        currentDirection++;
                        if (currentDirection > 4)
                        {
                            currentDirection = 1;
                        }
                        break;

                    case 1:
                        droidPos = nextPos;
                        break;

                    case 2:
                        droidPos = nextPos;
                        Console.WriteLine(droidPos);
                        break;
                    }

                    //if (result == 2) break;
                }
                if (computer.AwaitingInput())
                {
                    //computer.InputQueue.Enqueue(currentDirection);
                    //switch (currentDirection)
                    //{
                    //    case 1:
                    //        nextPos.Y++;
                    //        break;
                    //    case 2:
                    //        nextPos.Y--;
                    //        break;
                    //    case 3:
                    //        nextPos.X--;
                    //        break;
                    //    case 4:
                    //        nextPos.X++;
                    //        break;
                    //}
                    Console.Clear();
                    Console.WriteLine();
                    for (long y = screen.Keys.Max(p => p.Y); y >= screen.Keys.Min(p => p.Y); y--)
                    {
                        for (long x = screen.Keys.Min(p => p.X); x <= screen.Keys.Max(p => p.X); x++)
                        {
                            Point point = new Point(x, y);

                            if (point.Equals(droidPos))
                            {
                                Console.Write("D");
                                continue;
                            }

                            if (point.Equals(new Point(0, 0)))
                            {
                                Console.Write("&");
                                continue;
                            }
                            if (!screen.ContainsKey(point))
                            {
                                Console.Write(" ");
                                continue;
                            }
                            switch (screen[point])
                            {
                            case 0:
                                Console.Write("#");
                                break;

                            case 1:
                                Console.Write(".");
                                break;

                            case 2:
                                Console.Write("*");
                                break;
                            }
                        }
                        Console.WriteLine();
                    }

                    while (computer.AwaitingInput())
                    {
                        ConsoleKeyInfo i = Console.ReadKey();

                        switch (i.Key)
                        {
                        case ConsoleKey.LeftArrow:
                            computer.InputQueue.Enqueue(3);
                            nextPos.X--;
                            break;

                        case ConsoleKey.RightArrow:
                            computer.InputQueue.Enqueue(4);
                            nextPos.X++;
                            break;

                        case ConsoleKey.UpArrow:
                            computer.InputQueue.Enqueue(1);
                            nextPos.Y++;
                            break;

                        case ConsoleKey.DownArrow:
                            computer.InputQueue.Enqueue(2);
                            nextPos.Y--;
                            break;

                        default:
                            break;
                        }
                    }
                }
            }

            sw.Stop();
            Debug.WriteLine(sw.Elapsed);
        }