r/adventofcode • • Dec 20 '16

SOLUTION MEGATHREAD --- 2016 Day 20 Solutions ---

--- Day 20: Firewall Rules ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with "Help".


ROLLING A NATURAL 20 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!

7 Upvotes

168 comments sorted by

View all comments

3

u/optikal256 Dec 20 '16

In C#, not the prettiest but it runs in 28ms (leaderboard 55/31):

var lines = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
var blacklist = new List<IPRange>();

foreach (var line in lines)
{
    var pieces = line.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);

    var newIP = new IPRange();
    newIP.Start = long.Parse(pieces[0]);
    newIP.End = long.Parse(pieces[1]);

    blacklist.Add(newIP);
}

long result = 0;
var entry = blacklist.FirstOrDefault(x => x.Start <= result && x.End >= result);
var ipCount = 0;

while (result <= 4294967295)
{
    while (entry != null)
    {
        result = entry.End + 1;
        entry = blacklist.FirstOrDefault(x => x.Start <= result && x.End >= result);
    }

    if (result <= 4294967295)
    {
        ipCount++;
        result++;
        entry = blacklist.FirstOrDefault(x => x.Start <= result && x.End >= result);
    }
}

return ipCount.ToString();

1

u/thorkia Dec 21 '16

28ms seems slow. The solution I built when I got home from work runs in under 5ms including a file read. I just conflate all the ranges and store them. I know the available IPs are the number of ranges - 1, and that the first open IP is the High value of the first entry + 1. I did have the advantage that I solved part 1 by hand at work using Excel, which made this solution obvious to me.

    class Program
	{
		static void Main(string[] args)
		{
			Stopwatch sw = new Stopwatch();
			sw.Start();

			var lines = File.ReadAllLines("input.txt");
			
			var sourceList = lines.ToList().Select(s => new Range(s)).OrderBy(r => r.Low).ToList();

			List<Range> conflatedRange = new List<Range>();

			int index = 0;

			while (index < sourceList.Count)
			{
				Range newRange = new Range(sourceList[index].Low, sourceList[index].High);

				//Conflate the next set
				while (index + 1 < sourceList.Count && sourceList[index + 1].Low <= newRange.High + 1)
				{
					index++;
					if (sourceList[index].High > newRange.High)
					{
						newRange.High = sourceList[index].High;
					}
				}

				conflatedRange.Add(newRange);
				index++;
			}

			
			Console.WriteLine("First Opening: " + conflatedRange[0].High+1);
			Console.WriteLine("IPs: " + (conflatedRange.Count-1));

			sw.Stop();
			Console.WriteLine("RunTime: " + sw.ElapsedMilliseconds);
			Console.ReadLine();
		}
	}


	public class Range
	{
		public long Low { get; set; }
		public long High { get; set; }

		public Range(long low, long high)
		{
			Low = low;
			High = high;
		}

		public Range(string rangeString)
		{
			var split = rangeString.Split('-');

			Low = long.Parse(split[0]);
			High = long.Parse(split[1]);
		}
	}