Exemplo n.º 1
0
        static void Main(string[] args)
        {
            string filename = "input";

            long[] data;
            using (StreamReader sr = new StreamReader(filename))
            {
                data = sr.ReadLine().Trim().Split(',').Select(s => Int64.Parse(s)).ToArray();  // All input is on the first line
            }

            // Part 1
            // long[] quine = new long[] {109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99};
            // long[] bignum = new long[] {1102,34915192,34915192,7,4,7,99,0};
            // long[] bignum2 = new long[] {104,1125899906842624,99};
            IntCodeMachine icm = new IntCodeMachine("ICM", data);
            long           output;

            icm.Reset();
            while (icm.Next(new long[] { 1 }, out output))
            {
                System.Console.WriteLine($"1. {output}");
            }

            // Part 2
            icm.Reset();
            while (icm.Next(new long[] { 2 }, out output))
            {
                System.Console.WriteLine($"2. {output}");
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            System.Console.WriteLine("Day 13");

            string filename = "input";

            int[] data;
            using (StreamReader sr = new StreamReader(filename))
            {
                data = sr.ReadLine().Trim().Split(",").Select(s => Int32.Parse(s)).ToArray();
            }

            IntCodeMachine icm = new IntCodeMachine("arcade", data);
            Dictionary <(int x, int y), Tile> canvas = new Dictionary <(int, int), Tile>();

            int[] EMPTY = new int[] { 0 };

            // Part 1
            icm.Reset();
            while (icm.active)
            {
                int x, y, id;
                if (icm.Next(EMPTY, out x) && icm.Next(EMPTY, out y) && icm.Next(EMPTY, out id))
                {
                    canvas[(x, y)] = (Tile)id;
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            System.Console.WriteLine("Day 17");

            string         filename = "input";
            IntCodeMachine icm;

            using (StreamReader sr = new StreamReader(filename))
            {
                int[] data = sr.ReadLine().Trim().Split(",").Select(s => Int32.Parse(s)).ToArray();
                icm = new IntCodeMachine("ASCII", data);
            }

            // Read the grid (and print it)
            int           output;
            StringBuilder sb = new StringBuilder();

            icm.Reset();
            while (icm.active)
            {
                if (!icm.Next(new int[] {}, out output))
                {
                    break;
                }
                sb.Append((char)output);
            }
            string view = sb.ToString().Trim();

            char[][] grid = view.Split().Select(s => s.ToCharArray()).ToArray();
            // System.Console.WriteLine(view);

            // Part 1
            int sum = 0;

            for (int y = 1; y < grid.Length - 1; y++)
            {
                for (int x = 1; x < grid[y].Length - 1; x++)
                {
                    if (grid[y][x] == '#' && grid[y - 1][x] == '#' && grid[y + 1][x] == '#' && grid[y][x - 1] == '#' && grid[y][x + 1] == '#')
                    {
                        sum += x * y;
                    }
                }
            }
            System.Console.WriteLine($"1. {sum}");

            // Part 2
            // I mostly did this by eye & hand, from the grid drawn by the code above
            // A B A B C B A C B C
            //
            // A L12L08R10R10
            // B L06L04L12
            // C R10L08L04R10
            // =>
            // 76,44,49,50,44,76,44,56,44,82,44,49,48,44,82,44,49,48
            // 76,44,54,44,76,44,52,44,76,44,49,50
            // 82,44,49,48,44,76,44,56,44,76,44,52,44,82,44,49,48
            //
            // A 65
            // B 66
            // C 67
            // L 76
            // R 82
            // , 44
            // \n 10
            // n 110
            // y 121

            int[] main  = new int[] { 65, 44, 66, 44, 65, 44, 66, 44, 67, 44, 66, 44, 65, 44, 67, 44, 66, 44, 67, 10 };
            int[] A     = new int[] { 76, 44, 49, 50, 44, 76, 44, 56, 44, 82, 44, 49, 48, 44, 82, 44, 49, 48, 10 };
            int[] B     = new int[] { 76, 44, 54, 44, 76, 44, 52, 44, 76, 44, 49, 50, 10 };
            int[] C     = new int[] { 82, 44, 49, 48, 44, 76, 44, 56, 44, 76, 44, 52, 44, 82, 44, 49, 48, 10 };
            int[] video = new int[] { 110, 10 };
            inputData = new List <int>()
                        .Concat(main)
                        .Concat(A)
                        .Concat(B)
                        .Concat(C)
                        .Concat(video)
                        .ToArray();

            icm.Reset();
            icm.SetRegister(0, 2);
            icm.InputReader  += new IcmInputReader(InputWriter);
            icm.OutputWriter += new IcmOutputWriter(OutputReader);
            icm.Run();

            // Read output
            sb.Clear();
            foreach (int item in outputData)
            {
                sb.Append((char)item);
            }
            System.Console.WriteLine(sb.ToString());
            System.Console.WriteLine(outputData.Last());
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            System.Console.WriteLine("Day 15");

            string         filename = "input";
            IntCodeMachine icm;

            using (StreamReader sr = new StreamReader(filename))
            {
                int[] data = sr.ReadLine().Trim().Split(",").Select(s => Int32.Parse(s)).ToArray();
                icm = new IntCodeMachine("arcade", data);
            }

            // Part 1
            Room currentRoom = new Room(0, 0);

            icm.Reset();
            while (icm.active)
            {
                int dir = currentRoom.NextDirection();
                if (dir < 5)
                {
                    (int x, int y) = currentRoom.Neighbour(dir);
                    if (Room.GetRoom(x, y) == null)
                    {
                        int response;
                        icm.Next(dir, out response);
                        if (response == Room.WALL)
                        {
                            new Room(x, y, Room.opposite[dir], Room.WALL);
                        }
                        else if (response == Room.EMPTY)
                        {
                            Room room = new Room(x, y, Room.opposite[dir], Room.EMPTY);
                            currentRoom = room;
                        }
                        else if (response == Room.SYSTEM)
                        {
                            Room room = new Room(x, y, Room.opposite[dir], Room.SYSTEM);
                            currentRoom = room;
                        }
                        else
                        {
                            throw new Exception($"Unknown response: {response}");
                        }
                    }
                }
                else
                {
                    // All option have been explored: backtrack to previous room
                    dir = currentRoom.PreviousDir;
                    if (dir > 0)
                    {
                        int response;
                        icm.Next(dir, out response);
                        if (response == Room.EMPTY || response == Room.SYSTEM)
                        {
                            (int x, int y) = currentRoom.Neighbour(dir);
                            currentRoom    = Room.GetRoom(x, y);
                        }
                        else
                        {
                            throw new Exception($"Unknown or unexpected response for previous room: {response}");
                        }
                    }
                    else
                    {
                        // No more options
                        // TODO: pathfinding
                        System.Console.WriteLine("Found all rooms?");

                        // Draw the maze
                        int xMin = Int32.MaxValue;
                        int xMax = Int32.MinValue;
                        int yMin = Int32.MaxValue;
                        int yMax = Int32.MinValue;
                        foreach ((int x, int y)p in Room.AllRooms.Keys)
                        {
                            if (p.x < xMin)
                            {
                                xMin = p.x;
                            }
                            if (p.x > xMax)
                            {
                                xMax = p.x;
                            }
                            if (p.y < yMin)
                            {
                                yMin = p.y;
                            }
                            if (p.y > yMax)
                            {
                                yMax = p.y;
                            }
                        }
                        System.Console.WriteLine($"Canvas: ({xMin},{yMin}) to ({xMax},{yMax}).");
                        for (int y = yMin; y <= yMax; y++)
                        {
                            string line = "";
                            for (int x = xMin; x <= xMax; x++)
                            {
                                if (Room.AllRooms.ContainsKey((x, y)))
                                {
                                    line += Room.AllRooms[(x, y)].GetImage();
                                }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            System.Console.WriteLine("Day 11");

            // Read data
            string filename = "input";

            long[] data;
            using (StreamReader sr = new StreamReader(filename))
            {
                data = sr.ReadLine().Trim().Split(",").Select(s => Int64.Parse(s)).ToArray();
            }

            // Part 1
            int X   = 0;
            int Y   = 0;
            int dir = 0;
            HashSet <(int, int)> painted = new HashSet <(int, int)>();
            HashSet <(int, int)> white = new HashSet <(int, int)>();
            IntCodeMachine       icm = new IntCodeMachine("robot", data);
            long colour, deltaDir;

            long[] input;
            long[] WHITE = new long[] { 1 };
            long[] BLACK = new long[] { 0 };
            long[] EMPTY = new long[0];
            icm.Reset();
            while (icm.active)
            {
                input = (white.Contains((X, Y))) ? WHITE : BLACK;
                if (icm.Next(input, out colour) && icm.Next(EMPTY, out deltaDir))
                {
                    // Paint black or white
                    if (colour == 0)
                    {
                        white.Remove((X, Y));
                    }
                    else
                    {
                        white.Add((X, Y));
                    }
                    painted.Add((X, Y));

                    dir = (deltaDir == 0) ? dir - 1 : dir + 1;
                    dir = (dir + 4) % 4;    // To keep positive, and in range

                    // Y is up
                    if (dir == 0)
                    {
                        Y++;
                    }
                    else if (dir == 1)
                    {
                        X++;
                    }
                    else if (dir == 2)
                    {
                        Y--;
                    }
                    else if (dir == 3)
                    {
                        X--;
                    }
                }
            }
            System.Console.WriteLine($"1. {painted.Count}");

            // Part 2
            white.Clear();
            white.Add((0, 0));
            X   = Y = 0;
            dir = 0;
            icm.Reset();
            while (icm.active)
            {
                input = (white.Contains((X, Y))) ? WHITE : BLACK;
                if (icm.Next(input, out colour) && icm.Next(EMPTY, out deltaDir))
                {
                    // Paint black or white
                    if (colour == 0)
                    {
                        white.Remove((X, Y));
                    }
                    else
                    {
                        white.Add((X, Y));
                    }
                    painted.Add((X, Y));

                    dir = (deltaDir == 0) ? dir - 1 : dir + 1;
                    dir = (dir + 4) % 4;    // To keep positive, and in range

                    // Y is up
                    if (dir == 0)
                    {
                        Y++;
                    }
                    else if (dir == 1)
                    {
                        X++;
                    }
                    else if (dir == 2)
                    {
                        Y--;
                    }
                    else if (dir == 3)
                    {
                        X--;
                    }
                }
            }

            // Find the canvas size and boundaries
            int minX = Int32.MaxValue;
            int maxX = Int32.MinValue;
            int minY = Int32.MaxValue;
            int maxY = Int32.MinValue;

            foreach ((int x, int y) in white)
            {
                if (x < minX)
                {
                    minX = x;
                }
                if (x > maxX)
                {
                    maxX = x;
                }
                if (y < minY)
                {
                    minY = y;
                }
                if (Y > maxY)
                {
                    maxY = y;
                }
            }
            System.Console.WriteLine("2. Registration Identifier:");
            for (int y = maxY; y >= minY; y--)  // y goes down for printing
            {
                for (int x = minX; x <= maxX; x++)
                {
                    if (white.Contains((x, y)))
                    {
                        System.Console.Write("\u2587");
                    }