Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Main()
- {
- //test if all works
- var testA = new string[] { "abcdef", "bababc", "abbcde", "abcccd", "aabcdd", "abcdee", "ababab", };
- var testB = new string[] { "abcde", "fghij", "klmno", "pqrst", "fguij", "axcye", "wvxyz", };
- //verify
- Debug.Assert(GetAnswerA(testA) == 12);
- Debug.Assert(GetAnswerB(testB) == "fgij");
- //Run
- var aoc = new AdventOfCode(2018, 2);
- //part A
- aoc.SubmitAnswer(GetAnswerA(aoc.InputLines), Part.A);
- //part B
- aoc.SubmitAnswer(GetAnswerB(aoc.InputLines), Part.B);
- }
- public string GetAnswerB(string[] lines)
- {
- string best = "";
- for(int i = 0; i < lines.Length; i++)
- {
- for(int j = i +1; j < lines.Length; j++)
- {
- var score = new string(lines[i].Zip(lines[j], (a,b) => (a, b)) //create tuple to chars
- .Where(x => x.a == x.b) //keep the chars that are identical
- .Select(x=> x.a).ToArray()); //keep 1 of the 2, and create a string
- if(best.Length < score.Length) best = score; //save the longest
- }
- }
- return best;
- }
- public int GetAnswerA(string[] lines)
- {
- return lines.SelectMany(
- row => row.GroupBy(x => x) //for a string group on chars, and mamre readable tuple
- .Select(x => x.Count())
- .Where(x => x >= 2) //keep bigger than 2
- .Distinct() //remove duplicates
- ) //we now have for each row, the unique duplicate counts
- .GroupBy(x => x) //group the counts
- .Select(x => x.Count()) //see how often each is there
- .Aggregate(1, ((x, y) => x * y)) //multiple
- ;
- }
- // Define other methods and classes here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement