Beispiel #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}");
            }
        }
Beispiel #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;
Beispiel #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());
        }
Beispiel #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();
                                }
Beispiel #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");
                    }
Beispiel #6
0
        //public ImmutableIntCodeMachine(string stPrg) : this(new IntCodeMachine(stPrg))
        //{
        //}

        private ImmutableIntCodeMachine(IntCodeMachine icm)
        {
            this.icm = icm;
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            const int N_AMP = 5;    // Number of amplifiers

            // Read input and create IntCode instances
            string         filename = @"input";
            IntCodeMachine amp;

            IntCodeMachine[] amplifier;
            using (StreamReader sr = new StreamReader(filename))
            {
                // All data is on the first line
                int[] data = sr.ReadLine().Trim().Split(",").Select(s => Int32.Parse(s)).ToArray();

                // Only 1 IntCode is needed, as they all use the same instructions, and they reset
                // their state between runs anyway.  Therefor we can reuse the amplifier.
                amp = new IntCodeMachine("Part1-test", data);

                // For part 2, we do need distinct amplifiers
                amplifier = new IntCodeMachine[N_AMP];
                for (int i = 0; i < N_AMP; i++)
                {
                    amplifier[i] = new IntCodeMachine(i.ToString(), data);
                }
            }

            System.Console.WriteLine("Day 07");

            int[] output         = new int[N_AMP];
            int   thrusterOutput = 0;

            for (int a = 0; a < N_AMP; a++)
            {
                output[0] = amp.Run(new int[] { a, 0 }, true);
                for (int b = 0; b < N_AMP; b++)
                {
                    if (b == a)
                    {
                        continue;
                    }
                    output[1] = amp.Run(new int[] { b, output[0] }, true);
                    for (int c = 0; c < N_AMP; c++)
                    {
                        if (c == a || c == b)
                        {
                            continue;
                        }
                        output[2] = amp.Run(new int[] { c, output[1] }, true);
                        for (int d = 0; d < N_AMP; d++)
                        {
                            if (d == a || d == b || d == c)
                            {
                                continue;
                            }
                            output[3] = amp.Run(new int[] { d, output[2] }, true);
                            int e = 10 - a - b - c - d; // No loop needed!
                            output[4] = amp.Run(new int[] { e, output[3] }, true);
                            if (output[4] > thrusterOutput)
                            {
                                thrusterOutput = output[4];
                            }
                        }
                    }
                }
            }
            System.Console.WriteLine($"1. {thrusterOutput}");

            // Part 2
            thrusterOutput = 0;
            int intermediateResult = 0;

            int[] phase, bestPhase = null;
            for (int a = 5; a < 10; a++)
            {
                for (int b = 5; b < 10; b++)
                {
                    if (b == a)
                    {
                        continue;
                    }
                    for (int c = 5; c < 10; c++)
                    {
                        if (c == a || c == b)
                        {
                            continue;
                        }
                        for (int d = 5; d < 10; d++)
                        {
                            if (d == a || d == b || d == c)
                            {
                                continue;
                            }
                            int e = 35 - a - b - c - d; // No loop needed!

                            // Starting the machines also resets their state
                            phase = new int[] { a, b, c, d, e };
                            amplifier[0].Start(new int[] { phase[0], 0 }, out output[0]);
                            amplifier[1].Start(new int[] { phase[1], output[0] }, out output[1]);
                            amplifier[2].Start(new int[] { phase[2], output[1] }, out output[2]);
                            amplifier[3].Start(new int[] { phase[3], output[2] }, out output[3]);
                            if (amplifier[4].Start(new int[] { phase[4], output[3] }, out output[4]))
                            {
                                intermediateResult = output[4];
                            }
                            while (amplifier[0].active)
                            {
                                amplifier[0].Next(new int[] { output[4] }, out output[0]);
                                amplifier[1].Next(new int[] { output[0] }, out output[1]);
                                amplifier[2].Next(new int[] { output[1] }, out output[2]);
                                amplifier[3].Next(new int[] { output[2] }, out output[3]);

                                // output is always overwritten, even if the machine has no output
                                // Next() return true iff the machine actually has output (and the
                                // value is meaningful)
                                if (amplifier[4].Next(new int[] { output[3] }, out output[4]))
                                {
                                    intermediateResult = output[4];
                                }
                            }

                            if (intermediateResult > thrusterOutput)
                            {
                                thrusterOutput = intermediateResult;
                                bestPhase      = phase;
                            }
                        }
                    }
                }
            }
            System.Console.WriteLine($"2. {thrusterOutput} [{bestPhase[0]},{bestPhase[1]},{bestPhase[2]},{bestPhase[3]},{bestPhase[4]}]");
        }