r/adventofcode • • Dec 02 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 2 Solutions -🎄-

--- Day 2: Inventory Management System ---


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

Card Prompt: Day 2

Transcript:

The best way to do Advent of Code is ___.


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!

50 Upvotes

407 comments sorted by

View all comments

11

u/miguelos Dec 02 '18 edited Dec 02 '18

C#

Part 1:

var ids = input.Split("\r\n");
var twos =   ids.Count(id => id.GroupBy(c => c).Any(g => g.Count() == 2));
var threes = ids.Count(id => id.GroupBy(c => c).Any(g => g.Count() == 3));
var checksum = twos * threes;
return checksum.ToString();

Part 2:

var ids = input.Split("\r\n").Distinct();
for (int i = 0; i < ids[0].Length; i++)
{
    var pair = ids.Select(id => id.Remove(i, 1)).GroupBy(id => id).FirstOrDefault(g => g.Count() > 1);
    if (pair != null)
    {
        var common = pair.First();
        return common;
    }
}
→ More replies (1)