Skip to content

Instantly share code, notes, and snippets.

@bzar bzar/reaktor_outrun.cpp
Last active Aug 29, 2015

Embed
What would you like to do?
Reaktor fast track outrun C++ solution
// g++ -Ofast -fomit-frame-pointer -funroll-loops -o reaktor_outrun reaktor_outrun.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>
#include <cctype>
#include <cstdio>
int main(int argc, char** argv)
{
std::vector<unsigned int> buffers[2];
std::vector<unsigned int>* curr = &buffers[0];
std::vector<unsigned int>* prev = &buffers[1];
curr->reserve(100);
prev->reserve(100);
unsigned int k = 0;
unsigned int i = 0;
unsigned int v = 0;
char c = '\0';
bool inValue = false;
std::FILE* file = std::fopen(argv[1], "r");
// Ignore first line seed comment
while(!std::feof(file) && std::getc(file) != '\n');
int const BUF_SIZE = 1024 * 1024;
char buf[BUF_SIZE] = {0};
std::size_t bufCount = 1;
std::size_t bufIndex = 0;
while(bufCount != 0)
{
bufCount = std::fread(buf, sizeof(buf[0]), BUF_SIZE, file);
bufIndex = 0;
while(bufIndex < bufCount)
{
c = buf[bufIndex];
++bufIndex;
if(std::isdigit(c))
{
v = v * 10 + (c - '0');
inValue = true;
continue;
}
else if(!inValue)
{
continue;
}
if(i == k)
{
i = 0;
k += 1;
std::vector<unsigned int>* temp = prev;
prev = curr;
curr = temp;
curr->push_back(0);
prev->push_back(0);
}
if(k > 1)
{
if(i == 0)
{
v += prev->at(0);
}
else if(i == k - 1)
{
v += prev->at(k - 2);
}
else
{
v += prev->at(i) > prev->at(i - 1) ? prev->at(i) : prev->at(i - 1);
}
}
curr->at(i) = v;
++i;
v = 0;
inValue = false;
}
}
int max = *std::max_element(curr->begin(), curr->end());
std::cout << max << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.