Exemplo n.º 1
0
        public override void PartTwo(IInput input, IOutput output)
        {
            var location = FindFirstVisitedTwice(input.Parse());

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

            output.WriteProperty("Distance from start in blocks", distance);
Exemplo n.º 2
0
        public override void PartOne(IInput input, IOutput output)
        {
            var target = input.Content.AsInt();

            var end = SpiralPointGenerator().ElementAt(target - 1);

            output.WriteProperty($"Distance from (0, 0) to {end}", PointHelpers.ManhattanDistance(Point2d.Origin, end));
        }
Exemplo n.º 3
0
        public void PartOne(string[] lines, TextWriter @out)
        {
            var mappedLines = lines.Select(ParseLine).ToArray();
            var closest     = GetLineIntersections(mappedLines)
                              .OrderBy(x => PointHelpers.ManhattanDistance(CentralPort, x))
                              .First();

            var distance = PointHelpers.ManhattanDistance(closest, CentralPort);

            @out.WriteLine($"Closest point to CentralPort: {closest}: Distance {distance}");
        }
Exemplo n.º 4
0
        public override void PartOne(IInput input, IOutput output)
        {
            var particles = input.Lines.ParseParticles();

            for (var i = 0; i < 1000; i++)
            {
                particles = Tick(particles);
            }

            var closest = particles
                          .MinBy(particle => PointHelpers.ManhattanDistance(particle.Position, Point3d.Origin)) !;

            output.WriteProperty("Closest", closest.Id);
Exemplo n.º 5
0
    public override void PartTwo(IInput input, IOutput output)
    {
        var scanners = input.Parse();

        var(_, scannerPositions) = ReorientScannerRegions(scanners);

        var maximumDistance = scannerPositions
                              .Subsets(2)
                              .Select(subset => PointHelpers.ManhattanDistance(subset[0], subset[1]))
                              .Max();

        output.WriteProperty("Max Distance between Scanners", maximumDistance);
    }
Exemplo n.º 6
0
    public override void PartTwo(IInput input, IOutput output)
    {
        var map    = input.ParseFullMap();
        var area2d = Area2d.Create(map.Keys);

        var graph = new Graph(map);
        var path  = graph.AStarSearch(
            area2d.TopLeft,
            area2d.BottomRight,
            static (a, b) => PointHelpers.ManhattanDistance(a, b));

        var totalCost = path.Skip(1)
                        .Sum(point => map[point]);

        output.WriteProperty("Total Cost", totalCost);
    }
Exemplo n.º 7
0
        public override void PartOne(IInput input, IOutput output)
        {
            var graph = input.Parse();

            var path = graph.AStarSearch(
                new Point2d(1, 1),
                new Point2d(31, 39),
                static (a, b) => PointHelpers.ManhattanDistance(a, b));

            output.WriteBlock(() =>
            {
                var visitedOfficeSpace = graph.SetVisited(path);
                return(new Panel(visitedOfficeSpace.Print()));
            });

            output.WriteProperty("Path length", path.Length - 1);
        }
Exemplo n.º 8
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);
        }