r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-

--- Day 3: No Matter How You Slice It ---


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.


Advent of Code: The Party Game!

Click here for rules

ATTENTION: minor change request from the mods!

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


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!

41 Upvotes

426 comments sorted by

View all comments

5

u/miguelos Dec 03 '18 edited Dec 03 '18

C#

Part 1:

var answer = (
    from claim in Regex.Matches(File.ReadAllText(@"C:\input.txt"), @"(?<claim>#(?<id>\d+) @ (?<l>\d+),(?<t>\d+): (?<w>\d+)x(?<h>\d+))\n")
    let l = int.Parse(claim.Groups["l"].Value)
    let t = int.Parse(claim.Groups["t"].Value)
    let w = int.Parse(claim.Groups["w"].Value)
    let h = int.Parse(claim.Groups["h"].Value)
    from x in Enumerable.Range(l, w)
    from y in Enumerable.Range(t, h)
    group (x, y) by (x, y) into g
    where g.Count() > 1
    select g)
    .Count();

Part 2:

var input = System.IO.File.ReadAllText(@"C:\input.txt");
var claims = input.Split('\n').Where(c => !string.IsNullOrWhiteSpace(c));

var table = new int[1000, 1000];
var noOverlaps = new List<int>();

foreach (var claim in claims)
{
    var parts = claim.Split(new[] { '#', '@', ' ', ',', ':', 'x' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
    var id = parts[0];
    var x = parts[1];
    var y = parts[2];
    var w = parts[3];
    var h = parts[4];

    noOverlaps.Add(id);

    for (int i = 0; i < w; i++)
    {
        for (int j = 0; j < h; j++)
        {
            var previousId = table[x + i, y + j];
            if (previousId == 0)
            {
                table[x + i, y + j] = id;
            }
            else
            {
                noOverlaps.Remove(id);
                noOverlaps.Remove(previousId);
            }
        }
    }
}

var answer = noOverlaps.First();

Console.WriteLine(answer);