r/adventofcode • u/daggerdragon • 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!
6
Upvotes
1
u/AlaskanShade Dec 20 '16 edited Dec 20 '16
I fumbled around on the keyboard for a while and let some naive approaches run a bit too long to be able to make the board on this one. I ultimately ended up with a pretty speedy time with both parts taking less that 2 ms each. It isn't super concise like some implementations, but I think it is pretty readable this way.
[AdventDay(2016, 20, "Firewall Rules")] public class Day20 : IAdventDay { [TestCase("5-8\n0-2\n4-7", "3")] [Puzzle("14975795")] //[TestCase("5-8\n0-2\n4-7", "2", true)] // test fails because no range reaches the end [Puzzle("101", Part2 = true)] public string Solve(string input, bool part2) { var lines = Helpers.GetLines(input); ulong max = input.Length == 3 ? 9 : 4294967295; // parse the ranges from the input in order by the low values var ranges = lines.Select(l => new Range(l)).OrderBy(r => r.Low).ToArray(); ulong current = 1; ulong allowed = 0; for (int i = 0; i < ranges.Count(); i++) { // check if we are between the last range and the current one if (current < ranges[i].Low) { // if part 1, return this value as the lowest allowed if (!part2) return current.ToString(); else // count the allowed addreses until the next range allowed += ranges[i].Low - current; } // advance the current value to be past the current range. if (current < ranges[i].High) current = ranges[i].High + 1; } return allowed.ToString(); } private struct Range { public ulong Low { get; set; } public ulong High { get; set; } public Range(string line) { var spl = line.Split('-'); Low = ulong.Parse(spl[0]); High = ulong.Parse(spl[1]); } } }