I am the journeyer from the valley of the dead Sega consoles. With the blessings of Sega Saturn, the gaming system of destruction, I am the Scout of Silence… Sailor Saturn.

  • 1 Post
  • 3 Comments
Joined 3 years ago
cake
Cake day: June 29th, 2023

help-circle

  • I decided to double down on 2-2, since bad code is one of life’s little pleasures. Where we’re going we won’t need big-oh notation

    spoiler
    #include <string>
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <iterator>
    
    template <typename It>
    bool seemslegit(It begin, It end) {
        if (std::distance(begin, end) == 1) {
          return true;
        }
        int a = *begin++;
        int b = *begin++;
        if (a == b || std::abs(a-b) > 3) return false;;
        bool increasing = b > a;
        while (begin != end) {
          int c = *begin++;
          if (b == c || std::abs(b-c) > 3) return false;;
          switch (increasing) {
            case false:
              if (c < b) { b = c; continue; }
              return false;
            case true:
              if(c > b) { b = c; continue; }
              return false;
          }
        }
        return true;
    }
    
    template <typename It>
    void debug(It begin, It end) {
      bool legit = seemslegit(begin, end);
      while (begin != end) {
        std::cout << *begin++ << " ";
      }
      //std::cout << ": " << std::boolalpha << legit << std::endl;
    }
    
    int main() {
      int safe = 0;
      std::string s;
      while (std::getline(std::cin, s)) {
        std::istringstream iss(s);
        std::vector<int> report((std::istream_iterator<int>(iss)),
                                std::istream_iterator<int>());
        debug(report.begin(), report.end());
        if (seemslegit(report.begin(), report.end())) {
          safe++;
          std::cout << "\n\n";
          continue;
        }
        for (int i = 0; i < report.size(); ++i) {
          auto report2 = report;
          auto it = report2.erase(report2.begin()+i);
          debug(report2.begin(), report2.end());
          if (seemslegit(report2.begin(), report2.end())) {
            safe++;
            break;
          }
        }
        std::cout << "\n\n";
     }
      std::cout << safe << std::endl;
    }
    
    Commentary

    Doing this “efficiently” should be possible. since you only need ~2-ish look-back you should be able to score reports in O(n) time. One complication is you might get the direction wrong, need to consider that erasing one of the first two elements could change the direction. But that requires thinking, and shoving all the permutations into a function with ungodly amounts of copying does not.


  • 2-1: I have quickly run out of hecks to give. This is the sort of problem that gives prolog programmers feelings of smug superiority.

    spoiler
    #include <string>
    #include <iostream>
    #include <sstream>
    
    int main() {
      int safe = 0;
      std::string s;
      while (std::getline(std::cin, s)) {
        std::istringstream iss(s);
        int a, b, c;
        if (!(iss >> a >> b)) {
          safe++; continue;
        }
        if (a == b || std::abs(a-b) > 3) continue;
        bool increasing = b > a;
        while (iss >> c) {
          if (b == c || std::abs(b-c) > 3) goto structuredprogrammingisfornerds;
          switch (increasing) {
            case false:
              if (c < b) { b = c; continue; }
              goto structuredprogrammingisfornerds;
            case true:
              if(c > b) { b = c; continue; }
              goto structuredprogrammingisfornerds;
          }
        }
        safe++;
        structuredprogrammingisfornerds:;
      }
      std::cout << safe << std::endl;
    }
    

    As usual the second part has punished me for my cowboy code, so I’ll have to take a different more annoying tack (maybe tomorrow). Or you know I could just double down on the haphazard approach…


  • I can’t sleep, so here’s 1-1 and 1-2, unfortunately I couldn’t think of any silly solutions this time, so it’s straightforward instead:

    spoiler
    #include <iostream>
    #include <vector>
    #include <numeric>
    #include <algorithm>
    #include <set>
    #include <iterator>
    
    int main() {
      std::multiset<int> l, r;
      int a, b;
      while (std::cin >> a >> b) {
        l.insert(a); r.insert(b);
      }
      std::vector<int> delta;
      std::transform(l.begin(), l.end(), r.begin(), std::back_inserter(delta),
        [](int x, int y) { return std::abs(x-y); }
      );
      std::cout << std::accumulate(delta.begin(), delta.end(), 0) << std::endl;
    }
    
    spoiler
    #include <iostream>
    #include <numeric>
    #include <set>
    
    int main() {
      std::multiset<int> l, r;
      int a, b;
      while (std::cin >> a >> b) {
        l.insert(a); r.insert(b);
      }
      std::cout << std::accumulate(l.begin(), l.end(), 0, [&r](int acc, int x) {
        return acc + x * r.count(x);
      }) << std::endl;
    }