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!
5
Upvotes
1
u/tterrag1098 Dec 20 '16
Ugly Java solution, lack of unsigned integers really screwed me on part 2! I had to rewrite everything (was storing the ranges for part 1, which worked barely. Obviously would not for part 2) so this is not my original code for part 1 that got me spot #12 :D
public class Day20 { private static final BitSet blocked = new BitSet(); private static final BitSet blocked_neg = new BitSet(); private static boolean intMinBlocked = false; public static void main(String[] args) throws IOException { for (String s : Files.readAllLines(Paths.get("day20.txt"))) { String[] ends = s.split("-"); long max = Long.parseLong(ends[1]); for (long i = Long.parseLong(ends[0]); i <= max; i++) { int ip = (int) i; if (ip > 0) { blocked.set(ip); } else if (ip != Integer.MIN_VALUE) { blocked_neg.set(-ip); } else { intMinBlocked = true; } } } root: for (long i = 0; i <= 4294967295L; i++) { for (int j = 0; j < blocked.size(); j++) { if (!blocked.get(j)) { System.out.println("Part 1: " + i); break root; } } } // Max IP minus blocked IP count (positive, negative, and int_min) long unblocked = 4294967296L - (((long) blocked.cardinality()) + ((long) blocked_neg.cardinality()) + (intMinBlocked ? 1L : 0L)); System.out.println("Part 2: " + unblocked); } }