示例#1
0
        public override string SolutionOne()
        {
            var keypad = new int[3, 3]
            {
                { 1, 2, 3 },
                { 4, 5, 6 },
                { 7, 8, 9 }
            };

            var coOrd = new Utils.CoOrd(1, 1);

            var code = "";

            foreach (var line in Input)
            {
                foreach (var instruction in line)
                {
                    switch (instruction)
                    {
                    //Up, Down, Left, Right
                    case 'U':
                        if (coOrd.y > 0)
                        {
                            coOrd.y--;
                        }
                        break;

                    case 'D':
                        if (coOrd.y < 2)
                        {
                            coOrd.y++;
                        }
                        break;

                    case 'L':
                        if (coOrd.x > 0)
                        {
                            coOrd.x--;
                        }
                        break;

                    default:
                        if (coOrd.x < 2)
                        {
                            coOrd.x++;
                        }
                        break;
                    }
                }

                code += keypad[coOrd.y, coOrd.x];
            }


            return(code);
        }
示例#2
0
        public override string SolutionOne()
        {
            var positions = new Dictionary <Utils.CoOrd, int>();

            var pathnum = 1;

            foreach (var input in Input)
            {
                var path = input.Split(',');
                var pos  = new Utils.CoOrd(0, 0);
                foreach (var pathsect in path)
                {
                    var direction = pathsect.Substring(0, 1);
                    var length    = int.Parse(pathsect.Substring(1));

                    int newy;
                    int newx;
                    switch (direction)
                    {
                    case "U":
                        newy = pos.y + length;
                        for (var y = pos.y + 1; y <= newy; y++)
                        {
                            var newpos = new Utils.CoOrd(pos.x, y);
                            if (!positions.ContainsKey(newpos))
                            {
                                positions.Add(newpos, pathnum);
                            }
                            else
                            {
                                if (positions[newpos] < pathnum)
                                {
                                    positions[newpos] = positions[newpos] + pathnum;
                                }
                            }

                            pos.y++;
                        }
                        break;

                    case "D":
                        newy = pos.y - length;
                        for (var y = pos.y - 1; y >= newy; y--)
                        {
                            var newpos = new Utils.CoOrd(pos.x, y);
                            if (!positions.ContainsKey(newpos))
                            {
                                positions.Add(newpos, pathnum);
                            }
                            else
                            {
                                if (positions[newpos] < pathnum)
                                {
                                    positions[newpos] = positions[newpos] + pathnum;
                                }
                            }

                            pos.y--;
                        }
                        break;

                    case "R":
                        newx = pos.x + length;
                        for (var x = pos.x + 1; x <= newx; x++)
                        {
                            var newpos = new Utils.CoOrd(x, pos.y);
                            if (!positions.ContainsKey(newpos))
                            {
                                positions.Add(newpos, pathnum);
                            }
                            else
                            {
                                if (positions[newpos] < pathnum)
                                {
                                    positions[newpos] = positions[newpos] + pathnum;
                                }
                            }

                            pos.x++;
                        }
                        break;

                    case "L":
                        newx = pos.x - length;
                        for (var x = pos.x - 1; x >= newx; x--)
                        {
                            var newpos = new Utils.CoOrd(x, pos.y);
                            if (!positions.ContainsKey(newpos))
                            {
                                positions.Add(newpos, pathnum);
                            }
                            else
                            {
                                if (positions[newpos] < pathnum)
                                {
                                    positions[newpos] = positions[newpos] + pathnum;
                                }
                            }

                            pos.x--;
                        }
                        break;
                    }
                }

                pathnum++;
            }

            var crosses = positions.Where(p => p.Value == 3).ToList();

            crosses = crosses.OrderBy(p => Math.Abs(p.Key.x) + Math.Abs(p.Key.y)).ToList();

            return((Math.Abs(crosses[0].Key.x) + Math.Abs(crosses[0].Key.y)).ToString());
        }
示例#3
0
        public override string SolutionTwo()
        {
            var keypad = new char[5, 5]
            {
                { '-', '-', '1', '-', '-' },
                { '-', '2', '3', '4', '-' },
                { '5', '6', '7', '8', '9' },
                { '-', 'A', 'B', 'B', '-' },
                { '-', '-', 'D', '-', '-' }
            };

            var coOrd = new Utils.CoOrd(0, 2);
            var code  = "";

            foreach (var line in Input)
            {
                foreach (var instruction in line)
                {
                    switch (instruction)
                    {
                    //Up, Down, Left, Right
                    case 'U':
                        if (coOrd.y > 0)
                        {
                            if (keypad[coOrd.y - 1, coOrd.x] != '-')
                            {
                                coOrd.y--;
                            }
                        }
                        break;

                    case 'D':
                        if (coOrd.y < 4)
                        {
                            if (keypad[coOrd.y + 1, coOrd.x] != '-')
                            {
                                coOrd.y++;
                            }
                        }
                        break;

                    case 'L':
                        if (coOrd.x > 0)
                        {
                            if (keypad[coOrd.y, coOrd.x - 1] != '-')
                            {
                                coOrd.x--;
                            }
                        }
                        break;

                    default:
                        if (coOrd.x < 4)
                        {
                            if (keypad[coOrd.y, coOrd.x + 1] != '-')
                            {
                                coOrd.x++;
                            }
                        }
                        break;
                    }
                }

                code += keypad[coOrd.y, coOrd.x];
            }

            return(code);


            throw new NotImplementedException();
        }
示例#4
0
        public override string SolutionTwo()
        {
            const int size   = 999;
            var       spiral = new int[size, size];

            var max = 325489;
            var num = 1;

            var centre    = new Utils.CoOrd((size - 1) / 2, (size - 1) / 2);
            var prevCoOrd = new Utils.CoOrd((size - 1) / 2, (size - 1) / 2);
            var curCoOrd  = new Utils.CoOrd((size - 1) / 2, (size - 1) / 2);

            var direction = "R";

            while (num <= max)
            {
                prevCoOrd = new Utils.CoOrd(curCoOrd.x, curCoOrd.y);

                spiral[curCoOrd.y, curCoOrd.x] = num;
                //RIGHT, UP, DOWN, LEFT
                switch (direction)
                {
                case "R":
                    curCoOrd.x++;
                    if (spiral[curCoOrd.y - 1, curCoOrd.x] == 0)
                    {
                        direction = "U";
                    }
                    break;

                case "U":
                    curCoOrd.y--;
                    if (spiral[curCoOrd.y, curCoOrd.x - 1] == 0)
                    {
                        direction = "L";
                    }
                    break;

                case "L":
                    curCoOrd.x--;
                    if (spiral[curCoOrd.y + 1, curCoOrd.x] == 0)
                    {
                        direction = "D";
                    }
                    break;

                case "D":
                    curCoOrd.y++;
                    if (spiral[curCoOrd.y, curCoOrd.x + 1] == 0)
                    {
                        direction = "R";
                    }
                    break;
                }

                var sum = 0;

                for (int i = -1; i <= 1; i++)
                {
                    for (int j = -1; j <= 1; j++)
                    {
                        sum += spiral[curCoOrd.y + i, curCoOrd.x + j];
                    }
                }

                num = sum;
            }

            return(num.ToString());
        }
示例#5
0
        public override string SolutionOne()
        {
            const int size   = 999;
            var       spiral = new int[size, size];

            var max = 325489;
            var num = 1;

            var centre    = new Utils.CoOrd((size - 1) / 2, (size - 1) / 2);
            var prevCoOrd = new Utils.CoOrd((size - 1) / 2, (size - 1) / 2);
            var curCoOrd  = new Utils.CoOrd((size - 1) / 2, (size - 1) / 2);

            var direction = "R";

            while (num <= max)
            {
                prevCoOrd = new Utils.CoOrd(curCoOrd.x, curCoOrd.y);

                spiral[curCoOrd.y, curCoOrd.x] = num;
                //RIGHT, UP, DOWN, LEFT
                switch (direction)
                {
                case "R":
                    curCoOrd.x++;
                    if (spiral[curCoOrd.y - 1, curCoOrd.x] == 0)
                    {
                        direction = "U";
                    }
                    break;

                case "U":
                    curCoOrd.y--;
                    if (spiral[curCoOrd.y, curCoOrd.x - 1] == 0)
                    {
                        direction = "L";
                    }
                    break;

                case "L":
                    curCoOrd.x--;
                    if (spiral[curCoOrd.y + 1, curCoOrd.x] == 0)
                    {
                        direction = "D";
                    }
                    break;

                case "D":
                    curCoOrd.y++;
                    if (spiral[curCoOrd.y, curCoOrd.x + 1] == 0)
                    {
                        direction = "R";
                    }
                    break;
                }

                num++;
            }

            var x = Math.Abs(prevCoOrd.x - centre.x);
            var y = Math.Abs(prevCoOrd.y - centre.y);

            var steps = x + y;

            return(steps.ToString());
        }