-
October 18th, 2006, 09:05 AM
#16
Re: Fun C++ Problem
I have 3 solutions. Took me a few minutes. Didn't actually time it.
-
October 18th, 2006, 09:43 AM
#17
Re: Fun C++ Problem
I have all three.
Regards,
Paul McKenzie
-
October 18th, 2006, 09:57 AM
#18
Re: Fun C++ Problem
So I ask dcjr84, do we bring the solutions in open or do you want some more time?
I can't hold any longer... I will put them all in my next post here ...
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
October 18th, 2006, 10:02 AM
#19
Re: Fun C++ Problem
Still can't get it
Is replacing the same character twice valid?
If there is no love sun won't shine
-
October 18th, 2006, 10:02 AM
#20
Re: Fun C++ Problem
me neither, I only get 2!
-
October 18th, 2006, 10:07 AM
#21
Re: Fun C++ Problem
Originally Posted by laitinen
me neither, I only get 2!
Try to think outside the box.
-
October 18th, 2006, 10:10 AM
#22
Re: Fun C++ Problem
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
-
October 18th, 2006, 10:14 AM
#23
Re: Fun C++ Problem
Now, I think I have three. Not sure unless someone posts the solutions hehe.....
If there is no love sun won't shine
-
October 18th, 2006, 10:34 AM
#24
Re: Fun C++ Problem
++++++++[>++++++++<-]>+.<+++[>++++<-]>+.<++[>-----<-]>.<+++[>++++<-]>++.<+++[>----<-]>-.----.
God does not play dice with the universe.(A.Einstein)
-
October 18th, 2006, 10:38 AM
#25
Re: Fun C++ Problem
++++++++[>++++++++<-]>+.<+++[>++++<-]>+.<++[>-----<-]>.<+++[>++++<-]>++.<+++[>----<-]>-.----.
God does not play dice with the universe.(A.Einstein)
-
October 18th, 2006, 10:48 AM
#26
Re: Fun C++ Problem
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. But then, following that, I almost immediately got the third.
-
October 18th, 2006, 11:04 AM
#27
Re: Fun C++ Problem
Got a 4th solution......
but it's a cheat based on solution 1.........
and works wrong too
++++++++[>++++++++<-]>+.<+++[>++++<-]>+.<++[>-----<-]>.<+++[>++++<-]>++.<+++[>----<-]>-.----.
God does not play dice with the universe.(A.Einstein)
-
October 18th, 2006, 11:59 AM
#28
Re: Fun C++ Problem
Got two in a few minutes. But the third one took me 30 minutes!!!
-
October 18th, 2006, 12:13 PM
#29
Re: Fun C++ Problem
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
Last edited by RoboTact; October 19th, 2006 at 10:45 AM.
Reason: bugfix
"Programs must be written for people to read, and only incidentally for machines to execute."
-
October 18th, 2006, 12:39 PM
#30
Re: Fun C++ Problem
OMG!!!
RoboTact, for how long did you not have a good sleep? It was just a small puzzle..
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|