copy pasting the rules from last year’s thread:

Rules: no spoilers.

The other rules are made up aswe go along.

Share code by link to a forge, home page, pastebin (Eric Wastl has one here) or code section in a comment.

  • Sailor Sega Saturn@awful.systems
    link
    fedilink
    English
    arrow-up
    3
    ·
    edit-2
    17 days ago

    OK nerds, I was coerced into doing day five so I’m posting it here.

    spoiler

    stable sort with the ordering critera as the sort condition and it just works, either that or I got lucky inputs

    5-1 / 5-2
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <sstream>
    #include <string>
    #include <unordered_map>
    
    std::unordered_multimap<int, int> ordering;
    
    bool sorted_before(int a, int b) {
      auto range = ordering.equal_range(a);
      for (auto it = range.first; it != range.second; ++it) {
        if (it->second == b) return true;
      }
      return false;
    }
    
    int main() {
      int sum = 0;
      std::string line;
      while (std::getline(std::cin, line) && !line.empty()) {
        int l, r;
        char c;
        std::istringstream iss(line);
        iss >> l >> c >> r;
        ordering.insert(std::make_pair(l,r));
      }
      while (std::getline(std::cin, line)) {
        std::istringstream iss(line);
        std::vector<int> pages;
        int page;
        while (iss >> page) {
          pages.push_back(page);
          iss.get();
        }   
        std::vector<int> sorted_pages = pages;
        std::stable_sort(sorted_pages.begin(), sorted_pages.end(), sorted_before);
        if (pages == sorted_pages) {  // Change to != for 5-2
          sum += sorted_pages[sorted_pages.size()/2];
        }
      }
      std::cout << "Sum: " << sum << std::endl;
    }