Equd

AOC 2020 Day 06

Dec 6th, 2020
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1. #load "xunit"
  2. #load "AOC Connector"
  3. #load "AOC 2d Array"
  4.  
  5. void Main()
  6. {  
  7.     RunTests();  // Call RunTests() or press Alt+Shift+T to initiate testing.
  8.  
  9.     //get aoc data
  10.     var aoc = new AdventOfCode(2020, 6);       
  11.        
  12.     //solve A  
  13.     aoc.SubmitAnswer(SolveA(aoc.InputLines), Part.A);
  14.    
  15.     //solve B
  16.     aoc.SubmitAnswer(SolveB(aoc.InputLines), Part.B);
  17. }
  18.  
  19. public int SolveA(string[] input)
  20. {
  21.     return input.SplitInputOnDoubleBreak()
  22.         .Select(x => x.Where(y => y != ' ') //grab all items not whitespace
  23.             .Distinct().Count() //count the unique items
  24.         ).Sum(); //total
  25. }
  26.  
  27. public int SolveB(string[] input)
  28. {
  29.     return input.SplitInputOnDoubleBreak()
  30.     .Sum(x =>
  31.     (      
  32.         x.Replace(" ", "") //remove whitespaces
  33.         .GroupBy(y => y) //count each answer
  34.         .Count(y => y.Count() == x.Count(y => y == ' ') + 1)     //count the whitespace in in the original and + 1 is people count,
  35.     ));
  36. }
  37.  
  38. #region private::Tests
  39. #region test data
  40. string[] testData = new []
  41. {
  42. "abc",
  43. "",
  44. "a",
  45. "b",
  46. "c",
  47. "",
  48. "ab",
  49. "ac",
  50. "",
  51. "a",
  52. "a",
  53. "a",
  54. "a",
  55. "",
  56. "b",
  57. };
  58.  
  59. #endregion
  60. //tests
  61. [Fact] void TestA() => Assert.Equal(11, SolveA(testData));
  62. [Fact] void TestB() => Assert.Equal(6,  SolveB(testData));
  63. #endregion
  64.  
  65.  
  66.  
Advertisement
Add Comment
Please, Sign In to add comment