예제 #1
0
        public override void PartOne(IInput input, IOutput output)
        {
            var current   = Point2d.Origin;
            var direction = Direction.North;

            foreach (var directions in input.Parse())
            {
                direction = directions.Direction switch
                {
                    'L' => direction.Left(),
                    'R' => direction.Right(),
                    _ => throw new InvalidOperationException("Invalid direction")
                };

                current = Point2d.AddInDirection(current, direction, directions.Distance);
            }

            var distance = PointHelpers.ManhattanDistance(Point2d.Origin, current);

            output.WriteProperty("Distance from start in blocks", distance);
        }
예제 #2
0
        private static int CountActiveNeighbours(Point2d point, ImmutableHashSet <Point2d> bugs)
        {
            var count = 0;

            var adjacent = new[]
            {
                Point2d.AddInDirection(point, Direction.North, 1),
                Point2d.AddInDirection(point, Direction.East, 1),
                Point2d.AddInDirection(point, Direction.South, 1),
                Point2d.AddInDirection(point, Direction.West, 1),
            };

            foreach (var neighbour in adjacent)
            {
                if (bugs.Contains(neighbour))
                {
                    count++;
                }
            }

            return(count);
        }