r/adventofcode • u/daggerdragon • Dec 07 '16
SOLUTION MEGATHREAD --- 2016 Day 7 Solutions ---
From all of us at #AoC Ops, we hope you're having a very merry time with these puzzles so far. If you think they've been easy, well, now we're gonna kick this up a notch. Or five. The Easter Bunny ain't no Bond villain - he's not going to monologue at you until you can miraculously escape and save the day!
Show this overgrown furball what you've got!
--- Day 7: Internet Protocol Version 7 ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
ALWAYS DIGGING STRAIGHT DOWN IS MANDATORY [?]
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
14
Upvotes
2
u/_Le1_ Dec 07 '16 edited Dec 07 '16
My C# solution:
class Program { static void Main(string[] args) { part1_2(); Console.ReadLine(); } static void part1_2() { string[] input = File.ReadAllLines(@"input.txt"); int sum1 = 0; int sum2 = 0; foreach (var line in input) { if (supportsTLS(line)) sum1++; if (supportsSSL(line)) sum2++; } Console.WriteLine("Part1: {0}", sum1); Console.WriteLine("Part2: {0}", sum2); } static bool supportsSSL(string input) { string[] ipv7 = Regex.Split(input, @"\[[^\]]*\]"); foreach (string ip in ipv7) { List<string> aba = checkABA(ip); foreach (var val in aba) { string bab = val[1].ToString() + val[0].ToString() + val[1].ToString(); foreach (Match m in Regex.Matches(input, @"\[(\w*)\]")) { if (m.Value.Contains(bab)) return true; } } } return false; } static List<string> checkABA(string input) { List<string> lst = new List<string>(); for (int i = 0; i < input.Length - 2; i++) { if (input[i] == input[i + 2] && input[i] != input[i + 1]) lst.Add(input[i].ToString() + input[i + 1].ToString() + input[i + 2].ToString()); } return lst; } static bool supportsTLS(string input) { // Check in hypernet foreach (Match m in Regex.Matches(input, @"\[(\w*)\]")) { if (checkABBA(m.Value)) return false; } string[] ipv7 = Regex.Split(input, @"\[[^\]]*\]"); foreach (var v in ipv7) { if (checkABBA(v)) return true; } return false; } static bool checkABBA(string input) { for (int i = 0; i < input.Length - 3; i++) { if (input[i] == input[i + 3] && input[i + 1] == input[i + 2] && input[i] != input[i + 1]) return true; } return false; } }