Click here to Skip to main content
15,919,358 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Coding Challenge - Morris Sequence Pin
Kenneth Haugland2-Dec-17 17:29
mvaKenneth Haugland2-Dec-17 17:29 
GeneralRe: Coding Challenge - Morris Sequence Pin
Dan Sutton2-Dec-17 6:58
Dan Sutton2-Dec-17 6:58 
GeneralRe: Coding Challenge - Morris Sequence Pin
Dave Kreskowiak2-Dec-17 7:57
mveDave Kreskowiak2-Dec-17 7:57 
GeneralRe: Coding Challenge - Morris Sequence Pin
Member 131943022-Dec-17 11:06
Member 131943022-Dec-17 11:06 
GeneralRe: Coding Challenge - Morris Sequence Pin
Dave Kreskowiak4-Dec-17 4:17
mveDave Kreskowiak4-Dec-17 4:17 
GeneralRe: Coding Challenge - Morris Sequence Pin
Member 131943024-Dec-17 4:22
Member 131943024-Dec-17 4:22 
GeneralRe: Coding Challenge - Morris Sequence Pin
Dave Kreskowiak4-Dec-17 6:39
mveDave Kreskowiak4-Dec-17 6:39 
GeneralRe: Coding Challenge - Morris Sequence Pin
Member 131943024-Dec-17 6:51
Member 131943024-Dec-17 6:51 
OK, here it is...

#include <iostream>
#include <chrono>
#include <ctime>

using namespace std;

#define maxLevel 100

static uint32_t currentLevel = 0;
static chrono::time_point<chrono::system_clock> start, timeFinished;

class LevelProcessor
{
public:
LevelProcessor() :
currentOccurrence(0),
currentPrefix(0),
myLevel(currentLevel++),
totalSize(0)
{
}

void ProcessLevel(uint32_t prefix);
void FinishLevel();

uint32_t currentOccurrence;
uint32_t currentPrefix;
const uint32_t myLevel;

uint64_t totalSize;
};

static LevelProcessor processors[maxLevel];

void LevelProcessor::ProcessLevel(uint32_t prefix)
{
if (prefix == currentPrefix)
{
++currentOccurrence;
return;
}

if (currentOccurrence != 0)
{
if (myLevel < maxLevel - 1)
{
processors[myLevel + 1].ProcessLevel(currentOccurrence);
processors[myLevel + 1].ProcessLevel(currentPrefix);
}
++totalSize;
}

currentPrefix = prefix;
currentOccurrence = 1;
}

void LevelProcessor::FinishLevel()
{
++totalSize;

if (myLevel < maxLevel - 1)
{
processors[myLevel + 1].ProcessLevel(currentOccurrence);
processors[myLevel + 1].ProcessLevel(currentPrefix);
}

chrono::time_point<chrono::system_clock> timeFinished = chrono::system_clock::now();
chrono::duration<double> elapsed_seconds = timeFinished - start;
time_t end_time = chrono::system_clock::to_time_t(timeFinished);

cout << "level " << myLevel + 1 << " is done, size = " << totalSize * 2 << " at " << "elapsed time: " << elapsed_seconds.count() << "secs" << endl;

if (myLevel < maxLevel - 1)
processors[myLevel + 1].FinishLevel();
}

int main()
{
start = chrono::system_clock::now();

processors[1].ProcessLevel(1);
processors[1].FinishLevel();

timeFinished = chrono::system_clock::now();
chrono::duration<double> elapsed_seconds = timeFinished - start;
time_t end_time = chrono::system_clock::to_time_t(timeFinished);

cout << "finished computation at " << ctime(&end_time)
<< "elapsed time: " << elapsed_seconds.count() << "secs" << endl;
}


So much for my indenting, oh well.

GeneralRe: Coding Challenge - Morris Sequence Pin
Dave Kreskowiak4-Dec-17 11:25
mveDave Kreskowiak4-Dec-17 11:25 
GeneralRe: Coding Challenge - Morris Sequence Pin
Member 131943025-Dec-17 2:42
Member 131943025-Dec-17 2:42 
GeneralRe: Coding Challenge - Morris Sequence Pin
Dave Kreskowiak5-Dec-17 3:56
mveDave Kreskowiak5-Dec-17 3:56 
GeneralRe: Coding Challenge - Morris Sequence Pin
Dave Kreskowiak5-Dec-17 15:40
mveDave Kreskowiak5-Dec-17 15:40 
GeneralRe: Coding Challenge - Morris Sequence Pin
Member 131943026-Dec-17 3:13
Member 131943026-Dec-17 3:13 
GeneralRe: Coding Challenge - Morris Sequence Pin
Dave Kreskowiak6-Dec-17 4:57
mveDave Kreskowiak6-Dec-17 4:57 
GeneralRe: Coding Challenge - Morris Sequence Pin
Member 131943027-Dec-17 4:22
Member 131943027-Dec-17 4:22 
GeneralRe: Coding Challenge - Morris Sequence Pin
PIEBALDconsult2-Dec-17 12:38
mvePIEBALDconsult2-Dec-17 12:38 
GeneralRe: Coding Challenge - Morris Sequence Pin
Tony Riddiough3-Dec-17 10:57
Tony Riddiough3-Dec-17 10:57 
GeneralRe: Coding Challenge - Morris Sequence Pin
PIEBALDconsult3-Dec-17 12:32
mvePIEBALDconsult3-Dec-17 12:32 
GeneralRe: Coding Challenge - Morris Sequence Pin
Tony Riddiough4-Dec-17 14:36
Tony Riddiough4-Dec-17 14:36 
GeneralRe: Coding Challenge - ANSWER Pin
Dave Kreskowiak4-Dec-17 4:15
mveDave Kreskowiak4-Dec-17 4:15 
GeneralRe: Coding Challenge - ANSWER Pin
PeejayAdams5-Dec-17 22:48
PeejayAdams5-Dec-17 22:48 
GeneralRe: Coding Challenge - ANSWER Pin
Paulo_JCG12-Dec-17 4:59
professionalPaulo_JCG12-Dec-17 4:59 
GeneralRe: Coding Challenge - ANSWER Pin
Dave Kreskowiak12-Dec-17 5:24
mveDave Kreskowiak12-Dec-17 5:24 
GeneralRe: Coding Challenge - Morris Sequence Pin
Paulo_JCG12-Dec-17 4:53
professionalPaulo_JCG12-Dec-17 4:53 
GeneralRe: Coding Challenge - Morris Sequence Pin
Member 1319430215-Mar-18 5:07
Member 1319430215-Mar-18 5:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.