Browse Source

minor cleanups

master
mid-kid 2 years ago
parent
commit
8d4017c0da
  1. 3
      d01.cc
  2. 11
      d02.cc

3
d01.cc

@ -8,9 +8,8 @@ vector<unsigned> parse()
{ {
vector<unsigned> elves; vector<unsigned> elves;
string line;
unsigned elf = 0; unsigned elf = 0;
while (getline(cin, line)) { for (string line; getline(cin, line);) {
if (line.empty()) { if (line.empty()) {
elves.push_back(elf); elves.push_back(elf);
elf = 0; elf = 0;

11
d02.cc

@ -7,12 +7,11 @@ using namespace std;
vector<array<unsigned, 2>> parse() vector<array<unsigned, 2>> parse()
{ {
vector<array<unsigned, 2>> input; vector<array<unsigned, 2>> input;
string line; for (string line; getline(cin, line);) {
while (getline(cin, line)) { if (line.size() < 3 || line[1] != ' ') throw "parse";
if (line.size() < 3 || line[1] != ' ') abort();
unsigned oppo = line[0] - 'A'; unsigned oppo = line[0] - 'A';
unsigned user = line[2] - 'X'; unsigned user = line[2] - 'X';
if (oppo >= 3 || user >= 3) abort(); if (oppo >= 3 || user >= 3) throw "parse";
input.push_back({oppo, user}); input.push_back({oppo, user});
} }
return input; return input;
@ -21,7 +20,7 @@ vector<array<unsigned, 2>> parse()
unsigned p1(vector<array<unsigned, 2>> &input) unsigned p1(vector<array<unsigned, 2>> &input)
{ {
unsigned score = 0; unsigned score = 0;
for (array<unsigned, 2> i : input) { for (auto i : input) {
unsigned shape = i[1]; unsigned shape = i[1];
unsigned outcome = (i[1] - i[0] + 4) % 3; unsigned outcome = (i[1] - i[0] + 4) % 3;
score += shape + 1 + outcome * 3; score += shape + 1 + outcome * 3;
@ -32,7 +31,7 @@ unsigned p1(vector<array<unsigned, 2>> &input)
unsigned p2(vector<array<unsigned, 2>> &input) unsigned p2(vector<array<unsigned, 2>> &input)
{ {
unsigned score = 0; unsigned score = 0;
for (array<unsigned, 2> i : input) { for (auto i : input) {
unsigned shape = (i[1] + i[0] + 2) % 3; unsigned shape = (i[1] + i[0] + 2) % 3;
unsigned outcome = i[1]; unsigned outcome = i[1];
score += shape + 1 + outcome * 3; score += shape + 1 + outcome * 3;

Loading…
Cancel
Save