Example #1
0
        public static string firstPuzzle(string input)
        {
            long[]      intcode = input.Split(',').Select(long.Parse).ToArray();
            IntComputer ic      = new IntComputer(intcode);
            int         i       = 499;
            int         j       = 499;
            int         compass = 2;
            List <Tuple <int, int> > painted = new List <Tuple <int, int> >();

            int[,] hull = new int[1000, 1000];
            ic.inputs.Add(hull[i, j]);
            ic.compute();
            while (ic.isWaiting)
            {
                //Paint
                //elem1: 0 = blck, 1 = wht, elem2: 0 = left, 1 = right
                if (hull[i, j] != (int)ic.outputs.ElementAt(0))
                {
                    hull[i, j] = (int)ic.outputs.ElementAt(0);
                    painted.Add(new Tuple <int, int>(i, j));
                }
                ic.outputs.RemoveAt(0);
                //Rotate
                compass = rotateCompass(compass, (int)ic.outputs.ElementAt(0));
                ic.outputs.RemoveAt(0);
                //Move
                if (compass == 1)
                {
                    j--;
                }
                else if (compass == 2)
                {
                    i--;
                }
                else if (compass == 3)
                {
                    j++;
                }
                else if (compass == 4)
                {
                    i++;
                }
                ic.inputs.Add(hull[i, j]);
                ic.compute();
            }
            var result = painted.GroupBy(key => key, item => 1)
                         .Select(group => new
            {
                group.Key,
                Duplicates = group.Count()
            }).ToList();

            return(result.Count.ToString());
        }
Example #2
0
        public static string secondPuzzle(string input)
        {
            long[]      intcode = input.Split(',').Select(long.Parse).ToArray();
            IntComputer ic      = new IntComputer(intcode);

            ic.inputs.Add((long)2);
            ic.compute();
            List <long> output = ic.outputs;

            return(string.Join(',', output));
        }
Example #3
0
        public string secondPuzzle(string input)
        {
            long[] intcode = input.Split(',').Select(long.Parse).ToArray();
            ic = new IntComputer(intcode);



            Point og = new Point {
                X = 0, Y = 0
            };

            StartPoint = og;

            CheckAdjacentPoints(og);

            var minX = SeenPoints.Keys.Min(k => k.X);
            var maxX = SeenPoints.Keys.Max(k => k.X);
            var minY = SeenPoints.Keys.Min(k => k.Y);
            var maxY = SeenPoints.Keys.Max(k => k.Y);

            var offsetX = Math.Abs(minX);
            var offsetY = Math.Abs(minY);

            var sizeX = maxX + offsetX;
            var sizeY = maxY + offsetY;

            StartPoint = new Point {
                X = StartPoint.X + offsetX, Y = StartPoint.Y + offsetY
            };
            TargetPoint = new Point {
                X = TargetPoint.X + offsetX, Y = TargetPoint.Y + offsetY
            };

            Board     = new long[sizeY + 1, sizeX + 1];
            FloodLvls = new long[sizeY + 1, sizeX + 1];
            Seen      = new bool[sizeY + 1, sizeX + 1];

            foreach (var p in SeenPoints)
            {
                Board[p.Key.Y + offsetY, p.Key.X + offsetX] = p.Value;
            }

            FloodFill(TargetPoint);
            long timeStepsToFlood = FloodLvls.Cast <long>().Max();

            return(timeStepsToFlood.ToString());
        }
Example #4
0
        public static string firstPuzzle(string input)
        {
            long[]      intcode = input.Split(',').Select(long.Parse).ToArray();
            IntComputer ic      = new IntComputer(intcode);

            int blocks = 0;

            ic.compute();
            Console.WriteLine(ic.isWaiting.ToString());
            for (int i = 2; i < ic.outputs.Count; i += 3)
            {
                if (ic.outputs[i] == 2)
                {
                    blocks++;
                }
            }

            return(blocks.ToString());
        }
Example #5
0
        public static string firstPuzzle(string input)
        {
            long[]      intcode = input.Split(',').Select(long.Parse).ToArray();
            IntComputer ic      = new IntComputer(intcode);

            ic.compute();

            int[,] scaffolding = new int[49, 42];
            int i = 0, j = 0;

            int alignmentParam = 0;

            foreach (var o in ic.outputs)
            {
                if (o == 10)
                {
                    i++;
                    j = 0;
                }
                else
                {
                    scaffolding[i, j] = (int)o;
                    j++;
                }
                Console.Write((char)o);
            }
            for (i = 1; i < 48; i++)
            {
                for (j = 1; j < 41; j++)
                {
                    if (scaffolding[i, j] == 35)
                    {
                        //check if its intersection
                        if (scaffolding[i - 1, j] == 35 && scaffolding[i + 1, j] == 35 && scaffolding[i, j - 1] == 35 && scaffolding[i, j + 1] == 35)
                        {
                            alignmentParam += i * j;
                            //Console.WriteLine("Intersection at " + i + ", " + j + " alignment params is (" + n + "*" + m + ")=" + alignmentParam);
                        }
                    }
                }
            }
            return(alignmentParam.ToString());
        }
Example #6
0
        public static string secondPuzzle(string input)
        {
            long[] intcode = input.Split(',').Select(long.Parse).ToArray();
            intcode[0] = 2;
            IntComputer ic = new IntComputer(intcode);

            char[] moves = { 'A', ',', 'C', ',', 'C', ',', 'B', ',', 'A', ',', 'C', ',', 'B', ',', 'A', ',', 'C', ',', 'B', '\n', //main
                             'L', ',', '6', ',', 'R', ',', '1', '2', ',', 'L', ',', '4', ',', 'L', ',', '6', '\n',                //a
                             'L', ',', '6', ',', 'L', ',', '1', '0', ',', 'L', ',', '1', '0', ',', 'R', ',', '6', '\n',           //b
                             'R', ',', '6', ',', 'L', ',', '6', ',', 'R', ',', '1', '2', '\n',                                    //c
                             'n', '\n' };                                                                                         //live video?
            foreach (char c in moves)
            {
                ic.inputs.Add((long)c);
            }

            ic.compute();

            long spaceDust = ic.outputs.Last();

            return(spaceDust.ToString());
        }
Example #7
0
        public static string secondPuzzle(string input)
        {
            long[]      intcode = input.Split(',').Select(long.Parse).ToArray();
            IntComputer ic      = new IntComputer(intcode);
            int         i       = 499;
            int         j       = 499;
            int         compass = 2;
            List <Tuple <int, int> > painted = new List <Tuple <int, int> >();

            int[,] hull = new int[1000, 1000];
            hull[i, j]  = 1;
            ic.inputs.Add(hull[i, j]);
            ic.compute();
            while (ic.isWaiting)
            {
                //Paint
                //elem1: 0 = blck, 1 = wht, elem2: 0 = left, 1 = right
                if (hull[i, j] != (int)ic.outputs.ElementAt(0))
                {
                    hull[i, j] = (int)ic.outputs.ElementAt(0);
                    painted.Add(new Tuple <int, int>(i, j));
                }
                ic.outputs.RemoveAt(0);
                //Rotate
                compass = rotateCompass(compass, (int)ic.outputs.ElementAt(0));
                ic.outputs.RemoveAt(0);
                //Move
                if (compass == 1)
                {
                    j--;
                }
                else if (compass == 2)
                {
                    i--;
                }
                else if (compass == 3)
                {
                    j++;
                }
                else if (compass == 4)
                {
                    i++;
                }
                ic.inputs.Add(hull[i, j]);
                ic.compute();
            }
            List <string> lines = new List <string>();
            //Print hull
            int minFirstOne = 1000;
            int maxLastOne  = 0;

            for (i = 0; i < hull.GetLength(0); i++)
            {
                string line     = "";
                int    firstOne = 1000;
                int    lastOne  = 0;
                for (j = 0; j < hull.GetLength(1); j++)
                {
                    if (hull[i, j] == 1 && j < firstOne)
                    {
                        firstOne = j;
                    }
                    if (hull[i, j] == 1 && j > lastOne)
                    {
                        lastOne = j;
                    }
                    line += hull[i, j] == 0 ? " " : "#";
                }
                if (firstOne != 1000)
                {
                    lines.Add(line);
                }
                if (firstOne < minFirstOne)
                {
                    minFirstOne = firstOne;
                }
                if (lastOne > maxLastOne)
                {
                    maxLastOne = lastOne;
                }
            }
            Console.WriteLine("min first one: " + minFirstOne + ", max last one: " + maxLastOne + " lines " + lines.Count);
            List <string> trimmedLines = new List <string>();

            foreach (var line in lines)
            {
                trimmedLines.Add(line.Substring(0, maxLastOne + 4).Substring(minFirstOne - 3));
            }
            return(string.Join('\n', trimmedLines));
        }
Example #8
0
        public static string secondPuzzle(string input)
        {
            long[]      intcode = input.Split(',').Select(long.Parse).ToArray();
            IntComputer ic      = new IntComputer(intcode);

            long score  = 0;
            long blocks = 363;
            Tuple <long, long> paddle   = new Tuple <long, long>(0, 0);
            Tuple <long, long> oldbBall = new Tuple <long, long>(0, 0);
            Tuple <long, long> ball     = new Tuple <long, long>(0, 0);

            ic.compute();
            while (ic.isWaiting)
            {
                blocks = 0;
                Console.WriteLine(ic.outputs.Count);
                while (ic.outputs.Count >= 3)
                {
                    if (ic.outputs[2] == 4)
                    {
                        oldbBall = ball;
                        ball     = new Tuple <long, long>(ic.outputs[0], ic.outputs[1]);
                    }
                    else if (ic.outputs[2] == 3)
                    {
                        paddle = new Tuple <long, long>(ic.outputs[0], ic.outputs[1]);
                    }
                    else if (ic.outputs[2] == 2)
                    {
                        blocks++;
                    }
                    else if (ic.outputs[0] == -1 && ic.outputs[1] == 0)
                    {
                        score = ic.outputs[2];
                    }
                    ic.outputs.RemoveRange(0, 3);
                }
                Console.WriteLine(blocks + " blocks left!");
                //Move paddle according to ball movement
                if (oldbBall.Item1 < ball.Item1)
                {
                    //ball is moving right
                    if (paddle.Item1 > ball.Item1 + 1)
                    {
                        ic.inputs.Add(-1);
                    }
                    else if (paddle.Item1 < ball.Item1 + 1)
                    {
                        ic.inputs.Add(1);
                    }
                    else
                    {
                        ic.inputs.Add(0);
                    }
                }
                else if (oldbBall.Item1 > ball.Item1)
                {
                    //ball is moving right
                    if (paddle.Item1 > ball.Item1 - 1)
                    {
                        ic.inputs.Add(-1);
                    }
                    else if (paddle.Item1 < ball.Item1 - 1)
                    {
                        ic.inputs.Add(1);
                    }
                    else
                    {
                        ic.inputs.Add(0);
                    }
                }
                else
                {
                    //ball is moving straight up or down
                    if (paddle.Item1 > ball.Item1)
                    {
                        ic.inputs.Add(-1);
                    }
                    else if (paddle.Item1 < ball.Item1)
                    {
                        ic.inputs.Add(1);
                    }
                    else
                    {
                        ic.inputs.Add(0);
                    }
                }
            }

            return(score.ToString());
        }