r/adventofcode • • Dec 04 '18

SOLUTION MEGATHREAD -πŸŽ„- 2018 Day 4 Solutions -πŸŽ„-

--- Day 4: Repose Record ---


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 4

Transcript:

Today’s puzzle would have been a lot easier if my language supported ___.


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!

38 Upvotes

340 comments sorted by

View all comments

1

u/Pyrobolser Dec 05 '18

C# solution for today! If anybody is interested to read or comment, I put my solutions on GitHub too. Any remark or idea appreciated. Have fun folks!

public class Guard
{
    public int Id { get; private set; }
    public Dictionary<int, int> Sleep { get; private set; }

    public Guard(int id)
    {
        Id = id;
        Sleep = Enumerable.Range(0, 60).ToDictionary(d => d, d => 0);
    }
}

public static class Day4Part1
{
    private static string[] InputLines => File.ReadAllLines(@"Inputs\Day4.txt").OrderBy(s => s).ToArray();

    public static void Solve()
    {
        HashSet<Guard> guards = new HashSet<Guard>();
        Guard guard = null;
        int start = 0, end = 0;
        foreach (string line in InputLines)
        {
            Match match = Regex.Match(line, @":(\d+)\] (Guard|falls|wakes) #?(\d+)?");
            switch (match.Groups[2].Value)
            {
                case "Guard":
                    guard = guards.FirstOrDefault(g => g.Id == int.Parse(match.Groups[3].Value));
                    if (guard == null)
                    {
                        guard = new Guard(int.Parse(match.Groups[3].Value));
                        guards.Add(guard);
                    }
                    break;
                case "falls":
                    start = int.Parse(match.Groups[1].Value);
                    break;
                case "wakes":
                    end = int.Parse(match.Groups[1].Value);
                    for (int i = start; i < end; i++)
                        guard.Sleep[i]++;
                    break;
            }
        }

        var sleepy = guards.OrderByDescending(g => g.Sleep.Sum(s => s.Value)).First();
        var most = sleepy.Sleep.OrderByDescending(g => g.Value).Select(s => s.Key).First();

        Console.WriteLine($"The ID of the guard multiplied by the minute is { sleepy.Id * most }.");
    }
}

public static class Day4Part2
{
    private static string[] InputLines => File.ReadAllLines(@"Inputs\Day4.txt").OrderBy(s => s).ToArray();

    public static void Solve()
    {
        HashSet<Guard> guards = new HashSet<Guard>();
        Guard guard = null;
        int start = 0, end = 0;
        foreach (string line in InputLines)
        {
            Match match = Regex.Match(line, @":(\d+)\] (Guard|falls|wakes) #?(\d+)?");
            switch (match.Groups[2].Value)
            {
                case "Guard":
                    guard = guards.FirstOrDefault(g => g.Id == int.Parse(match.Groups[3].Value));
                    if (guard == null)
                    {
                        guard = new Guard(int.Parse(match.Groups[3].Value));
                        guards.Add(guard);
                    }
                    break;
                case "falls":
                    start = int.Parse(match.Groups[1].Value);
                    break;
                case "wakes":
                    end = int.Parse(match.Groups[1].Value);
                    for (int i = start; i < end; i++)
                        guard.Sleep[i]++;
                    break;
            }
        }

        var sleepy = guards.OrderByDescending(g => g.Sleep.Max(s => s.Value)).First();
        var most = sleepy.Sleep.OrderByDescending(g => g.Value).Select(s => s.Key).First();

        Console.WriteLine($"The ID of the guard multiplied by the minute is { sleepy.Id * most }.");
    }
}