Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. using System.IO;
  2. using System.Linq;
  3. using System.Text;
  4. using static System.Console;
  5.  
  6. namespace AdventOfCode2016
  7. {
  8. public static class Day6
  9. {
  10. // Decode the messages.
  11. public static void Resolve()
  12. {
  13. var input = ReadInput();
  14. var encoded = ReverseInput(input);
  15. var messagePartOne = GetMessage(encoded, true);
  16. var messagePartTwo = GetMessage(encoded, false);
  17.  
  18. WriteLine($"The message being sent is { messagePartOne }.");
  19. WriteLine($"The message actually being sent by Santa is { messagePartTwo }.");
  20. }
  21.  
  22. // Read the Day 6 input.
  23. private static string[] ReadInput()
  24. {
  25. return File.ReadAllLines(@"Inputs\Day6.txt");
  26. }
  27.  
  28. // Put the columns in line for easy processing.
  29. private static char[][] ReverseInput(string[] input)
  30. {
  31. char[][] encoded = new char[input[0].Length][];
  32.  
  33. for (int i = 0; i < encoded.GetLength(0); i++)
  34. encoded[i] = new char[input.Length];
  35.  
  36. for (int i = 0; i < input.Length; i++)
  37. {
  38. encoded[0][i] = input[i][0];
  39. encoded[1][i] = input[i][1];
  40. encoded[2][i] = input[i][2];
  41. encoded[3][i] = input[i][3];
  42. encoded[4][i] = input[i][4];
  43. encoded[5][i] = input[i][5];
  44. encoded[6][i] = input[i][6];
  45. encoded[7][i] = input[i][7];
  46. }
  47.  
  48. return encoded;
  49. }
  50.  
  51. // Find the letter for every column.
  52. private static string GetMessage(char[][] encoded, bool isMostFrequent)
  53. {
  54. var builder = new StringBuilder();
  55. if (isMostFrequent)
  56. {
  57. for (int i = 0; i < encoded.GetLength(0); i++)
  58. {
  59. builder.Append(encoded[i].GroupBy(c => c).OrderByDescending(c => c.Count()).Select(c => c.Key).First());
  60. }
  61. }
  62. else
  63. {
  64. for (int i = 0; i < encoded.GetLength(0); i++)
  65. {
  66. builder.Append(encoded[i].GroupBy(c => c).OrderBy(c => c.Count()).Select(c => c.Key).First());
  67. }
  68. }
  69.  
  70. return builder.ToString();
  71. }
  72.  
  73.  
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement