68 lines
1.0 KiB
C++
68 lines
1.0 KiB
C++
#include <fstream>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
int main()
|
|
{
|
|
std::vector<std::string> input;
|
|
|
|
std::ifstream ifs("data.txt");
|
|
if(!ifs.is_open())
|
|
{
|
|
std::cerr << "Missing data.txt." << std::endl;
|
|
return -1;
|
|
}
|
|
|
|
std::string line;
|
|
while(!ifs.eof())
|
|
{
|
|
ifs >> line;
|
|
if(ifs.eof())
|
|
break;
|
|
|
|
input.push_back(line);
|
|
}
|
|
|
|
int answer = 0;
|
|
int num = 50;
|
|
for(const std::string &line : input)
|
|
{
|
|
bool to_right = ('R' == line[0]);
|
|
int amount = std::atoi(line.substr(1).c_str());
|
|
|
|
if(!to_right)
|
|
amount *= -1;
|
|
|
|
num += amount;
|
|
num %= 100;
|
|
if(num == 0)
|
|
++answer;
|
|
}
|
|
|
|
std::cout << "Part 1: " << answer << std::endl;
|
|
|
|
answer = 0;
|
|
num = 50;
|
|
for(const std::string &line : input)
|
|
{
|
|
bool to_right = ('R' == line[0]);
|
|
int amount = std::atoi(line.substr(1).c_str());
|
|
|
|
// slow, but accurate. :)
|
|
for(auto i = 0; i < amount; ++i)
|
|
{
|
|
num += (to_right ? 1 : -1);
|
|
num %= 100;
|
|
if(num < 0)
|
|
num += 100;
|
|
if(num == 0)
|
|
++answer;
|
|
}
|
|
}
|
|
|
|
std::cout << "Part 2: " << answer << std::endl;
|
|
|
|
return 0;
|
|
}
|