Beispiel #1
0
        public Image Generate()
        {
            var(xRange, yRange)   = Area2d.Create(_map.Keys);
            var(xOffset, yOffset) = CalculateOffsets(xRange.Min, yRange.Min);

            var imageWidth  = xRange.Max - xOffset + 1;
            var imageHeight = yRange.Max - yOffset + 1;
            var image       = new Image <Rgba32>(imageWidth, imageHeight);

            for (var y = yRange.Min; y <= yRange.Max; y++)
            {
                Span <Rgba32> pixelRowSpan = image.GetPixelRowSpan(y - yOffset);

                for (var x = xRange.Min; x <= xRange.Max; x++)
                {
                    var point   = new Point2d(x, y);
                    var offsetX = x - xOffset;

                    pixelRowSpan[offsetX] = GetColorForPoint(point);
                }
            }

            image.Mutate(x => x.Resize(image.Width * 10, image.Height * 10).Pixelate(10));

            return(image);
Beispiel #2
0
        public static ImmutableDictionary <Point2d, RegionDetails> Generate(Point2d start, Point2d target, ulong depth)
        {
            var map = ImmutableDictionary.CreateBuilder <Point2d, RegionDetails>();

            var area = Area2d.Create(start, target);

            foreach (var point in area.Items)
            {
                map[point] = RegionDetails.Calculate(start, target, point, depth, map);
            }

            return(map.ToImmutable());
        }
Beispiel #3
0
        public static ImmutableDictionary <Point2d, TileState> ParseInfectedNodes(this IInput input)
        {
            var arr      = input.Lines.As2DArray();
            var area     = Area2d.Create(arr);
            var infected = ImmutableDictionary.CreateBuilder <Point2d, TileState>();

            foreach (var(original, updated) in area.Recentre(Point2d.Origin))
            {
                infected.Add(updated, arr[original.X, original.Y] switch
                {
                    '#' => TileState.Infected,
                    '.' => TileState.Clean,
                    _ => throw new NotImplementedException()
                });
Beispiel #4
0
        public override void PartOne(IInput input, IOutput output)
        {
            var map = input
                      .As2DPoints()
                      .Where(c => c.Character is not ' ')
                      .ToImmutableDictionary(k => k.Point, v => v.Character);

            var area = Area2d.Create(map.Keys);

            var(point, character) = map
                                    .Where(k => k.Key.X == 0 || k.Key.Y == 0)
                                    .Where(v => v.Value is '-' or '|')
                                    .Single();

            var visited = new HashSet <Point2d>
            {
                point
            };
            var direction = StartingDirection(area, point, character);

            var letters = new List <char>();

            while (true)
            {
                var @char = map[point];

                if (@char is '+')
                {
                    (direction, _) = Direction.CardinalDirections
                                     .Select(dir => (Direction: dir, Point: point + dir))
                                     .Where(n => map.ContainsKey(n.Point) && visited.Contains(n.Point) is false)
                                     .Single();
                }
                else if (char.IsLetter(@char))
                {
                    letters.Add(@char);
                }

                if (map.ContainsKey(point + direction) is false)
                {
                    break;
                }

                point += direction;
                visited.Add(point);
            }

            output.WriteProperty("Final letters", string.Join("", letters));
        }
Beispiel #5
0
        public override void PartOne(IInput input, IOutput output)
        {
            const int Steps  = 100;
            var       points = input.Parse();
            var       area   = Area2d.Create(points.Keys);

            for (var i = 0; i < Steps; i++)
            {
                points = Step(points, area);
            }

            var lightsOn = points.Count(p => p.Value == LightState.On);

            output.WriteProperty("Number of lights on", lightsOn);
        }
Beispiel #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);
    }
Beispiel #7
0
    public static Map ParseMap(this IInputLines lines)
    {
        var arr  = lines.As2DArray();
        var area = Area2d.Create(arr);

        var cucumbers = ImmutableHashSet.CreateBuilder <SeaCucumber>();

        foreach (var point in area.Items)
        {
            var c = arr[point.Y, point.X];
            if (c is '>' or 'v')
            {
                cucumbers.Add(new SeaCucumber(point, c is '>' ? Direction.East : Direction.South));
            }
        }

        return(new Map(cucumbers.ToImmutable(), area));
    }
Beispiel #8
0
        public override void PartTwo(IInput input, IOutput output)
        {
            const int Steps = 100;

            var builder = input.Parse().ToBuilder();
            var area    = Area2d.Create(builder.Keys);

            LightCorners(area, builder);
            var points = builder.ToImmutable();

            for (var i = 0; i < Steps; i++)
            {
                points = Step(points, area);
            }

            var lightsOn = points.Count(p => p.Value == LightState.On);

            output.WriteProperty("Number of lights on", lightsOn);
        }
Beispiel #9
0
    public static ImmutableDictionary <Point2d, int> ParseFullMap(this IInput input)
    {
        var map     = input.ParseMap();
        var area2d  = Area2d.Create(map.Keys);
        var fullMap = ImmutableDictionary.CreateBuilder <Point2d, int>();

        for (var fy = 0; fy < 5; fy++)
        {
            var yOffset = (area2d.BottomLeft.Y + 1) * fy;

            for (var fx = 0; fx < 5; fx++)
            {
                var xOffset = (area2d.TopRight.X + 1) * fx;

                for (var y = 0; y < area2d.Height; y++)
                {
                    for (var x = 0; x < area2d.Width; x++)
                    {
                        var point = new Point2d(x + xOffset, y + yOffset);
                        var risk  = map[(x, y)];
Beispiel #10
0
        public static OfficeSpace Parse(this IInput input)
        {
            var favouriteNumber = int.Parse(input.Content.AsString());
            var builder         = ImmutableDictionary.CreateBuilder <Point2d, CellType>();

            var area2d = Area2d.Create(Point2d.Origin, new Point2d(51, 51));

            var items = area2d.Items.Select(point => KeyValuePair.Create(
                                                point, IsWall(point) ? CellType.Wall : CellType.Empty));

            builder.AddRange(items);

            return(new OfficeSpace(builder.ToImmutable()));

            bool IsWall(Point2d point)
            {
                var value = point.X * point.X + 3 * point.X + 2 * point.X * point.Y + point.Y + point.Y * point.Y;

                value += favouriteNumber;
                var bitsSet = BitOperations.PopCount((uint)value);

                return(bitsSet % 2 != 0);
            }
        }
Beispiel #11
0
 public Vault(string passcode)
 {
     _hasher   = MD5.Create();
     _area     = Area2d.Create(Point2d.Origin, new(3, 3));
     _passcode = passcode;
 }