コード例 #1
0
ファイル: Program.cs プロジェクト: heder/AoC2019
        static void Main(string[] args)
        {
            string[] input = File.ReadAllLines("in.txt")[0].Split(',');

            System.Int64[] ram = new System.Int64[input.Length];
            for (int i = 0; i < input.Length; i++)
            {
                ram[i] = Convert.ToInt64(input[i]);
            }

            var cpu = new Intcode.CPU(ram);

            System.Int64 inp = Convert.ToInt64(Console.ReadLine());

            Int64[] inArray = new Int64[1];
            inArray[0] = inp;

            cpu.Input = inArray;
            cpu.Run();

            if (cpu.State == Intcode.CpuState.OUTPUT_READY)
            {
                var o = cpu.Output;
            }

            Console.ReadKey();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            long[] input = File.ReadAllLines("in.txt")[0].Split(',').Select(f => Convert.ToInt64(f)).ToArray();

            const int SIZE = 100;

            int[,] grid = new int[SIZE, SIZE];

            Intcode.CPU cpu    = new Intcode.CPU(input);
            int         output = 0;

            int x = 0;
            int y = 0;

            while (cpu.State != Intcode.CpuState.STOPPED)
            {
                cpu.Input = new long[] { 1 };
                cpu.Run();
                if (cpu.State == Intcode.CpuState.OUTPUT_READY)
                {
                    output = (int)cpu.Output;
                }

                grid[x, y] = output;

                switch (output)
                {
                case 10:
                    Console.WriteLine();
                    y++;
                    x = 0;
                    break;

                default:
                    Console.Write(Encoding.ASCII.GetChars(new byte[] { (byte)output }));
                    x++;
                    break;
                }
            }

            int aggregate = 0;

            for (int i = 1; i < SIZE; i++)
            {
                for (int j = 1; j < SIZE; j++)
                {
                    // Check if "#" is surrounded by "#"
                    if (grid[i, j] == 35 && grid[i - 1, j] == 35 && grid[i + 1, j] == 35 && grid[i, j - 1] == 35 && grid[i, j + 1] == 35)
                    {
                        aggregate += i * j;
                    }
                }
            }

            Console.WriteLine(aggregate);
            Console.ReadKey();
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: heder/AoC2019
        static void Main(string[] args)
        {
            long[] input = File.ReadAllLines("in.txt")[0].Split(',').Select(f => Convert.ToInt64(f)).ToArray();

            input[0] = 2;
            Intcode.CPU cpu = new Intcode.CPU(input);

            int output = 0;

            while (cpu.State != Intcode.CpuState.STOPPED)
            {
                cpu.Run();

                if (cpu.State == Intcode.CpuState.OUTPUT_READY)
                {
                    output = (int)cpu.Output;
                }

                if (output > 255)
                {
                    Console.WriteLine(output);
                    Console.ReadKey();
                }

                switch (output)
                {
                case 58:
                case 63:
                    Console.Write(Encoding.ASCII.GetChars(new byte[] { (byte)output }));
                    var command = ASCIIEncoding.ASCII.GetBytes(Console.ReadLine() + '\n');

                    var i = command.Select(f => (long)f).ToArray();
                    cpu.Input = i;

                    //x = 0;
                    //y = 0;

                    break;


                case 10:
                    Console.WriteLine();
                    break;

                default:
                    Console.Write(Encoding.ASCII.GetChars(new byte[] { (byte)output }));
                    break;
                }
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: heder/AoC2019
        static void Main(string[] args)
        {
            long[] input = File.ReadAllLines("in.txt")[0].Split(',').Select(f => Convert.ToInt64(f)).ToArray();

            Intcode.CPU cpu    = new Intcode.CPU(input);
            int         output = 0;

            while (cpu.State != Intcode.CpuState.STOPPED)
            {
                byte[] command;

                cpu.Run();

                if (cpu.State == Intcode.CpuState.OUTPUT_READY)
                {
                    output = (int)cpu.Output.Dequeue();
                }

                if (cpu.State == Intcode.CpuState.WAITING_FOR_INPUT)
                {
                    command = ASCIIEncoding.ASCII.GetBytes(Console.ReadLine() + '\n');

                    var i = command.Select(f => (long)f).ToArray();
                    foreach (var item in i)
                    {
                        cpu.Input.Enqueue(item);
                    }
                }

                if (output > 255)
                {
                    Console.ReadKey();
                }

                switch (output)
                {
                case 10:
                    Console.WriteLine();
                    break;

                default:
                    Console.Write(Encoding.ASCII.GetChars(new byte[] { (byte)output }));
                    break;
                }
            }
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: heder/AoC2019
        static void Main(string[] args)
        {
            long[] input = File.ReadAllLines("in.txt")[0].Split(',').Select(f => Convert.ToInt64(f)).ToArray();

            const int SIZE = 50;

            int[,] grid = new int[SIZE, SIZE];
            int output = 0;

            for (int x = 0; x < SIZE; x++)
            {
                for (int y = 0; y < SIZE; y++)
                {
                    Intcode.CPU cpu = new Intcode.CPU(input);
                    cpu.Input = new long[] { x, y };
                    cpu.Run();

                    if (cpu.State == Intcode.CpuState.OUTPUT_READY)
                    {
                        output = (int)cpu.Output;
                        if (output == 1)
                        {
                            grid[x, y] = 1;
                        }
                    }
                }
            }

            int aggregate = 0;

            for (int x = 0; x < SIZE; x++)
            {
                for (int y = 0; y < SIZE; y++)
                {
                    if (grid[x, y] == 1)
                    {
                        aggregate++;
                    }
                }
            }

            Console.WriteLine(aggregate);
            Console.ReadKey();
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: heder/AoC2019
        static void Main(string[] args)
        {
            long[] input = File.ReadAllLines("in.txt")[0].Split(',').Select(f => Convert.ToInt64(f)).ToArray();

            const int XSIZE = 50;
            const int YSIZE = 50;

            int[,] grid = new int[XSIZE, YSIZE];

            input[0] = 2; // Free to play
            Intcode.CPU cpu = new Intcode.CPU(input);

            int score     = 0;
            int ballPos   = 0;
            int paddlePos = 0;

            int iterations = 0;

            while (cpu.State != Intcode.CpuState.STOPPED)
            {
                int x = 0;
                int y = 0;
                int c = 0;


                int ii = 0;

                //if (iterations > 1000)
                //{
                //    var joy = Console.ReadKey();


                //    if (joy.KeyChar == 'a')
                //    {
                //        ii = -1;
                //    }
                //    if (joy.KeyChar == 'd')
                //    {
                //        ii = 1;
                //    }
                //    if (joy.KeyChar == ' ')
                //    {
                //        ii = 0;
                //    }
                //}

                if (paddlePos > ballPos)
                {
                    ii = -1;
                }
                if (paddlePos < ballPos)
                {
                    ii = 1;
                }
                if (paddlePos == ballPos)
                {
                    ii = 0;
                }


                cpu.Input = new long[1] {
                    (long)ii
                };
                cpu.Run();
                if (cpu.State == Intcode.CpuState.OUTPUT_READY)
                {
                    x = (int)cpu.Output;
                }

                cpu.Input = new long[1] {
                    (long)ii
                };
                cpu.Run();
                if (cpu.State == Intcode.CpuState.OUTPUT_READY)
                {
                    y = (int)cpu.Output;
                }

                cpu.Input = new long[1] {
                    (long)ii
                };
                cpu.Run();
                if (cpu.State == Intcode.CpuState.OUTPUT_READY)
                {
                    c = (int)cpu.Output;
                }

                if (x == -1 && y == 0)
                {
                    score = c;
                }
                else
                {
                    grid[x, y] = c;

                    if (c == 4)
                    {
                        ballPos = x;
                    }

                    if (c == 3)
                    {
                        paddlePos = x;
                    }
                }



                //if (iterations > 1000)
                //{
                //    Console.Clear();
                //    for (int i = 0; i < YSIZE; i++)
                //    {
                //        for (int j = 0; j < XSIZE; j++)
                //        {
                //            //Console.Write(grid[i, j]);

                //            switch (grid[i, j])
                //            {
                //                case 0:
                //                    Console.Write(" ");
                //                    //b.SetPixel(j, i, Color.White);
                //                    break;

                //                case 1:
                //                    Console.Write("I");
                //                    //b.SetPixel(j, i, Color.White);
                //                    break;
                //                case 2:
                //                    Console.Write("X");
                //                    //b.SetPixel(j, i, Color.White);
                //                    break;
                //                case 3:
                //                    Console.Write("H");
                //                    //b.SetPixel(j, i, Color.White);
                //                    break;
                //                case 4:
                //                    Console.Write("o");
                //                    //b.SetPixel(j, i, Color.White);
                //                    break;

                //                default:
                //                    break;
                //            }


                //        }
                //        Console.WriteLine();

                //    }

                Console.WriteLine(score);
                //}

                iterations++;
            }


            Console.ReadKey();
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: heder/AoC2019
        static void Main(string[] args)
        {
            long[] input = File.ReadAllLines("in.txt")[0].Split(',').Select(f => Convert.ToInt64(f)).ToArray();

            Intcode.CPU cpu = new Intcode.CPU(input);

            int  output    = 0;
            bool trialmode = false;
            byte trial     = 0;

            while (cpu.State != Intcode.CpuState.STOPPED)
            {
                byte[] command;

                cpu.Run();

                if (cpu.State == Intcode.CpuState.OUTPUT_READY)
                {
                    output = (int)cpu.Output.Dequeue();
                }

                if (cpu.State == Intcode.CpuState.WAITING_FOR_INPUT)
                {
                    if (trialmode == true)
                    {
                        string drop = "drop fuel cell\ndrop cake\ndrop pointer\ndrop boulder\ndrop mutex\ndrop antenna\ndrop tambourine\ndrop coin\n";
                        string take = "";

                        if (IsBitSet(trial, 0))
                        {
                            take += "take fuel cell\n";
                        }
                        if (IsBitSet(trial, 1))
                        {
                            take += "take cake\n";
                        }
                        if (IsBitSet(trial, 2))
                        {
                            take += "take pointer\n";
                        }
                        if (IsBitSet(trial, 3))
                        {
                            take += "take boulder\n";
                        }
                        if (IsBitSet(trial, 4))
                        {
                            take += "take mutex\n";
                        }
                        if (IsBitSet(trial, 5))
                        {
                            take += "take antenna\n";
                        }
                        if (IsBitSet(trial, 6))
                        {
                            take += "take tambourine\n";
                        }
                        if (IsBitSet(trial, 7))
                        {
                            take += "take coin\n";
                        }

                        string c = drop + take + "east\n";

                        command = ASCIIEncoding.ASCII.GetBytes(c);
                        Console.WriteLine(trial);
                        trial++;
                    }
                    else
                    {
                        command = ASCIIEncoding.ASCII.GetBytes(Console.ReadLine() + '\n');

                        if (command[0] == 97)
                        {
                            trialmode = true;
                        }
                    }

                    var i = command.Select(f => (long)f).ToArray();
                    foreach (var item in i)
                    {
                        cpu.Input.Enqueue(item);
                    }
                }

                switch (output)
                {
                case 10:
                    Console.WriteLine();
                    break;

                default:
                    Console.Write(Encoding.ASCII.GetChars(new byte[] { (byte)output }));
                    break;
                }
            }
        }
コード例 #8
0
        public static void Explore()
        {
            while (true)
            {
                var toVisit = GetNotVisitedNeighBours();

                if (toVisit.Count == 0)
                {
                    while (toVisit.Count == 0)
                    {
                        // All visited, move droid back to a tile where "toVisit" is not 0
                        cpu.Input = new long[] { Convert.ToInt64(GetCurrentTile().DirectionBack) };
                        cpu.Run();
                        if (cpu.State == Intcode.CpuState.OUTPUT_READY)
                        {
                            var output = (int)cpu.Output; // Should always be 1
                            if (output != 1)
                            {
                                Console.WriteLine($"Error moving droid back");
                            }

                            CurrentLocation = GetCoordinateByDirection(GetCurrentTile().DirectionBack);
                        }

                        if (cpu.State == Intcode.CpuState.STOPPED)
                        {
                            Visualise(true);
                            FillWithOxygen();
                            return;
                        }

                        toVisit = GetNotVisitedNeighBours();
                    }
                }

                var previousTile = GetCurrentTile();

                foreach (var direction in toVisit)
                {
                    int output = -1;
                    cpu.Input = new long[] { Convert.ToInt64(direction) };
                    cpu.Run();
                    if (cpu.State == Intcode.CpuState.OUTPUT_READY)
                    {
                        output = (int)cpu.Output;
                    }

                    if (output == 0) // Droid hit a wall. Tile in direction "item" is wall. Droid has not moved.
                    {
                        GetTileInDirection(direction).Type    = TileType.WALL;
                        GetTileInDirection(direction).Visited = true;
                    }

                    if (output == 1) // Droid moved
                    {
                        GetTileInDirection(direction).Type          = TileType.EMPTY;
                        GetTileInDirection(direction).Visited       = true;
                        GetTileInDirection(direction).DirectionBack = GetOppositeDirection(direction);
                        CurrentLocation = GetCoordinateByDirection(direction);
                        GetCurrentTile().DistanceFromOrigin = previousTile.DistanceFromOrigin + 1;
                        break;
                    }

                    if (output == 2) // Droid found destination
                    {
                        GetTileInDirection(direction).Type               = TileType.O2_SENSOR;
                        GetTileInDirection(direction).Visited            = true;
                        GetTileInDirection(direction).DirectionBack      = GetOppositeDirection(direction);
                        GetTileInDirection(direction).DistanceFromOrigin = previousTile.DistanceFromOrigin + 1;
                        CurrentLocation = GetCoordinateByDirection(direction);
                        GetCurrentTile().DistanceFromOrigin = previousTile.DistanceFromOrigin + 1;

                        sensor.X = CurrentLocation.X;
                        sensor.Y = CurrentLocation.Y;

                        break;
                        // Trace back to origin
                    }
                }

                iterations++;

                Visualise(false);
            }
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: heder/AoC2019
        static void Main(string[] args)
        {
            const int SIZE = 100;

            long[] input = File.ReadAllLines("in.txt")[0].Split(',').Select(f => Convert.ToInt64(f)).ToArray();

            Pixel[,] grid = new Pixel[SIZE, SIZE];
            Intcode.CPU cpu = new Intcode.CPU(input);

            int currentX = SIZE / 2;
            int currentY = SIZE / 2;

            grid[currentX, currentY].Color = 1;

            Direction currentDirection = Direction.UP;

            while (cpu.State != Intcode.CpuState.STOPPED)
            {
                // Read current color
                var currentColor = grid[currentX, currentY].Color;

                // Input to Intcode
                cpu.Input = new long[1] {
                    currentColor
                };
                cpu.Run();

                if (cpu.State == Intcode.CpuState.OUTPUT_READY)
                {
                    // Output is color
                    grid[currentX, currentY].Color   = (int)cpu.Output;
                    grid[currentX, currentY].Painted = true;
                }

                cpu.Input = new long[0];
                cpu.Run();

                // Output is turn
                if (cpu.State == Intcode.CpuState.OUTPUT_READY)
                {
                    if (cpu.Output == 0)
                    {
                        // Turn Left
                        if (currentDirection == Direction.RIGHT)
                        {
                            currentDirection = Direction.UP;
                        }
                        else
                        {
                            currentDirection++;
                        }
                    }
                    if (cpu.Output == 1)
                    {
                        if (currentDirection == Direction.UP)
                        {
                            currentDirection = Direction.RIGHT;
                        }
                        else
                        {
                            currentDirection--;
                        }
                    }

                    // Move
                    switch (currentDirection)
                    {
                    case Direction.UP:
                        currentX--;
                        break;

                    case Direction.LEFT:
                        currentY--;
                        break;

                    case Direction.DOWN:
                        currentX++;
                        break;

                    case Direction.RIGHT:
                        currentY++;
                        break;
                    }
                }
            }

            int painted = 0;

            for (int x = 0; x < SIZE; x++)
            {
                for (int y = 0; y < SIZE; y++)
                {
                    switch (grid[x, y].Color)
                    {
                    case 0:
                        Console.Write('.');
                        break;

                    case 1:
                        Console.Write('*');
                        break;

                    default:
                        break;
                    }

                    if (grid[x, y].Painted == true)
                    {
                        painted++;
                    }
                }

                Console.WriteLine("");
            }

            Console.WriteLine($"Painted {painted} tiles");
            Console.ReadKey();
        }
コード例 #10
0
ファイル: Program.cs プロジェクト: heder/AoC2019
        static void Main(string[] args)
        {
            long[] input = File.ReadAllLines("in.txt")[0].Split(',').Select(f => Convert.ToInt64(f)).ToArray();

            const int SIZE = 100;

            int[,] grid = new int[SIZE, SIZE];

            Intcode.CPU cpu = new Intcode.CPU(input);

            while (cpu.State != Intcode.CpuState.STOPPED)
            {
                int x = 0;
                int y = 0;
                int c = 0;

                cpu.Input = new long[] { };
                cpu.Run();
                if (cpu.State == Intcode.CpuState.OUTPUT_READY)
                {
                    x = (int)cpu.Output;
                }

                cpu.Input = new long[] { };
                cpu.Run();
                if (cpu.State == Intcode.CpuState.OUTPUT_READY)
                {
                    y = (int)cpu.Output;
                }

                cpu.Input = new long[] { };
                cpu.Run();
                if (cpu.State == Intcode.CpuState.OUTPUT_READY)
                {
                    c = (int)cpu.Output;
                }

                grid[x, y] = c;
            }

            int noOfBlockTiles = 0;

            // Dump some ASCII-Art
            for (int i = 0; i < SIZE; i++)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    //switch (image[((i * STRIDE) + j)])
                    //{
                    //    case 0:
                    //        Console.Write(" ");
                    //        b.SetPixel(j, i, Color.Transparent);
                    //        break;

                    //    case 1:
                    //        Console.Write("o");
                    //        b.SetPixel(j, i, Color.Black);
                    //        break;
                    //    case 2:
                    //        Console.Write(".");
                    //        b.SetPixel(j, i, Color.White);
                    //        break;

                    //    default:
                    //        break;
                    //}
                    if (grid[j, i] == 2)
                    {
                        noOfBlockTiles++;
                    }
                }
            }

            Console.WriteLine(noOfBlockTiles);
            Console.ReadKey();
        }
コード例 #11
0
ファイル: Program.cs プロジェクト: heder/AoC2019
        static void Main(string[] args)
        {
            long[] input = File.ReadAllLines("in.txt")[0].Split(',').Select(f => Convert.ToInt64(f)).ToArray();

            const int SIZE = 1500;

            int[,] grid = new int[SIZE, SIZE];
            int output = 0;

            Intcode.CPU cpu;

            for (int x = 0; x < SIZE; x++)
            {
                for (int y = 0; y < SIZE; y++)
                {
                    cpu       = new Intcode.CPU(input);
                    cpu.Input = new long[] { x, y };
                    cpu.Run();

                    if (cpu.State == Intcode.CpuState.OUTPUT_READY)
                    {
                        output = (int)cpu.Output;
                        if (output == 1)
                        {
                            grid[x, y] = 1;
                        }
                    }
                }
            }

            int xmaxTractorBeam = 0;

            // Find rightmost tractor beam position
            for (int x = SIZE - 1; x > 0; x--)
            {
                for (int y = 0; y < SIZE; y++)
                {
                    if (grid[x, y] == 1)
                    {
                        Console.WriteLine($"First tractor beam grid is at xpos {x}");
                        xmaxTractorBeam = x;
                        break;
                    }
                }

                if (xmaxTractorBeam > 0)
                {
                    break;
                }
            }

            xmaxTractorBeam -= 100;

            int lowx = 10000;
            int lowy = 10000;

            for (int xx = xmaxTractorBeam - 100; xx >= 0; xx--)
            {
                coord a = new coord()
                {
                    x = xx, y = 0
                };
                coord b = new coord()
                {
                    x = xx + 99, y = 0
                };
                coord c = new coord()
                {
                    x = xx, y = 99
                };
                coord d = new coord()
                {
                    x = xx + 99, y = 99
                };

                // Move ship down until all coordinates are inside beam
                for (int i = 0; i < SIZE - 100; i++)
                {
                    a.y++;
                    b.y++;
                    c.y++;
                    d.y++;

                    if (grid[a.x, a.y] == 1 && grid[b.x, b.y] == 1 && grid[c.x, c.y] == 1 && grid[d.x, d.y] == 1)
                    {
                        //Console.WriteLine($"Ship fits @ {a.x},{a.y}");
                        if (a.x < lowx)
                        {
                            lowx = a.x; lowy = a.y;
                        }
                    }
                }
            }

            Bitmap bm = new Bitmap(SIZE, SIZE);

            for (int i = 0; i < SIZE; i++)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    if (grid[i, j] == 1)
                    {
                        bm.SetPixel(i, j, Color.White);
                    }
                    else
                    {
                        bm.SetPixel(i, j, Color.Black);
                    }
                }
            }

            bm.SetPixel(lowx, lowy, Color.Red);
            bm.Save("output.png", System.Drawing.Imaging.ImageFormat.Png);

            Console.WriteLine($"{lowx},{lowy}");
            Console.ReadKey();
        }