Advertisement
Guest User

Day4, part 1,2

a guest
Dec 4th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.82 KB | None | 0 0
  1.     private void Day4Part1()
  2.     {
  3.         string[] lines = File.ReadAllLines("C:/input.txt");
  4.  
  5.         long finalSum = 0;
  6.  
  7.         Dictionary<char, int> charCount = new Dictionary<char, int>();
  8.  
  9.         for(int i = 0; i < lines.Length; i++)
  10.         {
  11.             string entry = lines[i];
  12.             string entryChecksum = entry.Substring(entry.LastIndexOf("[") + 1, 5);
  13.             int sectorId = int.Parse(entry.Substring(entry.LastIndexOf("-") + 1, entry.LastIndexOf("[") - entry.LastIndexOf("-") - 1));
  14.             string letters = new string(entry.Substring(0, entry.LastIndexOf('-')).Replace("-", "").OrderBy(c => c).ToArray());
  15.  
  16.             charCount.Clear();
  17.             for(int j = 0; j < letters.Length; j++)
  18.             {
  19.                 if(!charCount.ContainsKey(letters[j]))
  20.                 {
  21.                     charCount.Add(letters[j], 1);
  22.                 }
  23.                 else
  24.                 {
  25.                     charCount[letters[j]]++;
  26.                 }
  27.             }
  28.  
  29.             string checksum = new string(charCount.OrderByDescending(r => r.Value).Select(r => r.Key).Take(5).ToArray());
  30.             if(checksum == entryChecksum)
  31.             {
  32.                 finalSum += sectorId;
  33.             }
  34.         }
  35.         Debug.Log(finalSum);
  36.     }
  37.  
  38.     private void Day4Part2()
  39.     {
  40.         string[] lines = File.ReadAllLines("C:/input.txt");
  41.  
  42.         for(int i = 0; i < lines.Length; i++)
  43.         {
  44.             string entry = lines[i];
  45.             string entryChecksum = entry.Substring(entry.LastIndexOf("[") + 1, 5);
  46.             int sectorId = int.Parse(entry.Substring(entry.LastIndexOf("-") + 1, entry.LastIndexOf("[") - entry.LastIndexOf("-") - 1));
  47.             string letters = entry.Substring(0, entry.LastIndexOf('-'));
  48.  
  49.             string location = "";
  50.             for(int j = 0; j < letters.Length; j++)
  51.             {
  52.                 location += Shift(letters[j], sectorId);
  53.             }
  54.             if(location.Contains("pole") && location.Contains("north"))
  55.             {
  56.                 Debug.Log(sectorId);
  57.             }
  58.         }
  59.     }
  60.  
  61.     private char Shift(char c, int count)
  62.     {
  63.         count = count % 26;
  64.  
  65.         int ac = (int)c - 97;
  66.         ac += count;
  67.         ac = ac % 26;
  68.         ac += 97;
  69.         return (char)ac;
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement