Example #1
0
        public static void Execute()
        {
            RobotCoord startPoint = new RobotCoord {
                X = 0, Y = 0, Current = true
            };

            coordsVisited.Add(startPoint);
            long executionPointer = 0;
            long colour           = 0;
            long direction        = 0;
            int  currentDirection = Direction.Up;

            while (input[executionPointer] != OpcodeActions.End)
            {
                RobotCoord currentCoord = coordsVisited.First(x => x.Current);
                _globalInput = currentCoord.Colour == 0 ? 0 : 1;

                colour = ParseOpcode(input, ref executionPointer);
                if (colour == -1)
                {
                    break;
                }

                if ((int)colour != currentCoord.Colour)
                {
                    currentCoord.Colour = (int)colour;

                    currentCoord.ColourChanged = true;
                }
                executionPointer += 2;

                direction = ParseOpcode(input, ref executionPointer);
                CalculateDirection(direction, ref currentDirection, currentCoord);

                executionPointer += 2;
            }

            Console.WriteLine($"Number of painted squares: {coordsVisited.Count(x => x.ColourChanged)}");
        }
Example #2
0
        private static void WalkForwardOne(int currentDirection, RobotCoord currentCoord)
        {
            coordsVisited.ForEach(x => x.Current = false);

            RobotCoord newCoord = new RobotCoord();

            switch (currentDirection)
            {
            case 1:
                newCoord = new RobotCoord
                {
                    X       = currentCoord.X,
                    Y       = currentCoord.Y + 1,
                    Current = true
                };

                if (coordsVisited.Any(x => x.X == newCoord.X && x.Y == newCoord.Y))
                {
                    var coord = coordsVisited.First(x => x.X == newCoord.X && x.Y == newCoord.Y);
                    coord.Current = true;
                }
                else
                {
                    coordsVisited.Add(newCoord);
                }

                break;

            case 2:
                newCoord = new RobotCoord
                {
                    X       = currentCoord.X + 1,
                    Y       = currentCoord.Y,
                    Current = true
                };

                if (coordsVisited.Any(x => x.X == newCoord.X && x.Y == newCoord.Y))
                {
                    var coord = coordsVisited.First(x => x.X == newCoord.X && x.Y == newCoord.Y);
                    coord.Current = true;
                }
                else
                {
                    coordsVisited.Add(newCoord);
                }
                break;

            case 3:
                newCoord = new RobotCoord
                {
                    X       = currentCoord.X,
                    Y       = currentCoord.Y - 1,
                    Current = true
                };

                if (coordsVisited.Any(x => x.X == newCoord.X && x.Y == newCoord.Y))
                {
                    var coord = coordsVisited.First(x => x.X == newCoord.X && x.Y == newCoord.Y);
                    coord.Current = true;
                }
                else
                {
                    coordsVisited.Add(newCoord);
                }
                break;

            case 4:
                newCoord = new RobotCoord
                {
                    X       = currentCoord.X - 1,
                    Y       = currentCoord.Y,
                    Current = true
                };
                if (coordsVisited.Any(x => x.X == newCoord.X && x.Y == newCoord.Y))
                {
                    var coord = coordsVisited.First(x => x.X == newCoord.X && x.Y == newCoord.Y);
                    coord.Current = true;
                }
                else
                {
                    coordsVisited.Add(newCoord);
                }
                break;
            }
        }
Example #3
0
        private static void CalculateDirection(long direction, ref int currentDirection, RobotCoord currentCoord)
        {
            switch (direction)
            {
            case 0:
                if (currentDirection == Direction.Up)
                {
                    currentDirection = Direction.Left;
                }
                else
                {
                    currentDirection -= 1;
                }

                WalkForwardOne(currentDirection, currentCoord);

                break;

            case 1:
                if (currentDirection == Direction.Left)
                {
                    currentDirection = Direction.Up;
                }
                else
                {
                    currentDirection += 1;
                }

                WalkForwardOne(currentDirection, currentCoord);

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Example #4
0
        public static void Execute()
        {
            RobotCoord startPoint = new RobotCoord {
                X = 0, Y = 0, Current = true, Colour = 1
            };

            coordsVisited.Add(startPoint);
            long executionPointer = 0;
            long colour           = 0;
            long direction        = 0;
            int  currentDirection = Direction.Up;

            while (input[executionPointer] != OpcodeActions.End)
            {
                RobotCoord currentCoord = coordsVisited.First(x => x.Current);
                _globalInput = currentCoord.Colour == 0 ? 0 : 1;

                colour = ParseOpcode(input, ref executionPointer);
                if (colour == -1)
                {
                    break;
                }

                if ((int)colour != currentCoord.Colour)
                {
                    currentCoord.Colour = (int)colour;

                    currentCoord.ColourChanged = true;
                }
                executionPointer += 2;

                direction = ParseOpcode(input, ref executionPointer);
                CalculateDirection(direction, ref currentDirection, currentCoord);

                executionPointer += 2;
            }

            var minX = coordsVisited.Min(x => x.X);
            var maxX = coordsVisited.Max(x => x.X);

            var visitedYs = coordsVisited.Select(x => x.Y).Distinct().OrderBy(x => x).ToList();

            foreach (var y in visitedYs.OrderByDescending(x => x))
            {
                var coordsAtY = coordsVisited.Where(x => x.Y == y).OrderBy(x => x.X).ToList();

                for (int x = minX; x <= maxX; x++)
                {
                    if (coordsAtY.Any(c => c.X == x))
                    {
                        if (coordsAtY.First(c => c.X == x).Colour == 1)
                        {
                            Console.Write("#");
                        }
                        else
                        {
                            Console.Write(" ");
                        }
                    }
                    else
                    {
                        Console.Write(" ");
                    }
                }

                Console.WriteLine();
            }
        }