diff --git a/d01.cc b/d01.cc index 8274cd4..617a402 100644 --- a/d01.cc +++ b/d01.cc @@ -4,9 +4,11 @@ using namespace std; -vector parse() +typedef vector Input; + +Input parse() { - vector elves; + Input elves; unsigned elf = 0; for (string line; getline(cin, line);) { @@ -18,16 +20,16 @@ vector parse() elf += stoul(line); } - sort(elves.begin(), elves.end(), greater()); + sort(elves.begin(), elves.end(), greater()); return elves; } -unsigned p1(vector &input) +unsigned p1(Input &input) { return input[0]; } -unsigned p2(vector &input) +unsigned p2(Input &input) { return input[0] + input[1] + input[2]; } diff --git a/d02.cc b/d02.cc index 9f49b94..bc6a0c3 100644 --- a/d02.cc +++ b/d02.cc @@ -4,9 +4,11 @@ using namespace std; -vector> parse() +typedef vector> Input; + +Input parse() { - vector> input; + Input input; for (string line; getline(cin, line);) { if (line.at(1) != ' ') throw "parse"; unsigned oppo = line.at(0) - 'A'; @@ -17,7 +19,7 @@ vector> parse() return input; } -unsigned p1(vector> &input) +unsigned p1(Input &input) { unsigned score = 0; for (auto i : input) { @@ -28,7 +30,7 @@ unsigned p1(vector> &input) return score; } -unsigned p2(vector> &input) +unsigned p2(Input &input) { unsigned score = 0; for (auto i : input) { diff --git a/d03.cc b/d03.cc index df3eaf3..48986c2 100644 --- a/d03.cc +++ b/d03.cc @@ -1,6 +1,7 @@ #include #include #include +#include using namespace std; @@ -25,7 +26,7 @@ template unordered_set intersect(unordered_set &a, unordered_set &b) { unordered_set o; - for (auto x : a) if (b.count(x)) o.insert(x); + copy_if(a.begin(), a.end(), inserter(o, o.end()), [&](T i){return b.count(i);}); return o; } diff --git a/d04.cc b/d04.cc index ebf80f3..d4dc58d 100644 --- a/d04.cc +++ b/d04.cc @@ -1,11 +1,11 @@ #include #include #include +#include using namespace std; -class Range { -public: +struct Range { unsigned min; unsigned max; @@ -35,21 +35,21 @@ Input parse() unsigned p1(const Input &input) { - unsigned count = 0; - for (auto i : input) if (i[0].contains(i[1]) || i[1].contains(i[0])) count++; - return count; + return count_if(input.begin(), input.end(), [](decltype(input[0]) i){ + return i[0].contains(i[1]) || i[1].contains(i[0]); + }); } unsigned p2(const Input &input) { - unsigned count = 0; - for (auto i : input) if (i[0].overlaps(i[1])) count++; - return count; + return count_if(input.begin(), input.end(), [](decltype(input[0]) i){ + return i[0].overlaps(i[1]); + }); } int main() { - Input input = parse(); + auto input = parse(); cout << p1(input) << endl; cout << p2(input) << endl; } diff --git a/d05.cc b/d05.cc index a0e9ece..72237d6 100644 --- a/d05.cc +++ b/d05.cc @@ -90,7 +90,7 @@ string p2(const Input &input) int main() { - Input input = parse(); + auto input = parse(); cout << p1(input) << endl; cout << p2(input) << endl; }