From 719c1e1b69d606f528d1b2e8b8ebe77be1561909 Mon Sep 17 00:00:00 2001 From: Marcus Penate Date: Mon, 19 Dec 2022 10:01:21 -0500 Subject: [PATCH] Day 9 --- Day9/part1.cpp | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ Day9/part2.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 Day9/part1.cpp create mode 100644 Day9/part2.cpp diff --git a/Day9/part1.cpp b/Day9/part1.cpp new file mode 100644 index 0000000..9dbb068 --- /dev/null +++ b/Day9/part1.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include + +using namespace std; + +struct Rope +{ + Rope(pair head, pair tail) : head{head}, tail{tail} {} + pair head, tail; + +}; + +Rope move_rope(Rope rope, int dx, int dy); + +int main(int argc, char* argv[]) +{ + Rope rope({0,0}, {0,0}); + set> visited; + + string line; + while(getline(cin, line)) + { + char dir = line[0]; + int amount = stoi(line.substr(2)); + int dx = (dir=='R')?1:((dir=='L')?-1:0); + int dy = (dir=='U')?1:((dir=='D')?-1:0); + + for(int i = 0; i < amount; i++) + { + rope = move_rope(rope, dx, dy); + visited.insert(rope.tail); + } + } + + cout << visited.size() << endl; +} + +Rope move_rope(Rope rope, int dx, int dy) +{ + rope.head.first += dx; + rope.head.second += dy; + + if (rope.head.first == rope.tail.first) + { //same x + if (rope.head.second > rope.tail.second) + { + rope.tail.second = rope.head.second-1; + } + else if (rope.head.second < rope.tail.second) + { + rope.tail.second = rope.head.second+1; + } + } + else if (rope.head.second == rope.tail.second) + { //same y + if (rope.head.first > rope.tail.first) + { + rope.tail.first = rope.head.first-1; + } + else if (rope.head.first < rope.tail.first) + { + rope.tail.first = rope.head.first+1; + } + } + else + { //diagonal + if (abs(rope.head.first-rope.tail.first) == 1 && abs(rope.head.second-rope.tail.second) == 1) + { + return rope; + } + + if (rope.head.first-rope.tail.first > 0) + { + rope.tail.first++; + } + else + { + rope.tail.first--; + } + + if (rope.head.second-rope.tail.second > 0) + { + rope.tail.second++; + } + else + { + rope.tail.second--; + } + } + + return rope; +} \ No newline at end of file diff --git a/Day9/part2.cpp b/Day9/part2.cpp new file mode 100644 index 0000000..c4b1144 --- /dev/null +++ b/Day9/part2.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include + +using namespace std; + +void move_rope(pair* head, pair* tail); + +int main(int argc, char* argv[]) +{ + vector> rope; + for(int i = 0; i < 10; i++) + { + rope.push_back({0,0}); + } + + set> visited; + + string line; + while(getline(cin, line)) + { + char dir = line[0]; + int amount = stoi(line.substr(2)); + int dx = (dir=='R')?1:((dir=='L')?-1:0); + int dy = (dir=='U')?1:((dir=='D')?-1:0); + + for(int i = 0; i < amount; i++) + { + rope[0].first += dx; + rope[0].second += dy; + + vector>::iterator knot1 = rope.begin(); + vector>::iterator knot2 = rope.begin()+1; + for(int j = 0; j < rope.size(); j++) + { + move_rope(&(*knot1), &(*knot2)); + ++knot1; + ++knot2; + } + visited.insert(rope.back()); + } + } + + cout << visited.size() << endl; +} + +void move_rope(pair* head, pair* tail) +{ + if (head->first == tail->first) + { //same x + if (head->second > tail->second) + { + tail->second = head->second-1; + } + else if (head->second < tail->second) + { + tail->second = head->second+1; + } + } + else if (head->second == tail->second) + { //same y + if (head->first > tail->first) + { + tail->first = head->first-1; + } + else if (head->first < tail->first) + { + tail->first = head->first+1; + } + } + else + { //diagonal + if (abs(head->first-tail->first) == 1 && abs(head->second-tail->second) == 1) + { + return; + } + + if (head->first-tail->first > 0) + { + tail->first++; + } + else + { + tail->first--; + } + + if (head->second-tail->second > 0) + { + tail->second++; + } + else + { + tail->second--; + } + } +} \ No newline at end of file