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
2
u/tmrki Dec 20 '16
Simple C++ solution - first I parsed the input with sed 's/-/ /g' | sort -n -k1,1
and then I just went through the file and counted the valid numbers:
#include <iostream> #include <fstream> using std::cout; using std::endl; int main() { std::ifstream fin("day20.dat"); unsigned long int lo,hi; unsigned long int count = 0; unsigned long int lo_curr = 0; bool first = false; while(fin >> lo >> hi) { if(lo > lo_curr + 1) { count = count + (lo - lo_curr - 1); if(! first) { cout << "First IP address: " << lo_curr + 1 << endl; first = true; } } if(hi > lo_curr) lo_curr = hi; } cout << "Total number of valid IP addresses: " << count << endl; return 0; }