Skip to content

Commit

Permalink
Add Moderate difficulty problem. 4.5
Browse files Browse the repository at this point in the history
Annoying thing about this code was that you'd expect the period about which the string returns to it's base form to be along the lines of "string.length()-1" or something. 
However, there's a notable case where string.length() returns a number of the form 2+3*n, where n is a positive integer. I eventually found a solution to these, but then realized that there were other, non-obvious exceptions.
In my rage, I have brute forced finding the period. Yes, I am ashamed.
  • Loading branch information
MarcusCaparini authored Aug 24, 2023
1 parent 81613a7 commit e29ed6f
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions Slom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <iostream>
#include <string>
#include <vector>

using namespace std;
// This code works by finding and storing the "next position" of any locaiton in the string based on the problem
// description. From that, we can find the period over which it returns to the original string.
// Note that we failed to find a nice mathematical solution to this - I just brute forced it.
// Finally, we calculate how many more times the string needs to be put through the formula to return to the
// original string based on the above period and number of iterations.
//
// I despise this question.
int main(void){
long long int blinks;
string k1,k2;
int Length, period;
vector<int> position;

// Take input
cin >> blinks;
cin >> k1;
k2 = k1;
Length = k1.length();


// Store the next positions based on string size.
position.push_back(0);
for(int i = 1; i < Length; i++){
if(i < (Length+1)/2){
position.push_back(2*i);
}
else if(i >= Length/2){
position.push_back(Length - (Length+1)%2 - 2*(i - Length/2));
}
}


// Find the period
int range = 0;
string k3 = k1;
while(1){
for(int b = 0; b < Length; b++){
k2[position[b]] = k1[b];
}
k1 = k2;
range++;
if(k1 == k3){
break;
}
}
period = range;



// put the string through the formula the correct amount of times to return the original string.
for(int a = 0; a < (period - blinks%period); a++){
for(int b = 0; b < Length; b++){
k2[position[b]] = k1[b];
}
k1 = k2;
}




cout << k1 << endl;

return 0;
}




0 comments on commit e29ed6f

Please sign in to comment.