r/adventofcode • • Dec 10 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 10 Solutions -🎄-

--- Day 10: The Stars Align ---


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

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

Card prompt: Day 10

Transcript: With just one line of code, you, too, can ___!


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 at 00:16:49!

21 Upvotes

233 comments sorted by

View all comments

1

u/miguelos Dec 10 '18

C#

var data = File
    .OpenText(@"C:\Users\pc\Desktop\input.txt")
    .ReadToEnd()
    .Trim()
    .Split('\n')
    .Select(l => (
        x: int.Parse(l.Substring(10, 6)),
        y: int.Parse(l.Substring(18, 6)),
        dx: int.Parse(l.Substring(36, 2)),
        dy: int.Parse(l.Substring(40, 2))
    ))
    .ToArray();

var box = (x: 100, y: 30);
var candidates = from seconds in Range(0, int.MaxValue)
                 let points = from datum in data
                              let x = datum.x + (seconds * datum.dx)
                              let y = datum.y + (seconds * datum.dy)
                              select (x, y)
                 let minX = points.Min(x => x.x)
                 let maxX = points.Max(x => x.x)
                 let minY = points.Min(x => x.y)
                 let maxY = points.Max(x => x.y)
                 where maxX - minX <= box.x
                 where maxY - minY <= box.y
                 let normalized = from cc in points
                                  let x = cc.x - minX
                                  let y = cc.y - minY
                                  select (x, y)
                 select (seconds, points: normalized);

foreach (var candidate in candidates)
{
    Console.WriteLine($"Elapsed: {candidate.seconds} seconds");

    for (int y = 0; y < box.y; y++)
    {
        for (int x = 0; x < box.x; x++)
        {
            Console.Write(candidate.points.Contains((x, y)) ? "#" : ".");
        }

        Console.WriteLine();
    }

    Console.ReadLine();
    Console.Clear();
}