Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void Day4Part1()
- {
- string[] lines = File.ReadAllLines("C:/input.txt");
- long finalSum = 0;
- Dictionary<char, int> charCount = new Dictionary<char, int>();
- for(int i = 0; i < lines.Length; i++)
- {
- string entry = lines[i];
- string entryChecksum = entry.Substring(entry.LastIndexOf("[") + 1, 5);
- int sectorId = int.Parse(entry.Substring(entry.LastIndexOf("-") + 1, entry.LastIndexOf("[") - entry.LastIndexOf("-") - 1));
- string letters = new string(entry.Substring(0, entry.LastIndexOf('-')).Replace("-", "").OrderBy(c => c).ToArray());
- charCount.Clear();
- for(int j = 0; j < letters.Length; j++)
- {
- if(!charCount.ContainsKey(letters[j]))
- {
- charCount.Add(letters[j], 1);
- }
- else
- {
- charCount[letters[j]]++;
- }
- }
- string checksum = new string(charCount.OrderByDescending(r => r.Value).Select(r => r.Key).Take(5).ToArray());
- if(checksum == entryChecksum)
- {
- finalSum += sectorId;
- }
- }
- Debug.Log(finalSum);
- }
- private void Day4Part2()
- {
- string[] lines = File.ReadAllLines("C:/input.txt");
- for(int i = 0; i < lines.Length; i++)
- {
- string entry = lines[i];
- string entryChecksum = entry.Substring(entry.LastIndexOf("[") + 1, 5);
- int sectorId = int.Parse(entry.Substring(entry.LastIndexOf("-") + 1, entry.LastIndexOf("[") - entry.LastIndexOf("-") - 1));
- string letters = entry.Substring(0, entry.LastIndexOf('-'));
- string location = "";
- for(int j = 0; j < letters.Length; j++)
- {
- location += Shift(letters[j], sectorId);
- }
- if(location.Contains("pole") && location.Contains("north"))
- {
- Debug.Log(sectorId);
- }
- }
- }
- private char Shift(char c, int count)
- {
- count = count % 26;
- int ac = (int)c - 97;
- ac += count;
- ac = ac % 26;
- ac += 97;
- return (char)ac;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement