Пример #1
0
        protected override object SolvePartOne()
        {
            var perm = _phases.Permutate(_phases.Length);

            int[] curPhase = new int[5];
            //var output = new List<string>();
            string  s   = Input.Trim('\n');
            IntCode cpu = IntCode.Create(s);

            int  runningAmp = 0;
            long input      = 0;
            long output     = 0;

            long maxOutput = long.MinValue;

            cpu.GetInput += (i) =>
            {
                return(i switch
                {
                    0 => curPhase[runningAmp],
                    _ => input,
                });
                //System.Console.WriteLine($"Getting input {curPhase[runningAmp]}.");
                //return curPhase[runningAmp];
            };
Пример #2
0
        protected override object SolvePartOne()
        {
            IntCode cpu = IntCode.Create(Input.Trim('\n'));

            cpu.ByteCode[1] = 12;
            cpu.ByteCode[2] = 2;
            cpu.Run();

            return(cpu.ByteCode[0].ToString());
        }
Пример #3
0
        protected override object SolvePartTwo()
        {
            var     output = new List <long>();
            string  s      = Input.Trim('\n');
            IntCode cpu    = IntCode.Create(s);

            cpu.GetInput += (i) => 5;
            cpu.Output   += (s, e) => output.Add(e);
            cpu.Run();
            return(output.Last().ToString());
        }
Пример #4
0
        protected override object SolvePartTwo()
        {
            var panels = new Dictionary <Point, long>
            {
                { new Point(), 1 },
            };

            (Point location, Point direction) = (new Point(), Point.North);
            IntCode cpu = IntCode.Create(Input.SplitByNewline().First());

            cpu.GetInput = (i) =>
            {
                return(panels.TryGetValue(location, out var result)
                ? result
                : 0);
            };
            int output = 0;

            cpu.Output += (s, e) =>
            {
                if (output == 0)
                {
                    panels[location] = e;
                }
                else
                {
                    direction = e switch
                    {
                        0 => new Point(direction.Y, -direction.X),
                        1 => new Point(-direction.Y, direction.X),
                        _ => throw new System.Exception(),
                    };
                    location += direction;
                }
                output = (output + 1) & 1;
            };
            cpu.Run();

            int minX = panels.Keys.Min(p => p.X);
            int minY = panels.Keys.Min(p => p.Y);
            int rows = panels.Keys.Max(p => p.Y) - minY + 1;
            int cols = panels.Keys.Max(p => p.X) - minX + 1;

            WriteConsole(rows, cols, 15, 15, (row, col) =>
            {
                var p  = new Point(col - minX, row - minY);
                char c = '.';
                if (panels.TryGetValue(p, out long value))
                {
                    c = panels[p] == 0 ? '.' : '#';
                }
                return(ConsoleColor.White, c);
            });
Пример #5
0
        protected override object SolvePartTwo()
        {
            string output = null;;
            var    cpu    = IntCode.Create(Input);

            cpu.GetInput = (i) => 2;
            cpu.Output  += (s, l) =>
            {
                //System.Console.WriteLine(l);
                output = $"{l}";
            };
            cpu.Run();
            return(output);
        }
Пример #6
0
        protected override object SolvePartOne()
        {
            var     output = new List <long>();
            string  s      = Input.Trim('\n');
            IntCode cpu    = IntCode.Create(s);

            cpu.GetInput += (i) => 1;
            cpu.Output   += (s, e) =>
            {
                output.Add(e);
                System.Console.WriteLine(e);
            };
            cpu.Run();
            return(output.Last().ToString());
        }
Пример #7
0
        protected override object SolvePartOne()
        {
            IntCode cpu    = IntCode.Create(Input.SplitByNewline().First());
            int     i      = 0;
            int     blocks = 0;

            cpu.Output += (s, e) =>
            {
                if (i == 2 && e == 2)
                {
                    blocks++;
                }

                i = ++i % 3;
            };
            cpu.Run();
            return(blocks.ToString());
        }
Пример #8
0
        protected override object SolvePartTwo()
        {
            IntCode cpu = IntCode.Create(Input.Trim('\n'));

            for (int i = 0; i < 100; i++)
            {
                for (int j = 0; j < 100; j++)
                {
                    cpu.Reset();
                    cpu.ByteCode[1] = i;
                    cpu.ByteCode[2] = j;
                    cpu.Run();

                    if (cpu.ByteCode[0] == 19690720)
                    {
                        return($"100 * {i} + {j} = {100 * i + j}");
                    }
                }
            }

            return(null);
        }
Пример #9
0
        protected override object SolvePartOne()
        {
            var panels = new Dictionary <Point, long>();

            (Point location, Point direction) = (new Point(), Point.North);
            IntCode cpu = IntCode.Create(Input.SplitByNewline().First());

            cpu.GetInput = (i) =>
            {
                return(panels.TryGetValue(location, out var result)
                ? result
                : 0);
            };
            int output = 0;

            cpu.Output += (s, e) =>
            {
                if (output == 0)
                {
                    panels[location] = e;
                }
                else
                {
                    direction = e switch
                    {
                        0 => new Point(direction.Y, -direction.X),
                        1 => new Point(-direction.Y, direction.X),
                        _ => throw new System.Exception(),
                    };
                    location += direction;
                }
                output = (output + 1) & 1;
            };
            cpu.Run();
            return(panels.Count.ToString());
        }
Пример #10
0
        protected override object SolvePartTwo()
        {
            IntCode cpu = IntCode.Create(Input.SplitByNewline().First());

            cpu.ByteCode[0] = 2;
            var output = new List <long>();

            long[] counts = new long[5];
            int    i      = 0;
            Point  ball   = new Point();
            Point  paddle = new Point();
            long   x      = 0;
            long   y      = 0;

            cpu.GetInput = (i) =>
            {
                return(ball.X.CompareTo(paddle.X));
            };
            cpu.Output += (s, e) =>
            {
                if (i == 0)
                {
                    x = e;
                }
                else if (i == 1)
                {
                    y = e;
                }
                else if (i == 2)
                {
                    if (x == -1)
                    {
                        WriteScore(e);
                    }
                    else
                    {
                        char c = e switch
                        {
                            0 => ' ',
                            1 => '#',
                            2 => '.',
                            3 => '_',
                            4 => 'o',
                        };
                        WriteConsole((int)y, (int)x, 15, 15, c);
                    }
                    if (e == 3)
                    {
                        paddle = new Point((int)x, (int)y);
                    }
                    if (e == 4)
                    {
                        ball = new Point((int)x, (int)y);
                    }
                    //Thread.Sleep(1);
                }
                i = ++i % 3;
                output.Add(e);
            };
            cpu.Run();

            return(null);
        }