Exemple #1
0
        protected override string SolvePartOne()
        {
            intcode = new Intcode(Input, 2);
            intcode.Run();

            SetTiles();

            return((tiles.Count(c => c.tile == (int)TileType.Block)).ToString());
        }
Exemple #2
0
        protected override string SolvePartTwo()
        {
            Intcode intcode = new Intcode(Input);

            intcode.SetInput(5);
            intcode.Run();

            return(intcode.output_register.ToString());
        }
Exemple #3
0
        protected override string SolvePartOne()
        {
            Intcode intcode = new Intcode(Input, 2);

            // We start at 0,0 and map it out
            int x = 0;
            int y = 0;

            RepairDroid.Direction direction = RepairDroid.Direction.North;

            // Add the first tile to kickstart us
            tiles.Add(new RepairDroid.Tile()
            {
                x = 0, y = 0, type = RepairDroid.TileType.Hallway
            });

            while (tiles.Count < 270400)
            {
                RepairDroid.Tile tile = tiles.First(a => a.x == x && a.y == y);

                // We know where we are, let's move
                direction         = GetNextDirection(tile, direction);
                (int x, int y)pos = GetXY(tile, direction);

                // Set our direction and run
                intcode.SetInput((int)direction);
                intcode.Run();

                // Check our output
                RepairDroid.TileType newTile = (RepairDroid.TileType)Convert.ToInt32(intcode.output_register);

                // If this is a wall, don't change x,y
                if (newTile != RepairDroid.TileType.Wall)
                {
                    x = pos.x;
                    y = pos.y;
                }

                // Add the tile if not already known
                if (tiles.Count(a => a.x == pos.x && a.y == pos.y) == 0)
                {
                    tiles.Add(new RepairDroid.Tile()
                    {
                        x = pos.x, y = pos.y, type = newTile
                    });
                }
            }

            return(tiles.Count.ToString());
        }
Exemple #4
0
        protected override string SolvePartTwo()
        {
            long[] input = Input.ToLongArray(",");

            input[1] = 57;
            input[2] = 41;

            //input = new long[] {1,0,0,0,99};
            //input = new long[] {2,3,0,3,99};
            //input = new long[] {2,4,4,5,99,0};
            //input = new long[] {1,1,1,4,99,5,6,0,99};

            Intcode intcode = new Intcode(input, 0);

            intcode.Run();

            return(intcode.memory[0].ToString());
        }
Exemple #5
0
        public Day19() : base(19, 2019, "")
        {
            // Need to find Santa's ship (100x100 square inside the beam)
            for (int y = 0; y < 50; y++)
            {
                for (int x = 0; x < 50; x++)
                {
                    Intcode intcode = new Intcode(Input, 2);
                    intcode.SetInput(x);
                    intcode.SetInput(y);
                    intcode.Run();

                    // Gather the output
                    map.Add(new BeamTile()
                    {
                        x = x, y = y, state = Convert.ToInt32(intcode.output_register.Dequeue())
                    });
                }
            }
        }
Exemple #6
0
        protected override string SolvePartTwo()
        {
            // Slopes didn't work, do this the "hard" way
            // Based on https://github.com/fdouw/AoC2019/blob/master/Day19/Day19.cs
            int prevStart = 0;      // Improve performance by ignoring the void below the beam
            int L         = 99;     // 100x100 block means x and x+99 inclusive

            Intcode intcode = null;

            for (int y = L; y < 10_000; y++)
            {
                for (int x = prevStart; x < 10_000; x++)
                {
                    intcode = new Intcode(Input, 2);
                    intcode.SetInput(x);
                    intcode.SetInput(y);
                    intcode.Run();

                    if (intcode.output_register.Dequeue() == 1)
                    {
                        // prevStart = (x < y) ? x : y;
                        prevStart = x;

                        intcode = new Intcode(Input, 2);
                        intcode.SetInput(x + L);
                        intcode.SetInput(y - L);
                        intcode.Run();

                        if (intcode.output_register.Dequeue() == 1)
                        {
                            return((x * 10_000 + y - L).ToString());
                        }

                        break;
                    }
                }
            }

            return(null);
        }
Exemple #7
0
        protected override string SolvePartOne()
        {
            Intcode intcode = new Intcode(Input, 2);
            //intcode.Run();

            // Traverse the hull
            // If a tile exists, return the color appropriately
            // If not, it hasn't been painted and it is black
            int x   = 0;
            int y   = 0;
            int dir = 0; // 0 == up, 1 == right, 2 == down, 3 == left

            while (intcode.State == State.Waiting)
            {
                // Each run is:
                // Send tile color input
                // Run
                // Retrieve new? color
                // Run
                // Retrieve direction
                // Change direction and update x/y

                IEnumerable <HullTile> search = tiles.Where(t => t.x == x && t.y == y);

                HullTile t;

                if (search.Count() == 0)
                {
                    t = new HullTile()
                    {
                        x = x, y = y, color = 0
                    };
                }
                else
                {
                    t = search.First();

                    // Remove the tile so we can update it
                    tiles.Remove(t);
                }

                // Send the color value
                intcode.SetInput(t.color);
                intcode.Run();

                // Get new color value
                t.color = Convert.ToInt32(intcode.output_register);

                // Add to the list
                this.tiles.Add(t);

                // Continue to get the direction
                intcode.Run();

                // Change direction
                if (intcode.output_register.Dequeue() == 0)
                {
                    dir -= 1;
                }
                else
                {
                    dir += 1;
                }

                // Correct the value
                if (dir < 0)
                {
                    dir += 4;
                }
                dir %= 4;

                switch (dir)
                {
                case 0:
                    y -= 1;
                    break;

                case 1:
                    x += 1;
                    break;

                case 2:
                    y += 1;
                    break;

                case 3:
                    x -= 1;
                    break;
                }

                // Move on!
            }

            return(this.tiles.Count.ToString());
        }
Exemple #8
0
        protected override string SolvePartTwo()
        {
            // Starts on a single white panel
            this.tiles = new List <HullTile>();

            Intcode intcode = new Intcode(Input, 2);
            //intcode.Run();

            // Traverse the hull
            // If a tile exists, return the color appropriately
            // If not, it hasn't been painted and it is black
            int x   = 0;
            int y   = 0;
            int dir = 0; // 0 == up, 1 == right, 2 == down, 3 == left

            bool first = true;

            while (intcode.State == State.Waiting)
            {
                // Each run is:
                // Send tile color input
                // Run
                // Retrieve new? color
                // Run
                // Retrieve direction
                // Change direction and update x/y

                IEnumerable <HullTile> search = tiles.Where(t => t.x == x && t.y == y);

                HullTile t;

                if (search.Count() == 0)
                {
                    t = new HullTile()
                    {
                        x = x, y = y, color = 0
                    };
                }
                else
                {
                    t = search.First();

                    // Remove the tile so we can update it
                    tiles.Remove(t);
                }

                if (first)
                {
                    t.color = 1;
                    first   = false;
                }

                // Send the color value
                intcode.SetInput(t.color);
                intcode.Run();

                // Get new color value
                t.color = Convert.ToInt32(intcode.output_register);

                // Add to the list
                this.tiles.Add(t);

                // Continue to get the direction
                intcode.Run();

                // Change direction
                if (intcode.output_register.Dequeue() == 0)
                {
                    dir -= 1;
                }
                else
                {
                    dir += 1;
                }

                // Correct the value
                if (dir < 0)
                {
                    dir += 4;
                }
                dir %= 4;

                switch (dir)
                {
                case 0:
                    y -= 1;
                    break;

                case 1:
                    x += 1;
                    break;

                case 2:
                    y += 1;
                    break;

                case 3:
                    x -= 1;
                    break;
                }

                // Move on!
            }

            string output = "\n";

            int sy = tiles.Min(a => a.y);
            int ey = tiles.Max(a => a.y);

            int sx = tiles.Min(a => a.x);
            int ex = tiles.Max(a => a.x);

            for (y = sy; y <= ey; y++)
            {
                for (x = sx; x <= ex; x++)
                {
                    HullTile tile = tiles.FirstOrDefault(a => a.x == x && a.y == y);

                    if (tile == null || tile.color == 0)
                    {
                        output += " ";
                    }
                    else
                    {
                        output += "#";
                    }
                }

                output += "\n";
            }

            return(output);
        }
Exemple #9
0
        protected override string SolvePartTwo()
        {
            intcode           = new Intcode(Input, 2);
            intcode.memory[0] = 2;
            intcode.Run();

            int  score         = 0;
            bool stopNextScore = false;

            while (intcode.State != State.Stopped)
            {
                GameTile tile = new GameTile();

                tile.x = Convert.ToInt32(intcode.output_register);
                intcode.Run();

                tile.y = Convert.ToInt32(intcode.output_register);
                intcode.Run();

                tile.tile = Convert.ToInt32(intcode.output_register);

                // Should we draw to make it faster?
                bool draw = false;

                // Score happens at the end of a screen draw
                if (tile.x == -1)
                {
                    score = tile.tile;

                    // Draw the screen
                    if (draw)
                    {
                        Console.WriteLine($"Score: {score}");
                    }
                    int maxX = tiles.Max(x => x.x);
                    int maxY = tiles.Max(y => y.y);

                    int y = 0;

                    while (y <= maxY)
                    {
                        for (int x = 0; x <= maxX; x++)
                        {
                            GameTile t = tiles.Where(t => t.x == x && t.y == y).First();

                            if (draw)
                            {
                                Console.Write(this.output[t.tile]);
                            }
                        }

                        if (draw)
                        {
                            Console.WriteLine();
                        }
                        y++;
                    }

                    if (draw)
                    {
                        Console.WriteLine();
                    }

                    if (stopNextScore)
                    {
                        break;
                    }
                }
                else
                {
                    if (tiles.Count(t => t.x == tile.x && t.y == tile.y) > 0)
                    {
                        GameTile t = tiles.Where(t => t.x == tile.x && t.y == tile.y).First();
                        tiles.Remove(t);
                    }

                    tiles.Add(tile);
                }


                stopNextScore = (tiles.Count > 0 && tiles.Count(c => c.tile == (int)TileType.Block) == 0);

                // Check where the ball is
                GameTile ball   = tiles.Where(t => t.tile == (int)TileType.Ball).FirstOrDefault();
                GameTile paddle = tiles.Where(t => t.tile == (int)TileType.Paddle).FirstOrDefault();

                intcode.ClearInput();

                if (ball != null && paddle != null)
                {
                    if (ball.x < paddle.x)
                    {
                        intcode.SetInput(-1);
                    }
                    else if (ball.x > paddle.x)
                    {
                        intcode.SetInput(1);
                    }
                    else
                    {
                        intcode.SetInput(0);
                    }
                }

                intcode.Run();
            }

            return(score.ToString());
        }
Exemple #10
0
        protected override string SolvePartOne()
        {
            List <List <int> > phases   = new List <List <int> >();
            List <int>         maxPhase = new List <int>();
            long maxThrust = 0;

            // All possible phases
            for (int a = 0; a <= 4; a++)
            {
                for (int b = 0; b <= 4; b++)
                {
                    for (int c = 0; c <= 4; c++)
                    {
                        for (int d = 0; d <= 4; d++)
                        {
                            for (int e = 0; e <= 4; e++)
                            {
                                var l = new List <int>()
                                {
                                    a, b, c, d, e
                                };
                                if (l.Distinct().Count() == 5)
                                {
                                    // Remove anything where the phase setting is used more than once
                                    phases.Add(l);
                                }
                            }
                        }
                    }
                }
            }

            foreach (var p in phases)
            {
                long out1, out2, out3, out4, out5;

                Intcode intcode1 = new Intcode(Input, 2);
                Intcode intcode2 = new Intcode(Input, 2);
                Intcode intcode3 = new Intcode(Input, 2);
                Intcode intcode4 = new Intcode(Input, 2);
                Intcode intcode5 = new Intcode(Input, 2);

                intcode1.SetInput(p[0]);
                intcode1.Run();

                // Set to initial 0
                intcode1.SetInput(0);
                intcode1.Run();

                out1 = intcode1.output_register.Dequeue();

                intcode2.SetInput(p[1]);
                intcode2.Run();

                // Set to Amp 1 output
                intcode2.SetInput(out1);
                intcode2.Run();

                out2 = intcode2.output_register.Dequeue();

                intcode3.SetInput(p[2]);
                intcode3.Run();

                // Set to Amp 2 output
                intcode3.SetInput(out2);
                intcode3.Run();

                out3 = intcode3.output_register.Dequeue();

                intcode4.SetInput(p[3]);
                intcode4.Run();

                // Set to Amp 3 output
                intcode4.SetInput(out3);
                intcode4.Run();

                out4 = intcode4.output_register.Dequeue();

                intcode5.SetInput(p[4]);
                intcode5.Run();

                // Set to Amp 4 output
                intcode5.SetInput(out4);
                intcode5.Run();

                out5 = intcode5.output_register.Dequeue();

                if (out5 > maxThrust)
                {
                    maxThrust = out5;
                    maxPhase  = p;
                }
            }

            return(maxThrust.ToString());
        }