Did it all on my own, while being incredibly quick at the same time, very proud

This commit is contained in:
2021-11-02 12:55:06 +01:00
parent 7eaf59bb45
commit f649acf5a3

View File

@@ -15,12 +15,7 @@ This is the only set of 4-digit numbers with this property.
Find the sum of the only ordered set of six cyclic 4-digit numbers for which each polygonal type: triangle, square, pentagonal, hexagonal, heptagonal, and octagonal, is represented by a different number in the set. Find the sum of the only ordered set of six cyclic 4-digit numbers for which each polygonal type: triangle, square, pentagonal, hexagonal, heptagonal, and octagonal, is represented by a different number in the set.
*/ */
#include <iostream> #include <bits/stdc++.h>
#include <chrono>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std; using namespace std;
@@ -52,9 +47,12 @@ bool validEnd(uint n){
return n % 100 >= 10; return n % 100 >= 10;
} }
std::set<unsigned int> results; bool matchingCycle(int a, int b){
return (a % 100) == (b / 100);
}
const unsigned int Limit = 10000; const unsigned int Limit = 10000;
std::vector<unsigned int> all(Limit, 0); vector<unsigned int> all(Limit, 0);
uint finalMask = 0b111111000; uint finalMask = 0b111111000;
void fillVector(vector<uint> & toFill, const uint bitmask, uint (*calc)(uint n)){ void fillVector(vector<uint> & toFill, const uint bitmask, uint (*calc)(uint n)){
@@ -68,66 +66,46 @@ void fillVector(vector<uint> & toFill, const uint bitmask, uint (*calc)(uint n))
} }
} }
void search(vector<uint> & sequence, uint mask = 0){ void search(uint& currentMask, vector<int>& results){
unsigned int from = 1000; int backPart = results.back() % 100;
unsigned int to = 10000; bool gottem = false;
if(!sequence.empty()){ for(int i = backPart * 100; i < backPart * 100 + 100; ++i){
auto lowerTwoDigits = sequence.back() % 100; if(all[i] && (currentMask | all[i]) != currentMask){
from = lowerTwoDigits * 100; auto leftOvers = bitset<9>(~currentMask & all[i]);
to = from + 100; int maskCopy = currentMask;
} vector<int> resultsCopy(results);
for(auto next = from; next < to; ++next){ // cout << "Currently looking at base of these numbers: ";
auto categories = all[next]; // for(auto x : results){
if(categories == 0){ // cout << x << " ";
continue; // } cout << '\n';
}
bool isUnique = true; for(int j = 3; j < 9; ++j){
for(auto x : sequence){ if(leftOvers[j]){
if(x == next){ currentMask |= 1 << j;
isUnique = false; results.push_back(i);
break; search(currentMask, results);
}
if(!isUnique){
continue;
}
for(auto j = 3; j <=8; ++j){
auto thisCategory = 1 << j;
if((categories && thisCategory) == 0){
continue;
}
if((mask & thisCategory) != 0){ if(currentMask == finalMask && matchingCycle(results.back(), results.front())){
continue; gottem = true;
} break;
} else{
auto nextMask = mask | thisCategory; currentMask = maskCopy;
if(nextMask == finalMask){ results = resultsCopy;
auto first = sequence.front();
auto lowerTwoDigits = next % 100;
auto upperTwoDigits = first / 100;
if(lowerTwoDigits == upperTwoDigits){
auto sum = next;
for(auto x : sequence){
sum += x;
}
results.insert(sum);
} }
} }
else{ }
sequence.push_back(next); if(gottem){
search(sequence, nextMask); break;
sequence.pop_back();
}
} }
} }
} }
} }
int main(){ int main(){
cout << "Hello this is Patrick" << endl; cout << "Hello this is Patrick" << endl;
@@ -141,13 +119,45 @@ int main(){
fillVector(all, 1 << 7, heptagonal); fillVector(all, 1 << 7, heptagonal);
fillVector(all, 1 << 8, octogonal); fillVector(all, 1 << 8, octogonal);
vector<uint> sequence; // for(uint i = 0; i < all.size(); ++i){
search(sequence); // if(all[i]){
// cout << i << " " << bitset<9>(all[i]) << endl;
// }
// }
for(auto x : results){ bool gottem = false;
cout << x << endl; vector<int> results;
for(uint i = 0; i < all.size(); ++i){
if(all[i]){
// cout << "at " << i << '\n';
results.clear();
results.push_back(i);
auto bs = bitset<9>(all[i]);
for(uint j = 3; j < 9; ++j){
if(bs[j]){
uint mask = 1 << j;
search(mask, results);
if(mask == finalMask && matchingCycle(results.back(), results.front())){
gottem = true;
break;
}
}
}
if(gottem)
break;
}
} }
int sum = 0;
for(auto r : results){
sum += r;
cout << r << " ";
} cout << endl;
cout << "Constituting to sum of: " << sum << endl;
auto duration = chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - start); auto duration = chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - start);
cout << (float)duration.count()/1000 << endl; cout << (float)duration.count()/1000 << endl;
return 0; return 0;