~cdv/aoc-2018-cc

6ce94c266ea47bc482c17da73a9c3b3f4ca615c0 — Christopher Vittal 1 year, 3 months ago 019119d
Solve day 2
1 files changed, 44 insertions(+), 3 deletions(-)

M day02.cc
M day02.cc => day02.cc +44 -3
@@ 4,8 4,42 @@
#include <iterator>
#include <vector>

static std::pair<bool, bool> count(std::string &s) {
  std::array<uint8_t, 26> freqs{0, 26};
static std::pair<bool, bool> count(const std::string &s) {
  static std::array<uint8_t, 26> freqs{};
  freqs.fill(0);
  for (const char c : s)
    ++freqs[c - 'a'];
  std::pair ret{false, false};
  for (const auto i : freqs) {
    if (i == 2)
      ret.first = true;
    else if (i == 3)
      ret.second = true;
  }
  return ret;
}

static std::string part_two(const std::vector<std::string> &lines) {
  std::string ret;
  for (auto a = lines.cbegin(); a != lines.cend(); ++a) {
    for (auto b = a+1; b != lines.cend(); ++b) {
      bool found = false;
      ret.clear();
      auto c{a->begin()}, d{b->begin()};
      for (; c != a->end() && d != b->end(); ++c, ++d) {
        if (*c == *d)
          ret.push_back(*c);
        else if (found)
          break;
        else
          found = true;
      }
      if (c == a->end() && found) {
        return ret;
      }
    }
  }
  return ret;
}

template <>


@@ 13,8 47,15 @@ template <bool p2>
void Day<2>::solve(std::istream &is, std::ostream &os) {
  std::vector<std::string> lines(std::istream_iterator<std::string>(is), {});
  if constexpr (!p2) {
    int two{0}, three{0};
    for (auto &s : lines) {
      auto [tw, th] = count(s);
      two += tw;
      three += th;
    }
    os << two * three;
  } else {
    os << "NYI";
    os << part_two(lines);
  }
}
template void Day<2>::solve<true>(std::istream& is, std::ostream& os);