コード例 #1
0
        public long Run(int input)
        {
            var intcode = new Intcode(_program);

            intcode.AddInputs(input);
            return(intcode.Execute().First());
        }
コード例 #2
0
        private IEnumerable <(int, int)> GetBounds(
            int maxX   = int.MaxValue,
            int maxY   = int.MaxValue,
            int startY = 0)
        {
            int prevWidth = 0;
            int startX    = 0;

            for (int y = startY; y < maxY; y++)
            {
                bool isFoundFirst     = false;
                int  first            = -1;
                bool isReturnedAnswer = false;
                for (int x = startX; x < maxX; x++)
                {
                    var ic = new Intcode(_program);
                    ic.AddInputs(x, y);
                    var output = (int)ic.Execute().First();
                    if (!isFoundFirst && output == 1)
                    {
                        isFoundFirst = true;
                        first        = x;
                        startX       = x;
                        if (prevWidth > 0)
                        {
                            x += prevWidth - 1;
                        }
                    }
                    else if (!isFoundFirst && x - startX > 10)
                    {
                        prevWidth = 0;
                        yield return(-1, -1);

                        isReturnedAnswer = true;
                        break;
                    }
                    else if (isFoundFirst && output == 0)
                    {
                        prevWidth = x - first - 1;
                        yield return(first, x - 1);

                        isReturnedAnswer = true;
                        break;
                    }
                }
                if (!isReturnedAnswer)
                {
                    if (first == -1)
                    {
                        prevWidth = 0;
                        yield return(-1, -1);
                    }
                    else
                    {
                        prevWidth = maxX - first - 1;
                        yield return(first, maxX - 1);
                    }
                }
            }
        }
コード例 #3
0
ファイル: Day05.cs プロジェクト: jjermanis/aoc2019
        public long DoCalc(int input)
        {
            var intcode = new Intcode(_program);

            intcode.AddInputs(input);
            return(intcode.Execute().Last());
        }
コード例 #4
0
        public long Execute(int noun, int verb)
        {
            // This was based on an older version of the Intcode machine. There were no
            // I/O opcodes - it was based on direct reads/writes into memory
            long[] memory = new long[_programData.Length];
            Array.Copy(_programData, memory, _programData.Length);
            var intcode = new Intcode(memory);

            intcode.Poke(1, noun);
            intcode.Poke(2, verb);

            // Run to termination
            intcode.Execute().LastOrDefault();
            return(intcode.Peek(0));
        }
コード例 #5
0
ファイル: Day13.cs プロジェクト: jjermanis/aoc2019
        public int BlockCountExit()
        {
            var intcode = new Intcode(_program);
            var screen  = new Dictionary <(long, long), long>();

            using (var enumer = intcode.Execute().GetEnumerator())
            {
                while (enumer.MoveNext())
                {
                    var x = enumer.Current;
                    enumer.MoveNext();
                    var y = enumer.Current;
                    enumer.MoveNext();
                    var tile = enumer.Current;

                    screen[(x, y)] = tile;
コード例 #6
0
ファイル: Day15.cs プロジェクト: jjermanis/aoc2019
        public (int, int) TraverseMaze(bool drawMaze = false)
        {
            var intcode = new Intcode(_program);

            var resultDistanceToOx      = -1;
            var resultMaxDistanceFromOx = -1;

            var x            = 0;
            var y            = 0;
            var dirIndex     = 0;
            var map          = new Dictionary <(int, int), char>();
            var deadEnds     = new HashSet <(int, int)>();
            var distances    = new Dictionary <(int, int), int>();
            var currDistance = 0;

            map[(0, 0)]       = '.';
コード例 #7
0
        public long MaxSignalWithFeedback()
        {
            var max = 0L;

            foreach (var perm in Util.Permute(5, 6, 7, 8, 9))
            {
                int                  intcodeCount = perm.Length;
                Intcode[]            intcodes     = new Intcode[intcodeCount];
                IEnumerator <long>[] icEnums      = new IEnumerator <long> [intcodeCount];
                for (var x = 0; x < intcodeCount; x++)
                {
                    intcodes[x] = new Intcode(_program);
                    intcodes[x].AddInputs(perm[x]);
                    icEnums[x] = intcodes[x].Execute().GetEnumerator();
                }
                intcodes[0].AddInputs(0);

                bool isTerminated = false;
                while (!isTerminated)
                {
                    for (var x = 0; x < intcodeCount; x++)
                    {
                        var isNext = icEnums[x].MoveNext();
                        if (!isNext)
                        {
                            isTerminated = true;
                            break;
                        }
                        NextIntcode(x).AddInputs(icEnums[x].Current);
                    }
                }

                max = Math.Max(max, intcodes[4].LastOutput);

                Intcode NextIntcode(int x) => intcodes[(x + 1) % 5];
            }
            return(max);
        }
コード例 #8
0
ファイル: Day11.cs プロジェクト: jjermanis/aoc2019
        private IDictionary <(int, int), long> GetPainting(int startColor)
        {
            var intcode  = new Intcode(_program);
            var painting = new Dictionary <(int, int), long>();

            painting[(0, 0)] = startColor;