Advertisement
Tkap1

Untitled

Dec 4th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #include "tk_types.h"
  7. #define assert(...)
  8. #define m_tk_array_impl
  9. #include "tk_array.h"
  10.  
  11. #include "utils.h"
  12.  
  13. int main()
  14. {
  15.     FILE* f = fopen("data05.txt", "r");
  16.     fseek(f, 0, SEEK_END);
  17.     int size = ftell(f);
  18.     fseek(f, 0, SEEK_SET);
  19.     char* text = (char*)calloc(1, size + 1);
  20.     fread(text, 1, size, f);
  21.  
  22.     constexpr int n = 140;
  23.  
  24.     s64 result = 0;
  25.  
  26.     struct s_rule
  27.     {
  28.         int a;
  29.         int b;
  30.     };
  31.     struct s_update
  32.     {
  33.         s_list<int, 32> nums;
  34.     };
  35.     s_list<s_rule, 2048> rules = zero;
  36.     s_list<s_update, 2048> updates = zero;
  37.  
  38.     while(true) {
  39.         if(consume_newline(&text)) { break; }
  40.         char* end = null;
  41.         int a = strtoll(text, &end, 10);
  42.         text = end;
  43.         text += 1;
  44.  
  45.         int b = strtoll(text, &end, 10);
  46.         text = end;
  47.  
  48.         rules.add({a, b});
  49.  
  50.         consume_newline(&text);
  51.     }
  52.  
  53.     s_update update = zero;
  54.     while(*text) {
  55.         if(consume_newline(&text)) { break; }
  56.         char* end = null;
  57.         int a = strtoll(text, &end, 10);
  58.         text = end;
  59.  
  60.         update.nums.add(a);
  61.  
  62.         if(consume_newline(&text)) {
  63.             updates.add(update);
  64.             update = zero;
  65.         }
  66.         else {
  67.             text += 1;
  68.         }
  69.     }
  70.  
  71.     foreach_val(update_i, update, updates) {
  72.         b8 ok = true;
  73.         foreach_val(n_i, n, update.nums) {
  74.             foreach_val(n2_i, n2, update.nums) {
  75.                 if(n_i == n2_i) { continue; }
  76.                 foreach_val(rule_i, rule, rules) {
  77.                     if(rule.a == n && rule.b == n2) {
  78.                         if(!(n_i < n2_i)) { ok = false; }
  79.                     }
  80.                     if(rule.a == n2 && rule.b == n) {
  81.                         if(!(n2_i < n_i)) { ok = false; }
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.         if(ok) {
  87.             result += update.nums[update.nums.count / 2];
  88.         }
  89.     }
  90.  
  91.  
  92.     printf("%lli\n", result);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement