this post was submitted on 02 Dec 2024
14 points (100.0% liked)

NotAwfulTech

385 readers
3 users here now

a community for posting cool tech news you don’t want to sneer at

non-awfulness of tech is not required or else we wouldn’t have any posts

founded 1 year ago
MODERATORS
 

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.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 3 points 2 weeks ago* (last edited 2 weeks ago)

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

spoilerstable 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;
}