r/adventofcode • • Dec 14 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 14 Solutions -๐ŸŽ„-

--- Day 14: Disk Defragmentation ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


[Update @ 00:09] 3 gold, silver cap.

  • How many of you actually entered the Konami code for Part 2? >_>

[Update @ 00:25] Leaderboard cap!

  • I asked /u/topaz2078 how many de-resolutions we had for Part 2 and there were 83 distinct users with failed attempts at the time of the leaderboard cap. tsk tsk

[Update @ 00:29] BONUS


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

13 Upvotes

130 comments sorted by

View all comments

Show parent comment

1

u/maxxori Dec 14 '17

One thing I would suggest (or request!) is that you include your extension methods along with the answer - Point.GetNeighbors threw me off for a while when I was trying to sanity-check my solution :)

2

u/dylanfromwinnipeg Dec 14 '17

Good feedback, I'll try and do that in future days. FYI, here's the relevant ones from day 14:

public static IEnumerable<Point> GetNeighbors(this Point point, bool includeDiagonals)
{
    var adjacentPoints = new List<Point>(8);

    adjacentPoints.Add(new Point(point.X - 1, point.Y));
    adjacentPoints.Add(new Point(point.X + 1, point.Y));
    adjacentPoints.Add(new Point(point.X, point.Y + 1));
    adjacentPoints.Add(new Point(point.X, point.Y - 1));

    if (includeDiagonals)
    {
        adjacentPoints.Add(new Point(point.X - 1, point.Y - 1));
        adjacentPoints.Add(new Point(point.X + 1, point.Y - 1));
        adjacentPoints.Add(new Point(point.X + 1, point.Y + 1));
        adjacentPoints.Add(new Point(point.X - 1, point.Y + 1));
    }

    return adjacentPoints;
}

public static void ForEach<T>(this T[,] a, Action<int, int> action)
{
    for (var x = a.GetLowerBound(0); x <= a.GetUpperBound(0); x++)
    {
        for (var y = a.GetLowerBound(1); y <= a.GetUpperBound(1); y++)
        {
            action(x, y);
        }
    }
}

public static IEnumerable<T> ToList<T>(this T[,] a)
{
    for (var x = a.GetLowerBound(0); x <= a.GetUpperBound(0); x++)
    {
        for (var y = a.GetLowerBound(1); y <= a.GetUpperBound(1); y++)
        {
            yield return a[x, y];
        }
    }
}

public static string HexToBinary(this string hex)
{
    StringBuilder sb = new StringBuilder();

    foreach (var c in hex.ToCharArray())
    {
        var intValue = int.Parse(c.ToString(), System.Globalization.NumberStyles.HexNumber);
        sb.Append(Convert.ToString(intValue, 2).PadLeft(4, '0'));
    }

    return sb.ToString();
}

1

u/maxxori Dec 15 '17

That's awesome :) I managed to write my own versions of those in order to get your code to run.

As it turns out that I had made a silly mistake when feeding the data into the hash function... everything else was working as I expected. D'oh!