Did it all on my own, while being incredibly quick at the same time, very proud
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -52,9 +47,12 @@ bool validEnd(uint n){
|
||||
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;
|
||||
std::vector<unsigned int> all(Limit, 0);
|
||||
vector<unsigned int> all(Limit, 0);
|
||||
uint finalMask = 0b111111000;
|
||||
|
||||
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){
|
||||
unsigned int from = 1000;
|
||||
unsigned int to = 10000;
|
||||
void search(uint& currentMask, vector<int>& results){
|
||||
int backPart = results.back() % 100;
|
||||
bool gottem = false;
|
||||
|
||||
if(!sequence.empty()){
|
||||
auto lowerTwoDigits = sequence.back() % 100;
|
||||
from = lowerTwoDigits * 100;
|
||||
to = from + 100;
|
||||
for(int i = backPart * 100; i < backPart * 100 + 100; ++i){
|
||||
if(all[i] && (currentMask | all[i]) != currentMask){
|
||||
auto leftOvers = bitset<9>(~currentMask & all[i]);
|
||||
int maskCopy = currentMask;
|
||||
vector<int> resultsCopy(results);
|
||||
|
||||
// cout << "Currently looking at base of these numbers: ";
|
||||
// for(auto x : results){
|
||||
// cout << x << " ";
|
||||
// } cout << '\n';
|
||||
|
||||
for(int j = 3; j < 9; ++j){
|
||||
if(leftOvers[j]){
|
||||
currentMask |= 1 << j;
|
||||
results.push_back(i);
|
||||
search(currentMask, results);
|
||||
|
||||
if(currentMask == finalMask && matchingCycle(results.back(), results.front())){
|
||||
gottem = true;
|
||||
break;
|
||||
} else{
|
||||
currentMask = maskCopy;
|
||||
results = resultsCopy;
|
||||
}
|
||||
|
||||
for(auto next = from; next < to; ++next){
|
||||
auto categories = all[next];
|
||||
if(categories == 0){
|
||||
continue;
|
||||
}
|
||||
|
||||
bool isUnique = true;
|
||||
for(auto x : sequence){
|
||||
if(x == next){
|
||||
isUnique = false;
|
||||
}
|
||||
if(gottem){
|
||||
break;
|
||||
}
|
||||
if(!isUnique){
|
||||
continue;
|
||||
}
|
||||
for(auto j = 3; j <=8; ++j){
|
||||
auto thisCategory = 1 << j;
|
||||
if((categories && thisCategory) == 0){
|
||||
continue;
|
||||
}
|
||||
|
||||
if((mask & thisCategory) != 0){
|
||||
continue;
|
||||
}
|
||||
|
||||
auto nextMask = mask | thisCategory;
|
||||
if(nextMask == finalMask){
|
||||
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);
|
||||
search(sequence, nextMask);
|
||||
sequence.pop_back();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
cout << "Hello this is Patrick" << endl;
|
||||
@@ -141,12 +119,44 @@ int main(){
|
||||
fillVector(all, 1 << 7, heptagonal);
|
||||
fillVector(all, 1 << 8, octogonal);
|
||||
|
||||
vector<uint> sequence;
|
||||
search(sequence);
|
||||
// for(uint i = 0; i < all.size(); ++i){
|
||||
// if(all[i]){
|
||||
// cout << i << " " << bitset<9>(all[i]) << endl;
|
||||
// }
|
||||
// }
|
||||
|
||||
for(auto x : results){
|
||||
cout << x << endl;
|
||||
bool gottem = false;
|
||||
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);
|
||||
cout << (float)duration.count()/1000 << endl;
|
||||
|
||||
Reference in New Issue
Block a user