Advertisement
davehill

AoC 2022 day 9

Dec 8th, 2022
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include "2022_utils.h"
  2.  
  3. using namespace std;
  4.  
  5. namespace {
  6.   template <typename T> int sgn(T val) {
  7.     return (T(0) < val) - (val < T(0));
  8.   }
  9.  
  10.   auto part1() {
  11.     auto s = load_strings("2022/2022_day09_input.txt");
  12.     int64_t result = 0;
  13.  
  14.     using P = pair<int, int>;
  15.     set<P> all_tail_pos;
  16.  
  17.     P head_pos{ 0, 0, };
  18.     P tail_pos{ 0, 0, };
  19.     all_tail_pos.insert(tail_pos);
  20.     for (auto& ss : s) {
  21.       char dir;
  22.       int steps;
  23.       sscanf_s(ss.c_str(), "%c %d", &dir, 1, &steps);
  24.  
  25.       for (int i = 0; i < steps; ++i) {
  26.         switch (dir) {
  27.         case 'R': {
  28.           head_pos.first += 1;
  29.         }break;
  30.         case 'L': {
  31.           head_pos.first -= 1;
  32.         } break;
  33.         case 'U': {
  34.           head_pos.second -= 1;
  35.         } break;
  36.         case 'D': {
  37.           head_pos.second += 1;
  38.         } break;
  39.         };
  40.  
  41.         int tail_to_head_x = head_pos.first - tail_pos.first;
  42.         int tail_to_head_y = head_pos.second - tail_pos.second;
  43.         if (abs(tail_to_head_x) >= 2 || abs(tail_to_head_y) >= 2) {
  44.           tail_pos.first += sgn(tail_to_head_x);
  45.           tail_pos.second += sgn(tail_to_head_y);
  46.         }
  47.         all_tail_pos.insert(tail_pos);
  48.       }
  49.     }
  50.  
  51.     return all_tail_pos.size();
  52.   }
  53.  
  54.   auto part2() {
  55.     auto s = load_strings("2022/2022_day09_input.txt");
  56.     int64_t result = 0;
  57.  
  58.     using P = pair<int, int>;
  59.     set<P> all_tail_pos;
  60.  
  61.     vector<P> rope(10, P{0, 0});
  62.     all_tail_pos.insert(rope.back());
  63.     for (auto& ss : s) {
  64.       char dir;
  65.       int steps;
  66.       sscanf_s(ss.c_str(), "%c %d", &dir, 1, &steps);
  67.  
  68.       for (int i = 0; i < steps; ++i) {
  69.         switch (dir) {
  70.         case 'R': {
  71.           rope[0].first += 1;
  72.         } break;
  73.         case 'L': {
  74.           rope[0].first -= 1;
  75.         } break;
  76.         case 'U': {
  77.           rope[0].second -= 1;
  78.         } break;
  79.         case 'D': {
  80.           rope[0].second += 1;
  81.         } break;
  82.         };
  83.  
  84.         for (int i = 0; i < rope.size() - 1; ++i) {
  85.           int tail_to_head_x = rope[i].first - rope[i + 1].first;
  86.           int tail_to_head_y = rope[i].second - rope[i + 1].second;
  87.           if (abs(tail_to_head_x) >= 2 || abs(tail_to_head_y) >= 2) {
  88.             rope[i + 1].first += sgn(tail_to_head_x);
  89.             rope[i + 1].second += sgn(tail_to_head_y);
  90.           }
  91.         }
  92.         all_tail_pos.insert(rope.back());
  93.       }
  94.     }
  95.  
  96.     return all_tail_pos.size();
  97.   }
  98. }
  99.  
  100. namespace y2022 {
  101.   void day09() {
  102.     cout << "part 1: " << part1() << "\n";
  103.     cout << "part 2: " << part2() << "\n";
  104.   }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement