private static List <BlockedIPRange> GetBlockedIPRanges() { StreamReader file = new StreamReader("input20.txt"); List <BlockedIPRange> blockedIPRanges = new List <BlockedIPRange>(); string line; while ((line = file.ReadLine()) != null) { string[] startAndEnd = line.Split('-'); uint rangeStart = Convert.ToUInt32(startAndEnd[0]); uint rangeEnd = Convert.ToUInt32(startAndEnd[1]); BlockedIPRange nextBlockedIPRange = new BlockedIPRange(rangeStart, rangeEnd); blockedIPRanges.Add(nextBlockedIPRange); } return(blockedIPRanges); }
private static uint FindLowestAllowedIP(List <BlockedIPRange> blockedIPRanges) { uint highestEnd = blockedIPRanges[0].GetEnd(); for (int i = 0; i < blockedIPRanges.Count - 1; i++) { BlockedIPRange range = blockedIPRanges[i]; BlockedIPRange nextRange = blockedIPRanges[i + 1]; if (range.GetEnd() > highestEnd) { highestEnd = range.GetEnd(); } if (nextRange.GetStart() > highestEnd + 1) { return(highestEnd + 1); } } throw new Exception("All IPs are blocked. Sorry"); }
private static uint FindNumAllowedIPs(List <BlockedIPRange> blockedIPRanges) { uint numAllowedIPs = 0; uint highestEnd = blockedIPRanges[0].GetEnd(); for (int i = 0; i < blockedIPRanges.Count - 1; i++) { BlockedIPRange range = blockedIPRanges[i]; BlockedIPRange nextRange = blockedIPRanges[i + 1]; if (range.GetEnd() > highestEnd) { highestEnd = range.GetEnd(); } if (nextRange.GetStart() - 1 > highestEnd) { numAllowedIPs += nextRange.GetStart() - highestEnd - 1; highestEnd = nextRange.GetEnd(); } } return(numAllowedIPs); }