Browse Source

cleanup

master
mid-kid 2 years ago
parent
commit
ef3af3e8c9
  1. 12
      d01.cc
  2. 10
      d02.cc
  3. 3
      d03.cc
  4. 18
      d04.cc
  5. 2
      d05.cc

12
d01.cc

@ -4,9 +4,11 @@
using namespace std; using namespace std;
vector<unsigned> parse() typedef vector<unsigned> Input;
Input parse()
{ {
vector<unsigned> elves; Input elves;
unsigned elf = 0; unsigned elf = 0;
for (string line; getline(cin, line);) { for (string line; getline(cin, line);) {
@ -18,16 +20,16 @@ vector<unsigned> parse()
elf += stoul(line); elf += stoul(line);
} }
sort(elves.begin(), elves.end(), greater<int>()); sort(elves.begin(), elves.end(), greater<unsigned>());
return elves; return elves;
} }
unsigned p1(vector<unsigned> &input) unsigned p1(Input &input)
{ {
return input[0]; return input[0];
} }
unsigned p2(vector<unsigned> &input) unsigned p2(Input &input)
{ {
return input[0] + input[1] + input[2]; return input[0] + input[1] + input[2];
} }

10
d02.cc

@ -4,9 +4,11 @@
using namespace std; using namespace std;
vector<array<unsigned, 2>> parse() typedef vector<array<unsigned, 2>> Input;
Input parse()
{ {
vector<array<unsigned, 2>> input; Input input;
for (string line; getline(cin, line);) { for (string line; getline(cin, line);) {
if (line.at(1) != ' ') throw "parse"; if (line.at(1) != ' ') throw "parse";
unsigned oppo = line.at(0) - 'A'; unsigned oppo = line.at(0) - 'A';
@ -17,7 +19,7 @@ vector<array<unsigned, 2>> parse()
return input; return input;
} }
unsigned p1(vector<array<unsigned, 2>> &input) unsigned p1(Input &input)
{ {
unsigned score = 0; unsigned score = 0;
for (auto i : input) { for (auto i : input) {
@ -28,7 +30,7 @@ unsigned p1(vector<array<unsigned, 2>> &input)
return score; return score;
} }
unsigned p2(vector<array<unsigned, 2>> &input) unsigned p2(Input &input)
{ {
unsigned score = 0; unsigned score = 0;
for (auto i : input) { for (auto i : input) {

3
d03.cc

@ -1,6 +1,7 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <unordered_set> #include <unordered_set>
#include <algorithm>
using namespace std; using namespace std;
@ -25,7 +26,7 @@ template<class T>
unordered_set<T> intersect(unordered_set<T> &a, unordered_set<T> &b) unordered_set<T> intersect(unordered_set<T> &a, unordered_set<T> &b)
{ {
unordered_set<T> o; unordered_set<T> 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; return o;
} }

18
d04.cc

@ -1,11 +1,11 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <array> #include <array>
#include <algorithm>
using namespace std; using namespace std;
class Range { struct Range {
public:
unsigned min; unsigned min;
unsigned max; unsigned max;
@ -35,21 +35,21 @@ Input parse()
unsigned p1(const Input &input) unsigned p1(const Input &input)
{ {
unsigned count = 0; return count_if(input.begin(), input.end(), [](decltype(input[0]) i){
for (auto i : input) if (i[0].contains(i[1]) || i[1].contains(i[0])) count++; return i[0].contains(i[1]) || i[1].contains(i[0]);
return count; });
} }
unsigned p2(const Input &input) unsigned p2(const Input &input)
{ {
unsigned count = 0; return count_if(input.begin(), input.end(), [](decltype(input[0]) i){
for (auto i : input) if (i[0].overlaps(i[1])) count++; return i[0].overlaps(i[1]);
return count; });
} }
int main() int main()
{ {
Input input = parse(); auto input = parse();
cout << p1(input) << endl; cout << p1(input) << endl;
cout << p2(input) << endl; cout << p2(input) << endl;
} }

2
d05.cc

@ -90,7 +90,7 @@ string p2(const Input &input)
int main() int main()
{ {
Input input = parse(); auto input = parse();
cout << p1(input) << endl; cout << p1(input) << endl;
cout << p2(input) << endl; cout << p2(input) << endl;
} }

Loading…
Cancel
Save