From ad3e9e42bbfb01965c052bf123fe54fd771d12f4 Mon Sep 17 00:00:00 2001 From: David Vereb Date: Sat, 17 Dec 2022 14:51:01 -0500 Subject: [PATCH] Added part 2. --- 2022/9/main.cpp | 122 ++++++++++++++++++++++++++++++------------------ 1 file changed, 77 insertions(+), 45 deletions(-) diff --git a/2022/9/main.cpp b/2022/9/main.cpp index efa28f3..5ddcc3a 100644 --- a/2022/9/main.cpp +++ b/2022/9/main.cpp @@ -6,13 +6,39 @@ #include #include -void DebugPrint(std::pair &head, std::pair &tail, - std::set > &visited_positions) +void ApplyMovement(std::pair &head, + std::pair &tail) { - int x_min = head.first; - int y_min = head.second; - int x_max = head.first; - int y_max = head.second; + const int x_diff = head.first - tail.first; + const int y_diff = head.second - tail.second; + if(std::abs(x_diff) > 1) // 2 away + { + tail.first += (head.first - tail.first) / 2; + if(std::abs(y_diff) > 1) // 2 away due to diagonally following!!! + tail.second += (head.second - tail.second) / 2; + else + // snap to axis since you're already on it or you're one away + tail.second = head.second; + } + if(std::abs(y_diff) > 1) // 2 away + { + tail.second += (head.second - tail.second) / 2; + if(std::abs(x_diff) > 1) // 2 away due to diagonally following!!! + tail.first += (head.first - tail.first) / 2; + else + // snap to axis since you're already on it or you're one away + tail.first = head.first; + } +} + +void DebugPrint(std::vector > &points, + std::set > &visited_positions, + std::set > &visited_positions_part2) +{ + int x_min = points[0].first; + int y_min = points[0].second; + int x_max = points[0].first; + int y_max = points[0].second; for(auto &pos : visited_positions) { @@ -21,6 +47,13 @@ void DebugPrint(std::pair &head, std::pair &tail, y_min = std::min(y_min, pos.second); y_max = std::max(y_max, pos.second); } + for(auto &pos : visited_positions_part2) + { + x_min = std::min(x_min, pos.first); + x_max = std::max(x_max, pos.first); + y_min = std::min(y_min, pos.second); + y_max = std::max(y_max, pos.second); + } for(auto i = 0; i < 50; ++i) std::cout << std::endl; @@ -32,12 +65,30 @@ void DebugPrint(std::pair &head, std::pair &tail, { for(auto x = x_min; x <= x_max; ++x) { - if(head.first == x && head.second == y) + if(points[0].first == x && points[0].second == y) std::cout << "\033[1;30mH"; // head - else if(tail.first == x && tail.second == y) - std::cout << "\033[1;25mT"; // head + else if(points[1].first == x && points[1].second == y) + std::cout << "\033[1;25m1"; // tail + else if(points[2].first == x && points[2].second == y) + std::cout << "\033[1;25m2"; // tail + else if(points[3].first == x && points[3].second == y) + std::cout << "\033[1;25m3"; // tail + else if(points[4].first == x && points[4].second == y) + std::cout << "\033[1;25m4"; // tail + else if(points[5].first == x && points[5].second == y) + std::cout << "\033[1;25m5"; // tail + else if(points[6].first == x && points[6].second == y) + std::cout << "\033[1;25m6"; // tail + else if(points[7].first == x && points[7].second == y) + std::cout << "\033[1;25m7"; // tail + else if(points[8].first == x && points[8].second == y) + std::cout << "\033[1;25m8"; // tail + else if(points[9].first == x && points[9].second == y) + std::cout << "\033[1;25m9"; // tail else if(visited_positions.contains({x, y})) std::cout << "\033[1;31mX"; // red + else if(visited_positions_part2.contains({x, y})) + std::cout << "\033[1;20mY"; // ?? else std::cout << "\033[0m-"; // default white } @@ -59,9 +110,11 @@ int main() unsigned long total_pt2 = 0; std::set > visited_positions; + std::set > visited_positions_part2; - std::pair head = {0,0}; - std::pair tail = {0,0}; + std::vector > points; + for(auto i = 0; i < 10; ++i) + points.push_back({0,0}); char dir; int amt; @@ -80,55 +133,34 @@ int main() switch(dir) { case 'U': - head.second--; + points[0].second--; break; case 'D': - head.second++; + points[0].second++; break; case 'L': - head.first--; + points[0].first--; break; case 'R': - head.first++; + points[0].first++; break; } // Follow with tail: - const int x_diff = head.first - tail.first; - const int y_diff = head.second - tail.second; - if(std::abs(x_diff) > 1) // 2 away - { - tail.first += (head.first - tail.first) / 2; - if(y_diff) - { - int to_change = std::ceil(std::abs(head.second - tail.second) / 2.0); - if(y_diff < 0) - tail.second -= to_change; - else - tail.second += to_change; - } - } - if(std::abs(y_diff) > 1) // 2 away - { - tail.second += (head.second - tail.second) / 2; - if(x_diff) - { - int to_change = std::ceil(std::abs(head.first - tail.first) / 2.0); - if(x_diff < 0) - tail.first -= to_change; - else - tail.first += to_change; - } - } + for(auto i = 0; i < points.size() - 1; ++i) + ApplyMovement(points[i], points[i+1]); - visited_positions.insert(tail); - // DebugPrint(head, tail, visited_positions); - // usleep(1000000); + visited_positions.insert(points[1]); + visited_positions_part2.insert(points.back()); + + DebugPrint(points, visited_positions, visited_positions_part2); + usleep(100000); } } - DebugPrint(head, tail, visited_positions); + DebugPrint(points, visited_positions, visited_positions_part2); total = visited_positions.size(); + total_pt2 = visited_positions_part2.size(); std::cout << " Total: " << total << std::endl; std::cout << "PT2 Total: " << total_pt2 << std::endl;