예제 #1
0
        public void Run()
        {
            Computer.Run();

            while (Computer.IsRunning)
            {
                Tick();
            }
        }
예제 #2
0
        public static void Problems()
        {
            Console.WriteLine("Input file:");
            string fileName = Console.ReadLine();

            while (!File.Exists(fileName))
            {
                Console.WriteLine("file not found - try again:");
                fileName = Console.ReadLine();
            }

            //inputs
            //north (1), south (2), west (3), and east (4)

            //outputs

            /*
             *  0: The repair droid hit a wall. Its position has not changed.
             *  1: The repair droid has moved one step in the requested direction.
             *  2: The repair droid has moved one step in the requested direction; its new position is the location of the oxygen system.
             *
             */

            bool done = false;

            Dictionary <int, HashSet <int> > walls = new Dictionary <int, HashSet <int> >();
            Dictionary <int, HashSet <int> > found = new Dictionary <int, HashSet <int> >();
            Queue <DroidPath> next  = new Queue <DroidPath>();
            DroidPath         start = new DroidPath();

            found[0] = new HashSet <int>();
            found[0].Add(0);
            AddNextPaths(next, start, walls, found);

            DroidPath oxygenLocation = new DroidPath();

            while (!done && next.Count > 0)
            {
                //dequeue next item
                DroidPath d = next.Dequeue();
                //run computer to end of inputs
                Computer c = new Computer(fileName);
                foreach (int m in d.stepOrder)
                {
                    c.Processor.Input(m);
                }
                c.Run();

                //read last output queue item
                long[] output = c.Processor.OutputQueue.ToArray();
                switch (output[^ 1])
예제 #3
0
        public override string Part2()
        {
            var computer = new Computer(IntCodeData, 2);

            while (!computer.Finished)
            {
                var output = computer.Run();
                if (output.HasValue)
                {
                    return(output.Value.ToString());
                }
            }

            return(null);
        }
예제 #4
0
        public static void Problems()
        {
            Console.WriteLine("Input file:");
            string fileName = Console.ReadLine();

            while (!File.Exists(fileName))
            {
                Console.WriteLine("file not found - try again:");
                fileName = Console.ReadLine();
            }
            var  c    = new Computer(fileName);
            bool quit = false;

            while (!quit)
            {
                c.Run();
                while (c.Output.Count > 0)
                {
                    char x = (char)c.Output.Dequeue();

                    if (x == 10)
                    {
                        Console.Write("\n");
                    }
                    else
                    {
                        Console.Write(x);
                    }
                }
                string input = Console.ReadLine();
                if (input == "RESET")
                {
                    c = new Computer(fileName);
                }
                else if (input == "QUIT")
                {
                    quit = true;
                }
                else
                {
                    foreach (char x in input)
                    {
                        c.Input(x);
                    }
                    c.Input(10);
                }
            }
        }
예제 #5
0
        public override string Part1()
        {
            _sequences = GenerateSequence('0', '1', '2', '3', '4');

            var maxOutput = 0L;

            foreach (var sequence in _sequences)
            {
                var output = 0L;
                foreach (var phase in sequence)
                {
                    var computer  = new Computer(IntCodeData, phase);
                    var tmpOutput = computer.Run(output);
                    output = tmpOutput.HasValue ? tmpOutput.Value : 0;
                }

                maxOutput = Math.Max(maxOutput, output);
            }

            return(maxOutput.ToString());
        }
예제 #6
0
        public static void Problems()
        {
            Console.WriteLine("Input file:");
            string fileName = Console.ReadLine();

            while (!File.Exists(fileName))
            {
                Console.WriteLine("file not found - try again:");
                fileName = Console.ReadLine();
            }
            Computer c = new Computer(fileName);

            c.Run();
            long[] output = c.Processor.OutputQueue.ToArray();
            int    blocks = 0;

            for (int i = 2; i < output.Length; i += 3)
            {
                if (output[i] == 2)
                {
                    blocks++;
                }
            }
            Console.WriteLine(blocks);

            //added to determine screen size for part 2
            long maxX = 0;
            long maxY = 0;

            for (int i = 0; i < output.Length; i += 3)
            {
                maxX = Math.Max(maxX, output[i]);
                maxY = Math.Max(maxY, output[i + 1]);
            }

            Console.WriteLine($"maxX:{maxX} maxY:{maxY}");
        }
예제 #7
0
        public static void Problems()
        {
            Console.WriteLine("Input file:");
            string fileName = Console.ReadLine();

            while (!File.Exists(fileName))
            {
                Console.WriteLine("file not found - try again:");
                fileName = Console.ReadLine();
            }

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();


            var comp = new Computer(fileName);

            string[] instructions = new string[] {
                "NOT A J\n",
                "NOT B T\n",
                "OR J T\n",
                "NOT C J\n",
                "OR J T\n",
                "OR T J\n",
                "AND D J\n",                //at this point, will jump if D is present unless ABCD
                "WALK\n"
            };
            foreach (string s in instructions)
            {
                foreach (char c in s)
                {
                    comp.Input(c);
                }
            }
            comp.Run();
            StringBuilder sb = new StringBuilder();

            while (comp.Output.Count > 0)
            {
                long next = comp.Output.Dequeue();
                if (next > 128)
                {
                    sb.Append(next.ToString());
                    sb.Append(Environment.NewLine);
                }
                else
                {
                    sb.Append((char)next);
                }
            }
            Console.Write(sb);
            Console.WriteLine("");
            Console.WriteLine("Part 2:");

            instructions = new string[] {
                "NOT A J\n",
                "NOT B T\n",
                "OR J T\n",
                "NOT C J\n",
                "OR J T\n",
                "OR T J\n",
                "AND D J\n",                //at this point, will jump if D is present unless ABCD
                "AND E T\n",
                "OR H T\n",
                "AND T J\n",                //same as step 1 execpt don't jump if !(E|H) -- don't jump if can neither jump nor walk after this jump
                "RUN\n"
            };
            comp = new Computer(fileName);
            foreach (string s in instructions)
            {
                foreach (char c in s)
                {
                    comp.Input(c);
                }
            }
            comp.Run();
            while (comp.Output.Count > 0)
            {
                long next = comp.Output.Dequeue();
                if (next > 128)
                {
                    sb.Append(next.ToString());
                    sb.Append(Environment.NewLine);
                }
                else
                {
                    sb.Append((char)next);
                }
            }
            Console.Write(sb);

            Console.WriteLine("");
            stopwatch.Stop();
            Console.WriteLine($"Time to complete (ms): {stopwatch.ElapsedMilliseconds}");
        }
예제 #8
0
        public static void Problem2()
        {
            Console.WriteLine("Input file:");
            string fileName = Console.ReadLine();

            while (!File.Exists(fileName))
            {
                Console.WriteLine("file not found - try again:");
                fileName = Console.ReadLine();
            }


            Computer c = new Computer(fileName);

            c.Ram[0] = 2;
            while (c.Processor.InstructionPointer >= 0)
            {
                c.Run();
                long[] output = c.Processor.OutputQueue.ToArray();
                c.Processor.OutputQueue.Clear();
                int eX = 0;
                int oX = 0;
                for (int i = 0; i < output.Length - 2; i += 3)
                {
                    int x = Convert.ToInt32(output[i]);
                    int y = Convert.ToInt32(output[i + 1]);

                    if (x == -1 && y == 0)
                    {
                        Console.SetCursorPosition(0, 0);
                        Console.Write($"Score: {output[i + 2].ToString().PadLeft(6, '0')}");
                    }
                    else
                    {
                        Console.SetCursorPosition(x, y + 2);
                        switch (output[i + 2])
                        {
                        case 0:
                            Console.Write(" ");
                            break;

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

                        case 2:
                            Console.Write("#");
                            break;

                        case 3:
                            Console.Write("=");
                            eX = x;
                            break;

                        case 4:
                            Console.Write("o");
                            oX = x;
                            break;
                        }
                    }
                }                //draw screen for loop

                //move paddle to track the ball
                c.Processor.Input(oX.CompareTo(eX));
            }
            ;

            {
                //draw any remaining output after game ends
                long[] output = c.Processor.OutputQueue.ToArray();
                for (int i = 0; i < output.Length - 2; i += 3)
                {
                    int x = Convert.ToInt32(output[i]);
                    int y = Convert.ToInt32(output[i + 1]);

                    if (x == -1 && y == 0)
                    {
                        Console.SetCursorPosition(0, 0);
                        Console.Write($"Score: {output[i + 2].ToString().PadLeft(6, '0')}");
                    }
                    else
                    {
                        Console.SetCursorPosition(x, y + 2);
                        switch (output[i + 2])
                        {
                        case 1:
                            Console.Write('|');
                            break;

                        case 2:
                            Console.Write("#");
                            break;

                        case 3:
                            Console.Write("=");
                            break;

                        case 4:
                            Console.Write("o");
                            break;
                        }
                    }
                }                //draw screen for loop
            }
            Console.SetCursorPosition(0, 22);
            Console.Write("Press any key to end");
            Console.ReadKey();
        }
예제 #9
0
        public static void Problems()
        {
            Console.WriteLine("Input file:");
            string fileName = Console.ReadLine();

            while (!File.Exists(fileName))
            {
                Console.WriteLine("file not found - try again:");
                fileName = Console.ReadLine();
            }
            List <Computer> computers = new List <Computer>();

            for (int i = 0; i < 50; i++)
            {
                var computer = new Computer(fileName);
                computer.Input(i);
                computer.Run();
                computers.Add(computer);
            }

            bool part1Complete = false;
            bool part2Complete = false;

            long          natX          = 0;
            long          natY          = 0;
            HashSet <int> waiting       = new HashSet <int>();
            long          natYDelivered = Int64.MinValue;

            while (!part1Complete || !part2Complete)
            {
                for (int i = 0; i < 50; i++)
                {
                    if (i == 0 && waiting.Count == 50)
                    {
                        computers[i].Input(natX);
                        computers[i].Input(natY);
                        if (natY == natYDelivered)
                        {
                            part2Complete = true;
                            Console.WriteLine($"problem 2: {natYDelivered}");
                        }
                        else
                        {
                            natYDelivered = natY;
                        }
                        waiting.Remove(0);
                    }

                    //check for messages on the output queue
                    while (computers[i].Output.Count > 0)
                    {
                        int  destination = Convert.ToInt32(computers[i].Output.Dequeue());
                        long x           = computers[i].Output.Dequeue();
                        long y           = computers[i].Output.Dequeue();

                        if (destination == 255)
                        {
                            natX = x;
                            natY = y;
                            if (!part1Complete)
                            {
                                Console.WriteLine($"part 1: {y}");
                                part1Complete = true;
                            }
                        }
                        else
                        {
                            //deliver messages to the appropriate computer
                            computers[destination].Input(x);
                            computers[destination].Input(y);
                            if (waiting.Contains(destination))
                            {
                                waiting.Remove(destination);
                            }
                        }
                    }

                    //if this computer's input queue is empty, queue -1 per Advent of Code day 23 instructions
                    if (!computers[i].Processor.HasInput)
                    {
                        waiting.Add(i);
                        computers[i].Input(-1);
                    }
                    else
                    {
                        if (waiting.Contains(i))
                        {
                            waiting.Remove(i);
                        }
                    }

                    computers[i].Run();
                }
            }
        }
예제 #10
0
        public static void Problems()
        {
            Console.WriteLine("Input file:");
            string fileName = Console.ReadLine();

            while (!File.Exists(fileName))
            {
                Console.WriteLine("file not found - try again:");
                fileName = Console.ReadLine();
            }

            int output = 0;

            for (int x = 0; x < 50; x++)
            {
                for (int y = 0; y < 50; y++)
                {
                    Computer c = new Computer(fileName);
                    c.Input(x);
                    c.Input(y);
                    c.Run();
                    if (c.Output.Dequeue() == 1)
                    {
                        output++;
                    }
                }
            }


            Console.WriteLine(output);


            //part 2
            bool foundAnswer = false;
            int  x1          = 0;

            for (int y = 0; y < 10000 && !foundAnswer; y++)
            {
                bool found1 = false;
                for (int x = x1; x < 10000 && !found1; x++)
                {
                    Computer c = new Computer(fileName);
                    c.Input(x);
                    c.Input(y);
                    c.Run();
                    long val = c.Output.Dequeue();
                    if (val == 1)
                    {
                        found1 = true;
                        x1     = x;
                        //go down 99 rows, then over until the first 1 is found - mark that as tempX for now
                        int  tempX = x;
                        int  tempY = y + 99;
                        long val2  = 0;
                        while (val2 == 0 && tempX < 10000)
                        {
                            Computer c2 = new Computer(fileName);
                            c2.Input(tempX);
                            c2.Input(tempY);
                            c2.Run();
                            val2 = c2.Output.Dequeue();
                            if (val2 == 0)
                            {
                                tempX++;
                            }
                        }
                        //check if tempX+99 at original y is a 1
                        Computer c3 = new Computer(fileName);
                        c3.Input(tempX + 99);
                        c3.Input(y);
                        c3.Run();
                        long val3 = c3.Output.Dequeue();
                        if (val3 == 1)
                        {
                            foundAnswer = true;
                            Console.WriteLine(tempX * 10000 + y);
                        }
                    }
                } //for loop x
            }     //for loop y
        }         //Problems()
예제 #11
0
        public static void Problems()
        {
            bool visualize = false;

            Console.WriteLine("Input file:");
            string fileName = Console.ReadLine();

            while (!File.Exists(fileName))
            {
                Console.WriteLine("file not found - try again:");
                fileName = Console.ReadLine();
            }

            Computer comp = new Computer(fileName);

            comp.Input(76);
            comp.Run();
            List <string> lines = new List <string>();

            HashSet <int> sc = new HashSet <int>();

            StringBuilder s = new StringBuilder();

            while (comp.Output.Count > 0)
            {
                long next = comp.Output.Dequeue();
                if (next == 10)
                {
                    if (s.Length > 0)
                    {
                        lines.Add(s.ToString());
                    }
                    s = new StringBuilder();
                }
                else
                {
                    s.Append((char)next);
                }
            }
            if (s.Length > 0)
            {
                lines.Add(s.ToString());
            }


            long AP = 0;

            for (int r = 1; r < lines.Count - 1; r++)
            {
                for (int c = 1; c < lines[r].Length - 1; c++)
                {
                    //check this and left/right/top/bottom for #^v<>
                    if (IsScaffold(lines[r][c]) && IsScaffold(lines[r - 1][c]) && IsScaffold(lines[r + 1][c]) && IsScaffold(lines[r][c - 1]) && IsScaffold(lines[r][c + 1]))
                    {
                        AP += r * c;
                    }
                }
            }



            /*Visualization*/
            if (visualize)
            {
                foreach (string line in lines)
                {
                    Console.WriteLine(line);
                }
            }

            //part 2...

            //(find robot)
            int x = 0;
            int y = 0;

            for (int r = 0; r < lines.Count; r++)
            {
                for (int c = 0; c < lines[r].Length; c++)
                {
                    if (IsScaffold(lines[r][c]))
                    {
                        sc.Add(r * 100 + c);
                    }
                    if (IsRobot(lines[r][c]))
                    {
                        x = c;
                        y = r;
                    }
                }
            }


            //find full walk path string
            //first attempt - don't turn until necessary
            // (visualization of provided input says this is possible with only one way to turn at each endpoint)
            StringBuilder path  = new StringBuilder();
            HashSet <int> found = new HashSet <int>();

            found.Add(x * 100 + y);
            int  forward   = 0;
            char direction = lines[y][x];

            while (found.Count < sc.Count)
            {
                switch (direction)
                {
                case '^':
                    if (y != 0 && IsScaffold(lines[y - 1][x]))
                    {
                        forward++;
                        y--;
                        found.Add(x * 100 + y);
                    }
                    else if (x != 0 && IsScaffold(lines[y][x - 1]))
                    {
                        path.Append(forward.ToString());
                        forward = 0;
                        path.Append("L");
                        direction = '<';
                    }
                    else if (x != (lines[y].Length - 1) && IsScaffold(lines[y][x + 1]))
                    {
                        path.Append(forward.ToString());
                        forward = 0;
                        path.Append("R");
                        direction = '>';
                    }
                    else
                    {
                        throw new Exception("no where to go");
                    }

                    break;

                case '>':
                    if (x != (lines[y].Length - 1) && IsScaffold(lines[y][x + 1]))
                    {
                        forward++;
                        x++;
                        found.Add(x * 100 + y);
                    }
                    else if (y != 0 && IsScaffold(lines[y - 1][x]))
                    {
                        path.Append(forward.ToString());
                        forward = 0;
                        path.Append("L");
                        direction = '^';
                    }
                    else if (y != (lines.Count - 1) && IsScaffold(lines[y + 1][x]))
                    {
                        path.Append(forward.ToString());
                        forward = 0;
                        path.Append("R");
                        direction = 'v';
                    }
                    else
                    {
                        throw new Exception("no where to go");
                    }

                    break;

                case '<':

                    if (x != 0 && IsScaffold(lines[y][x - 1]))
                    {
                        forward++;
                        x--;
                        found.Add(x * 100 + y);
                    }
                    else if (y != 0 && IsScaffold(lines[y - 1][x]))
                    {
                        path.Append(forward.ToString());
                        forward = 0;
                        path.Append("R");
                        direction = '^';
                    }
                    else if (y != (lines.Count - 1) && IsScaffold(lines[y + 1][x]))
                    {
                        path.Append(forward.ToString());
                        forward = 0;
                        path.Append("L");
                        direction = 'v';
                    }
                    else
                    {
                        throw new Exception("no where to go");
                    }
                    break;

                case 'v':
                    if (y != (lines.Count - 1) && IsScaffold(lines[y + 1][x]))
                    {
                        forward++;
                        y++;
                        found.Add(x * 100 + y);
                    }
                    else if (x != 0 && IsScaffold(lines[y][x - 1]))
                    {
                        path.Append(forward.ToString());
                        forward = 0;
                        path.Append("R");
                        direction = '<';
                    }
                    else if (x != (lines[y].Length - 1) && IsScaffold(lines[y][x + 1]))
                    {
                        path.Append(forward.ToString());
                        forward = 0;
                        path.Append("L");
                        direction = '>';
                    }
                    else
                    {
                        throw new Exception("no where to go");
                    }

                    break;
                }
            }

            path.Append(forward);



            //break into combinations of 3 substrings not greater than 20 characters (including commas between letters and numbers )
            //done visually - may rewrite with algorithm later to work with other possible input paths...

            /*
             * L4L4L6R10L6 L4L4L6R10L6 L12L6R10L6 R8R10L6 R8R10L6 L4L4L6R10L6 R8R10L6 L12L6R10L6 R8R10L6 L12L6R10L6
             * AAAAAAAAAAA AAAAAAAAAAA BBBBBBBBBB CCCCCCC CCCCCCC AAAAAAAAAAA CCCCCCC BBBBBBBBBB CCCCCCC BBBBBBBBBB
             * */

            String MainInstructions = "A,A,B,C,C,A,C,B,C,B";
            String A = "L,4,L,4,L,6,R,10,L,6";
            String B = "L,12,L,6,R,10,L,6";
            String C = "R,8,R,10,L,6";



            Computer c2 = new Computer(fileName);

            c2.Ram[0] = 2;
            foreach (var ins in MainInstructions)
            {
                c2.Input(ins);
            }
            c2.Input(10);
            foreach (var ins in A)
            {
                c2.Input(ins);
            }
            c2.Input(10);
            foreach (var ins in B)
            {
                c2.Input(ins);
            }
            c2.Input(10);
            foreach (var ins in C)
            {
                c2.Input(ins);
            }
            c2.Input(10);
            c2.Input(visualize ? 'y' : 'n');
            c2.Input(10);
            c2.Run();

            //Console.WriteLine();
            StringBuilder output = new StringBuilder();

            while (c2.Output.Count > 1)
            {
                long val = c2.Output.Dequeue();
                if (visualize)
                {
                    if (val == 10)
                    {
                        Console.WriteLine(output.ToString());
                        if (output.Length == 0)
                        {
                            //for visualization of map
                            System.Threading.Thread.Sleep(45);
                            Console.Clear();
                        }
                        output = new StringBuilder();
                    }
                    else
                    {
                        output.Append((char)val);
                    }
                }
            }
            //if (output.Length > 0) Console.WriteLine(output.ToString());
            long result = c2.Output.Dequeue();


            //print expected directions
            if (visualize)
            {
                Console.WriteLine(path.ToString().TrimStart('0'));
            }
            //result with provided input was L4L4L6R10L6L4L4L6R10L6L12L6R10L6R8R10L6R8R10L6L4L4L6R10L6R8R10L6L12L6R10L6R8R10L6L12L6R10L6

            Console.WriteLine($"Part 1: {AP}");
            Console.WriteLine($"Part 2: {result}");
        }
예제 #12
0
        public static void Problem()
        {
            Console.WriteLine("Input file:");
            string fileName = Console.ReadLine();

            while (!File.Exists(fileName))
            {
                Console.WriteLine("file not found - try again:");
                fileName = Console.ReadLine();
            }
            int x         = 0;
            int y         = 0;
            var colors    = new Dictionary <int, Dictionary <int, long> >();
            int direction = 0;

            Computer c = new Computer(fileName);

            c.Run();
            while (c.Processor.InstructionPointer >= 0)
            {
                //read output queue, move as instructed

                while (c.Processor.OutputQueue.Count > 0)
                {
                    long colorToPaint = c.Processor.OutputQueue.Dequeue();
                    if (!colors.ContainsKey(x))
                    {
                        colors[x] = new Dictionary <int, long>();
                    }
                    colors[x][y] = colorToPaint;
                    direction    = Turn(direction, c.Processor.OutputQueue.Dequeue());
                    switch (direction)
                    {
                    case 0:
                        y--;
                        break;

                    case 90:
                        x++;
                        break;

                    case 180:
                        y++;
                        break;

                    case 270:
                        x--;
                        break;
                    }
                }

                if (!colors.ContainsKey(x) || !colors[x].ContainsKey(y))
                {
                    c.Processor.Input(0);
                }
                else
                {
                    c.Processor.Input(colors[x][y]);
                }

                c.Run();
            }

            //read any remaining queue once processor is complete
            while (c.Processor.OutputQueue.Count > 0)
            {
                long colorToPaint = c.Processor.OutputQueue.Dequeue();
                if (!colors.ContainsKey(x))
                {
                    colors[x] = new Dictionary <int, long>();
                }
                colors[x][y] = colorToPaint;
                direction    = Turn(direction, c.Processor.OutputQueue.Dequeue());
                switch (direction)
                {
                case 0:
                    y--;
                    break;

                case 90:
                    x++;
                    break;

                case 180:
                    y++;
                    break;

                case 270:
                    x--;
                    break;
                }
            }

            //count painted cells

            int painted = 0;

            foreach (int keyX in colors.Keys)
            {
                foreach (int keyY in colors[keyX].Keys)
                {
                    painted++;
                }
            }

            Console.WriteLine(painted);
        }
예제 #13
0
        public static void Problem2()
        {
            Console.WriteLine("Input file:");
            string fileName = Console.ReadLine();

            while (!File.Exists(fileName))
            {
                Console.WriteLine("file not found - try again:");
                fileName = Console.ReadLine();
            }
            int x         = 0;
            int y         = 0;
            var colors    = new Dictionary <int, Dictionary <int, long> >();
            int direction = 0;

            Computer c = new Computer(fileName);

            //tell computer first input is painted;
            c.Processor.Input(1);
            c.Run();
            while (c.Processor.InstructionPointer >= 0)
            {
                //read output queue, move as instructed

                while (c.Processor.OutputQueue.Count > 0)
                {
                    long colorToPaint = c.Processor.OutputQueue.Dequeue();
                    if (!colors.ContainsKey(x))
                    {
                        colors[x] = new Dictionary <int, long>();
                    }
                    colors[x][y] = colorToPaint;
                    direction    = Turn(direction, c.Processor.OutputQueue.Dequeue());
                    switch (direction)
                    {
                    case 0:
                        y--;
                        break;

                    case 90:
                        x++;
                        break;

                    case 180:
                        y++;
                        break;

                    case 270:
                        x--;
                        break;
                    }
                }

                if (!colors.ContainsKey(x) || !colors[x].ContainsKey(y))
                {
                    c.Processor.Input(0);
                }
                else
                {
                    c.Processor.Input(colors[x][y]);
                }

                c.Run();
            }

            //read any remaining queue once processor is complete
            while (c.Processor.OutputQueue.Count > 0)
            {
                long colorToPaint = c.Processor.OutputQueue.Dequeue();
                if (!colors.ContainsKey(x))
                {
                    colors[x] = new Dictionary <int, long>();
                }
                colors[x][y] = colorToPaint;
                direction    = Turn(direction, c.Processor.OutputQueue.Dequeue());
                switch (direction)
                {
                case 0:
                    y--;
                    break;

                case 90:
                    x++;
                    break;

                case 180:
                    y++;
                    break;

                case 270:
                    x--;
                    break;
                }
            }

            int minX = 0;
            int maxX = 0;
            int minY = 0;
            int maxY = 0;

            foreach (int xKey in colors.Keys)
            {
                minX = Math.Min(xKey, minX);
                maxX = Math.Max(xKey, maxX);
                foreach (int yKey in colors[xKey].Keys)
                {
                    minY = Math.Min(yKey, minY);
                    maxY = Math.Max(yKey, maxY);
                }
            }

            for (int y1 = minY; y1 <= maxY; y1++)
            {
                StringBuilder s = new StringBuilder();
                for (int x1 = minX; x1 <= maxX; x1++)
                {
                    if (!colors.ContainsKey(x1) || !colors[x1].ContainsKey(y1))
                    {
                        s.Append(" ");
                    }
                    else
                    {
                        s.Append(colors[x1][y1] == 0 ? " " : "#");
                    }
                }
                Console.WriteLine(s.ToString());
            }
        }