Added part 2.

This commit is contained in:
David Vereb 2022-12-17 14:51:01 -05:00
parent 68552d259d
commit ad3e9e42bb

View File

@ -6,13 +6,39 @@
#include <unistd.h> #include <unistd.h>
#include <vector> #include <vector>
void DebugPrint(std::pair<int, int> &head, std::pair<int, int> &tail, void ApplyMovement(std::pair<int, int> &head,
std::set<std::pair<int, int> > &visited_positions) std::pair<int, int> &tail)
{ {
int x_min = head.first; const int x_diff = head.first - tail.first;
int y_min = head.second; const int y_diff = head.second - tail.second;
int x_max = head.first; if(std::abs(x_diff) > 1) // 2 away
int y_max = head.second; {
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<std::pair<int, int> > &points,
std::set<std::pair<int, int> > &visited_positions,
std::set<std::pair<int, int> > &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) for(auto &pos : visited_positions)
{ {
@ -21,6 +47,13 @@ void DebugPrint(std::pair<int, int> &head, std::pair<int, int> &tail,
y_min = std::min(y_min, pos.second); y_min = std::min(y_min, pos.second);
y_max = std::max(y_max, 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) for(auto i = 0; i < 50; ++i)
std::cout << std::endl; std::cout << std::endl;
@ -32,12 +65,30 @@ void DebugPrint(std::pair<int, int> &head, std::pair<int, int> &tail,
{ {
for(auto x = x_min; x <= x_max; ++x) 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 std::cout << "\033[1;30mH"; // head
else if(tail.first == x && tail.second == y) else if(points[1].first == x && points[1].second == y)
std::cout << "\033[1;25mT"; // head 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})) else if(visited_positions.contains({x, y}))
std::cout << "\033[1;31mX"; // red std::cout << "\033[1;31mX"; // red
else if(visited_positions_part2.contains({x, y}))
std::cout << "\033[1;20mY"; // ??
else else
std::cout << "\033[0m-"; // default white std::cout << "\033[0m-"; // default white
} }
@ -59,9 +110,11 @@ int main()
unsigned long total_pt2 = 0; unsigned long total_pt2 = 0;
std::set<std::pair<int, int> > visited_positions; std::set<std::pair<int, int> > visited_positions;
std::set<std::pair<int, int> > visited_positions_part2;
std::pair<int, int> head = {0,0}; std::vector<std::pair<int, int> > points;
std::pair<int, int> tail = {0,0}; for(auto i = 0; i < 10; ++i)
points.push_back({0,0});
char dir; char dir;
int amt; int amt;
@ -80,55 +133,34 @@ int main()
switch(dir) switch(dir)
{ {
case 'U': case 'U':
head.second--; points[0].second--;
break; break;
case 'D': case 'D':
head.second++; points[0].second++;
break; break;
case 'L': case 'L':
head.first--; points[0].first--;
break; break;
case 'R': case 'R':
head.first++; points[0].first++;
break; break;
} }
// Follow with tail: // Follow with tail:
const int x_diff = head.first - tail.first; for(auto i = 0; i < points.size() - 1; ++i)
const int y_diff = head.second - tail.second; ApplyMovement(points[i], points[i+1]);
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;
}
}
visited_positions.insert(tail); visited_positions.insert(points[1]);
// DebugPrint(head, tail, visited_positions); visited_positions_part2.insert(points.back());
// usleep(1000000);
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 = visited_positions.size();
total_pt2 = visited_positions_part2.size();
std::cout << " Total: " << total << std::endl; std::cout << " Total: " << total << std::endl;
std::cout << "PT2 Total: " << total_pt2 << std::endl; std::cout << "PT2 Total: " << total_pt2 << std::endl;