advent2024

Advent of Code 2024
git clone git://bsandro.tech/advent2024
Log | Files | Refs

main.cpp (3012B)


      1 #include <iostream>
      2 #include <filesystem>
      3 #include <fstream>
      4 #include <vector>
      5 #include <cstring>
      6 #include <cstdio>
      7 #include <tuple>
      8 #include <algorithm>
      9 
     10 typedef std::pair<int,int> Rule;
     11 typedef std::vector<Rule> Rules;
     12 
     13 struct Update {
     14     std::vector<int> data;
     15     bool check(const Rules &rules) {
     16         for (auto it1=data.begin(); it1!=data.end();++it1) {
     17             for (auto it2=it1; it2!=data.end();++it2) {
     18                 for (auto &r : rules) {
     19                     if (r.first==*it2&&r.second==*it1) {
     20                         //printf("%d,%d failed (rule %d,%d)\n", d1,d2,r.first,r.second);
     21                         return false;
     22                     }
     23                 }
     24             }
     25         }
     26         return true;
     27     }
     28     bool checkfix(const Rules &rules) {
     29         bool fixed = false;
     30         for (auto it1=data.begin(); it1!=data.end();++it1) {
     31             for (auto it2=it1; it2!=data.end();++it2) {
     32                 for (auto &r : rules) {
     33                     if (r.first==*it2&&r.second==*it1) {
     34                         //printf("%d,%d failed (rule %d,%d)\n", d1,d2,r.first,r.second);
     35                         std::iter_swap(it1, it2);
     36                         fixed = true;
     37                     }
     38                 }
     39             }
     40         }
     41         return fixed;
     42     }
     43     int middle() {
     44         return data[data.size()/2];
     45     }
     46     void print() {
     47         for (int d : data) {
     48             std::cout << "[" << d << "]";
     49         }
     50     }
     51 };
     52 
     53 struct Game {
     54     Rules rules;
     55     std::vector<Update> updates;
     56 };
     57 
     58 Game read_file(const std::string &path) {
     59     std::ifstream ifs(path, std::ios::binary);
     60     if (!ifs.is_open()) {
     61         throw std::runtime_error(path+":"+std::strerror(errno));
     62     }
     63     Game g;
     64     bool is_rules = true;
     65     while (1) {
     66         std::string str;
     67         std::getline(ifs, str);
     68         if (!ifs) break;
     69         if (str.empty()) {
     70             is_rules = false;
     71             continue;
     72         }
     73         if (is_rules) {
     74             int a,b;
     75             std::sscanf(str.c_str(), "%d|%d", &a, &b);
     76             g.rules.push_back(std::make_pair(a, b));
     77         } else {
     78             std::istringstream ss(str);
     79             Update u;
     80             for (std::string s; std::getline(ss, s, ',');) {
     81                 u.data.push_back(std::stoi(s));
     82             }
     83             g.updates.push_back(u);
     84         }
     85     }
     86     return g;
     87 }
     88 
     89 int part1(Game &g) {
     90     int r = 0;
     91     for (auto &u : g.updates) {
     92         if (u.check(g.rules)) {
     93             r+=u.middle();
     94         }
     95     }
     96     return r;
     97 }
     98 int part2(Game &g) {
     99     (void)g;
    100     int r = 0;
    101     for (auto &u : g.updates) {
    102         if (u.checkfix(g.rules)) {
    103             r+=u.middle();
    104         }
    105     }
    106     return r;
    107 }
    108 
    109 int main(int argc, char **argv) {
    110     const std::string fname = argc>1 ? argv[1] : "test1.txt";
    111     std::cout << "AoC 2024 day 05 " << fname << std::endl;
    112     Game game = read_file(fname);
    113     std::cout << "part1: " << part1(game) << std::endl;
    114     std::cout << "part2: " << part2(game) << std::endl;
    115 
    116     return 0;
    117 }