I have 3 solutions. Took me a few minutes. Didn't actually time it.
Printable View
I have 3 solutions. Took me a few minutes. Didn't actually time it.
I have all three.
Regards,
Paul McKenzie
So I ask dcjr84, do we bring the solutions in open or do you want some more time? :D
I can't hold any longer... I will put them all in my next post here ... :D :D :D ;)
Still can't get it :mad:
Is replacing the same character twice valid?
me neither, I only get 2!
Try to think outside the box. ;)Quote:
Originally Posted by laitinen
A hint to the third solution, if it's the one I think some here are having a problem with, is "do the math".
Regards,
Paul McKenzie
Now, I think I have three. Not sure unless someone posts the solutions :D :D hehe.....
got 2.......
got 3 ! !
Got all three!!! Took me a few seconds to get the first one. Then, my mind went completely blank (started thinking too hard for some difficult trick) and it took me almost 20 minutes to get the second:o. But then, following that, I almost immediately got the third:D.
Got a 4th solution......
but it's a cheat based on solution 1.........
and works wrong too :lol:
Got two in a few minutes. But the third one took me 30 minutes!!!
:rolleyes:Code:#include <windows.h>
#include <limits>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool exists(const char* fname)
{
ifstream fin(fname);
return fin.is_open();
}
bool check_out(const char* fname)
{
ifstream out(fname);
for(int i=0;i<20;i++)
{
int c = out.get();
if(c!='x')
return false;
c = out.get();
if(c!='\n')
return false;
}
int end = out.get();
if(!out.eof()) return false;
return true;
}
void run_with_timeout(char* cmdline)
{
PROCESS_INFORMATION info;
ZeroMemory(&info,sizeof(info));
STARTUPINFO si;
ZeroMemory(&si,sizeof(si));
if(!CreateProcess(0,cmdline,0,0,0,DETACHED_PROCESS,0,0,&si,&info))
return;
WaitForSingleObject(info.hProcess,1000);
TerminateProcess(info.hProcess, 1);
}
bool test_code(const char* code)
{
const char* src_name = "test_code.cpp";
ofstream src(src_name);
src<<"#include <fstream>\n";
src<<"#include <iostream>\n";
src<<"using std::endl;\n";
src<<"int main() { \n";
src<<"std::ofstream cout(\"out.txt\");\n";
src<<code;
src<<"}\n";
src.close();
if(exists("a.exe"))
system("del a.exe");
string cmd = string() + "g++ " + src_name + " 2> log.txt";
system(cmd.c_str());
if(!exists("a.exe"))
return false;
run_with_timeout("a.exe");
bool res = check_out("out.txt");
if(exists("out.txt"))
system("del out.txt");
return res;
}
void log_success(ostream& str, const char* code, int i, char c)
{
str<<endl;
str<<c<<" at "<<i<<":"<<endl;
str<<code;
str<<endl;
}
int main()
{
char code[] =
"int i, n = 20;\n"
"for (i=0; i<n; i--)\n"
"{\n"
"cout << \"x\" << endl;\n"
"}\n";
ofstream success_log("success_log.txt");
for(int i=0, n=strlen(code);i<n;i++)
{
cout<<i<<" of "<<n<<"..."<<endl;
for(char c = 32;c<127;c++)
{
cout<<c;
char old = code[i];
code[i] = c;
if(test_code(code))
{
log_success(success_log, code, i, c);
log_success(cout, code, i, c);
}
code[i] = old;
}
cout<<endl;
}
return 0;
}
Edit: v0.02 <- bugfix
OMG!!! :eek:
RoboTact, for how long did you not have a good sleep? It was just a small puzzle.. :eek: :D