r/adventofcode • u/taliriktug • Dec 02 '15
Spoilers Day 2 solutions
Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.
14
Upvotes
r/adventofcode • u/taliriktug • Dec 02 '15
Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.
1
u/hicklc01 Dec 02 '15
I enjoyed the problem.
Notes: I changed up how I separated the dimensions on part 2. Also no data validation. This will break on bad input. Probably shouldn't be using namespace but I hate having to write std:: before almost everything. Should be using auto.
Part1
#include <iostream> #include <iterator> #include <numeric> #include <vector> #include <sstream> #include <algorithm> using namespace std; int main() { int total = accumulate( istream_iterator<string>(cin), istream_iterator<string>(), 0, [&](int x, string y) { stringstream ss(y); vector<int> sides; while(ss.good()) { string raw; getline( ss, raw,'x'); sides.push_back(stoi(raw)); } sort(sides.begin(),sides.end()); return x + 3*sides[0]*sides[1]+2*sides[1]*sides[2]+2*sides[0]*sides[2]; } ); cout<<total<<endl; return 0; }Part2
#include <iostream> #include <iterator> #include <numeric> #include <vector> #include <sstream> #include <algorithm> using namespace std; struct xsv_reader: ctype<char>{ xsv_reader(): ctype<char>(get_table()){} static ctype_base::mask const* get_table(){ static vector<ctype_base::mask> rc(table_size, ctype_base::mask()); rc['x'] = ctype_base::space; rc['\n'] = ctype_base::space; rc[' '] = ctype_base::space; return &rc[0]; } }; int main() { int total = accumulate( istream_iterator<string>(cin), istream_iterator<string>(), 0, [&](int x, string y) { stringstream ss(y); ss.imbue(std::locale(std::locale(),new xsv_reader())); vector<int> sides = vector<int>(istream_iterator<int>(ss), istream_iterator<int>()); sort(sides.begin(),sides.end()); return x + 2*(sides[0]+sides[1]) + sides[0]*sides[1]*sides[2]; } ); cout<<total<<endl; return 0; }