138 lines
2.9 KiB
C++
138 lines
2.9 KiB
C++
#include <cmath>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <set>
|
|
#include <string>
|
|
#include <unistd.h>
|
|
#include <vector>
|
|
|
|
void DebugPrint(std::pair<int, int> &head, std::pair<int, int> &tail,
|
|
std::set<std::pair<int, int> > &visited_positions)
|
|
{
|
|
int x_min = head.first;
|
|
int y_min = head.second;
|
|
int x_max = head.first;
|
|
int y_max = head.second;
|
|
|
|
for(auto &pos : visited_positions)
|
|
{
|
|
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;
|
|
std::cout << "X: " << x_min << " - " << x_max << std::endl;
|
|
std::cout << "Y: " << y_min << " - " << y_max << std::endl;
|
|
std::cout << std::endl;
|
|
|
|
for(auto y = y_min; y <= y_max; ++y)
|
|
{
|
|
for(auto x = x_min; x <= x_max; ++x)
|
|
{
|
|
if(head.first == x && head.second == y)
|
|
std::cout << "\033[1;30mH"; // head
|
|
else if(tail.first == x && tail.second == y)
|
|
std::cout << "\033[1;25mT"; // head
|
|
else if(visited_positions.contains({x, y}))
|
|
std::cout << "\033[1;31mX"; // red
|
|
else
|
|
std::cout << "\033[0m-"; // default white
|
|
}
|
|
std::cout << std::endl;
|
|
}
|
|
std::cout << "\033[0m"; // default white
|
|
}
|
|
|
|
int main()
|
|
{
|
|
std::ifstream ifs("data.txt");
|
|
if(!ifs.is_open())
|
|
{
|
|
std::cerr << "Missing data.txt." << std::endl;
|
|
return -1;
|
|
}
|
|
|
|
unsigned long total = 0;
|
|
unsigned long total_pt2 = 0;
|
|
|
|
std::set<std::pair<int, int> > visited_positions;
|
|
|
|
std::pair<int, int> head = {0,0};
|
|
std::pair<int, int> tail = {0,0};
|
|
|
|
char dir;
|
|
int amt;
|
|
|
|
for(std::string line; std::getline(ifs, line); )
|
|
{
|
|
if(line == "")
|
|
continue;
|
|
|
|
dir = line[0];
|
|
amt = std::atoi(line.substr(2).c_str());
|
|
|
|
for(auto i = 0; i < amt; ++i)
|
|
{
|
|
// Move head:
|
|
switch(dir)
|
|
{
|
|
case 'U':
|
|
head.second--;
|
|
break;
|
|
case 'D':
|
|
head.second++;
|
|
break;
|
|
case 'L':
|
|
head.first--;
|
|
break;
|
|
case 'R':
|
|
head.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;
|
|
}
|
|
}
|
|
|
|
visited_positions.insert(tail);
|
|
// DebugPrint(head, tail, visited_positions);
|
|
// usleep(1000000);
|
|
}
|
|
}
|
|
|
|
DebugPrint(head, tail, visited_positions);
|
|
total = visited_positions.size();
|
|
|
|
std::cout << " Total: " << total << std::endl;
|
|
std::cout << "PT2 Total: " << total_pt2 << std::endl;
|
|
|
|
return 0;
|
|
}
|