Skip to content

Commit

Permalink
added day 13 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
dCubelic committed Dec 13, 2020
1 parent 63f45ef commit db8ef21
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
52 changes: 52 additions & 0 deletions 13/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>

std::vector<int> split(std::string & s)
{
std::vector<int> values;

size_t pos = 0;
std::string token;
while ((pos = s.find(',')) != std::string::npos) {
token = s.substr(0, pos);
try
{
values.push_back(std::stoi(token));
}
catch(std::exception &e) { }
s.erase(0, pos + 1);
}
try
{
values.push_back(std::stoi(token));
}
catch(std::exception &e) { }

return values;
}

int main()
{
int t;
std::string input;
scanf("%d\n", &t);
std::getline(std::cin, input);
std::vector<int> values = split(input);

int min = values[0] - t % values[0];
int argmin = 0;
for(auto v: values)
{
if(v - t % v < min)
{
min = v - t % v;
argmin = v;
}
}

std::cout << min*argmin;

return 0;
}
71 changes: 71 additions & 0 deletions 13/2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>

std::vector<std::pair<int, int>> split(std::string & s)
{
std::vector<std::pair<int, int>> values;

size_t pos = 0;
std::string token;
int index = 0;
while ((pos = s.find(',')) != std::string::npos) {
token = s.substr(0, pos);
try
{
values.emplace_back(std::stoi(token), index);
}
catch(std::exception &e) { }
s.erase(0, pos + 1);
index++;
}
try
{
values.emplace_back(std::stoi(s), index);
}
catch(std::exception &e) { }

return values;
}

int main()
{
long long t;
std::string input;
scanf("%lld\n", &t);
std::getline(std::cin, input);
std::vector<std::pair<int, int>> values = split(input);

t = 0;
long long step = values[0].first;
while(true)
{
bool flag = true;
for(auto p: values)
{
if((t + p.second) % p.first != 0) {
flag = false;
break;
}
else
{
if(step % p.first != 0)
{
step *= p.first;
}
}
}
if(flag)
{
std::cout << t;
break;
}
t += step;
}

return 0;
}

2 changes: 2 additions & 0 deletions 13/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1000511
29,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,37,x,x,x,x,x,409,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,17,13,19,x,x,x,23,x,x,x,x,x,x,x,353,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,41

0 comments on commit db8ef21

Please sign in to comment.