Example #1
0
        public void Part2()
        {
            var intCodeComputer = new IntCodeComputerDay11(Day11Input, TestContext);
            var input           = 1L;
            var track           = CalculateTrack(input, intCodeComputer);
            var trackByRows     = track.GroupBy(kvp => kvp.Key.y);
            var stringBuilder   = new StringBuilder();

            foreach (var row in trackByRows)
            {
                var ubound   = row.Max(kvp => kvp.Key.x);
                var valueByX = row.ToDictionary(k => k.Key.x, v => v.Value);

                for (var i = 0; i < ubound; i++)
                {
                    if (valueByX.ContainsKey(i))
                    {
                        stringBuilder.Append(valueByX[i] == 0 ? " " : "X");
                    }
                    else
                    {
                        stringBuilder.Append(" ");
                    }
                }

                stringBuilder.AppendLine();
            }
            TestContext.WriteLine(stringBuilder.ToString());
        }
Example #2
0
        public void Part1()
        {
            var intCodeComputer = new IntCodeComputerDay11(Day11Input, TestContext);
            var input           = 0L;
            var track           = CalculateTrack(input, intCodeComputer);

            TestContext.WriteLine(track.Count.ToString());
        }
Example #3
0
        private static Dictionary <(int x, int y), long> CalculateTrack(long input, IntCodeComputerDay11 intCodeComputer)
        {
            var track = new Dictionary <(int x, int y), long>();

            (int x, int y)location = (0, 0);
            var direction = Direction.Up;

            while (input != -1)
            {
                var output = intCodeComputer.IntCode(input);
                track[location] = output;
                output          = intCodeComputer.IntCode();
                if (output == 0)
                {
                    direction = (Direction)((byte)((byte)direction - 1) % 4);
                }
                else
                {
                    direction = (Direction)((byte)((byte)direction + 1) % 4);
                }
                switch (direction)
                {
                case Direction.Up:
                    location.y++;
                    break;

                case Direction.Right:
                    location.x++;
                    break;

                case Direction.Down:
                    location.y--;
                    break;

                case Direction.Left:
                    location.x--;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                if (track.TryGetValue(location, out input) == false)
                {
                    input = 0;
                }
            }

            return(track);
        }