Ejemplo n.º 1
0
        public object Work2()
        {
            string A = "L,12,L,10,R,8,L,12";
            string B = "R,8,R,10,R,12";
            string C = "L,10,R,12,R,8";

            string main = path.Replace(A, "A");

            main = main.Replace(B, "B");
            main = main.Replace(C, "C");

            mainAscii = Encoding.ASCII.GetBytes(main);
            AAscii    = Encoding.ASCII.GetBytes(A);
            BAscii    = Encoding.ASCII.GetBytes(B);
            CAscii    = Encoding.ASCII.GetBytes(C);
            source    = mainAscii;

            var array = input.Split(new[] { ',' }).Select(x => long.Parse(x)).ToArray();

            array[0] = 2;
            long dust = 0;
            var  comp = new IntCodeComp(array);

            comp.GetInput    = CompInput;
            comp.WriteOutput = (output) =>
            {
                dust = output;
                //Console.Write(Encoding.ASCII.GetString(new byte[] { (byte)output }));
            };

            comp.RunProgram();

            return(dust);
        }
Ejemplo n.º 2
0
        public object Work2()
        {
            var array = input.Split(new[] { ',' }).Select(x => long.Parse(x)).ToArray();

            var computer = new IntCodeComp(array)
            {
                GetInput    = () => 2,
                WriteOutput = (output) => Console.WriteLine($"Output: {output}")
            };

            computer.RunProgram();
            return(null);
        }
Ejemplo n.º 3
0
        public object Work1()
        {
            var  comp = new IntCodeComp(input);
            long x = 0, y = 0;
            bool isY    = false;
            int  pulled = 0;

            comp.GetInput = () =>
            {
                if (isY)
                {
                    isY = false;
                    return(y);
                }
                else
                {
                    isY = true;
                    return(x);
                }
            };

            comp.WriteOutput = (output) =>
            {
                if (output > 0)
                {
                    pulled++;
                }
            };

            for (x = 0; x < 50; x++)
            {
                for (y = 0; y < 50; y++)
                {
                    comp.RunProgram();
                }
            }

            return(pulled);
        }
Ejemplo n.º 4
0
        public object Work1()
        {
            var array = input.Split(new[] { ',' }).Select(x => long.Parse(x)).ToArray();

            var computer = new IntCodeComp(array);

            computer.GetInput = () =>
            {
                int value1;
                Console.Write("Insert number: ");
                if (!int.TryParse(Console.ReadLine(), out value1))
                {
                    throw new ArgumentException("Incorrect input supplied! Only numeric inputs are compatible.");
                }

                return(value1);
            };

            computer.WriteOutput = (output) => Console.WriteLine($"Output: {output}");

            computer.RunProgram();

            return(null);
        }
Ejemplo n.º 5
0
        public object Work1()
        {
            var comp = new IntCodeComp(input);

            var scaffoldBuilder = new StringBuilder();

            comp.WriteOutput = (output) =>
            {
                switch (output)
                {
                case 35:
                    scaffoldBuilder.Append('#');
                    break;

                case 46:
                    scaffoldBuilder.Append('.');
                    break;

                case 10:
                    scaffoldBuilder.AppendLine();
                    break;

                case 94:
                    scaffoldBuilder.Append('^');
                    break;

                default:
                    Console.WriteLine($"Invalid output from ASCII computer: '{output}'.");
                    break;
                }
            };

            comp.RunProgram();

            // transform to array
            var lines    = scaffoldBuilder.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            var scaffold = new char[lines[0].Length, lines.Length];

            for (int x = 0; x < lines[0].Length; x++)
            {
                for (int y = 0; y < lines.Length; y++)
                {
                    scaffold[x, y] = lines[y][x];
                }
            }

            Display(scaffold, lines[0].Length, lines.Length);

            // calculate intersections
            var intersections = new List <int>();

            for (int x = 0; x < lines[0].Length; x++)
            {
                for (int y = 0; y < lines.Length; y++)
                {
                    if (scaffold[x, y] == '#')
                    {
                        var surroundings = new StringBuilder();
                        if (x + 1 < lines[0].Length)
                        {
                            surroundings.Append(scaffold[x + 1, y]);
                        }
                        if (x - 1 >= 0)
                        {
                            surroundings.Append(scaffold[x - 1, y]);
                        }

                        if (y + 1 < lines.Length)
                        {
                            surroundings.Append(scaffold[x, y + 1]);
                        }
                        if (y - 1 >= 0)
                        {
                            surroundings.Append(scaffold[x, y - 1]);
                        }

                        surroundings = surroundings.Replace(".", "");

                        if (surroundings.Length >= 3)
                        {
                            intersections.Add(y * x);
                        }
                    }
                }
            }

            return(intersections.Sum());
        }
Ejemplo n.º 6
0
        public object Work1()
        {
            var array = input.Split(new[] { ',' }).Select(x => long.Parse(x)).ToArray();

            var amp1 = new IntCodeComp(array);
            var amp2 = new IntCodeComp(array);
            var amp3 = new IntCodeComp(array);
            var amp4 = new IntCodeComp(array);
            var amp5 = new IntCodeComp(array);

            var phases = new List <int>()
            {
                0, 1, 2, 3, 4
            }.Permute();
            int res1 = 0, res2 = 0, res3 = 0, res4 = 0, res5 = 0;

            var maxThrusts = new List <int>();

            foreach (var phaseEnum in phases)
            {
                var phase = phaseEnum.ToList();

                amp1.GetInput = () =>
                {
                    amp1.GetInput = () =>
                    {
                        return(0);
                    };

                    return(phase[0]);
                };
                amp1.WriteOutput = (output) => res1 = (int)output;

                amp2.GetInput = () =>
                {
                    amp2.GetInput = () =>
                    {
                        return(res1);
                    };

                    return(phase[1]);
                };
                amp2.WriteOutput = (output) => res2 = (int)output;

                amp3.GetInput = () =>
                {
                    amp3.GetInput = () =>
                    {
                        return(res2);
                    };

                    return(phase[2]);
                };
                amp3.WriteOutput = (output) => res3 = (int)output;

                amp4.GetInput = () =>
                {
                    amp4.GetInput = () =>
                    {
                        return(res3);
                    };

                    return(phase[3]);
                };
                amp4.WriteOutput = (output) => res4 = (int)output;

                amp5.GetInput = () =>
                {
                    amp5.GetInput = () =>
                    {
                        return(res4);
                    };

                    return(phase[4]);
                };
                amp5.WriteOutput = (output) => res5 = (int)output;

                amp1.RunProgram();
                amp2.RunProgram();
                amp3.RunProgram();
                amp4.RunProgram();
                amp5.RunProgram();

                maxThrusts.Add(res5);
            }

            return(maxThrusts.Max());
        }
Ejemplo n.º 7
0
        public object Work2()
        {
            var  comp = new IntCodeComp(input);
            long x = 0, y = 0;
            bool isY  = false;
            int  size = 200;

            // offset identified by calculation and subsequent experimentation
            int offsetY = 1000;
            int offsetX = 1300;

            int[,] field = new int[size, size];

            comp.GetInput = () =>
            {
                if (isY)
                {
                    isY = false;
                    return(y);
                }
                else
                {
                    isY = true;
                    return(x);
                }
            };

            comp.WriteOutput = (output) =>
            {
                if (output > 0)
                {
                    field[x - offsetX, y - offsetY] = 1;
                }
                else
                {
                    field[x - offsetX, y - offsetY] = 0;
                }
            };

            // generate field
            for (x = offsetX; x < size + offsetX; x++)
            {
                for (y = offsetY; y < size + offsetY; y++)
                {
                    comp.RunProgram();
                }
            }

            for (x = 0; x < size - 99; x++)
            {
                for (y = 0; y < size - 99; y++)
                {
                    if (field[x, y] == 1 && field[x + 99, y] == 1 && field[x, y + 99] == 1)
                    {
                        DisplayFieldWithObject(field, x, y, 100);
                        Console.WriteLine($"100x100 field found at: X: {x + offsetX} Y: {y + offsetY}; answer is {(x + offsetX) * 10000 + (y + offsetY)}");
                        return(0);
                    }
                }
            }
            return(0);
        }