Last active
September 23, 2015 14:49
-
-
Save kahless62003/4c8ff5bcca42c13fde17 to your computer and use it in GitHub Desktop.
Daily Programmer Challenge #233 [Easy] The house that ASCII built
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <time.h> | |
/*Processes the input file*/ | |
int process_file(void) | |
{ | |
int number_of_lines_to_process = 0, lc0, lc1, lc2; | |
int maxlinelength=30; | |
char buffer[maxlinelength+1]; | |
if(fgets(buffer, maxlinelength, stdin) != NULL) | |
{ | |
if(buffer[strlen(buffer)-1]=='\n') | |
buffer[strlen(buffer)-1]='\0'; | |
if(sscanf(buffer, "%i", &number_of_lines_to_process)!=1) | |
return 1; | |
char plan[number_of_lines_to_process][maxlinelength+1]; | |
for(lc0 = 0; lc0 < number_of_lines_to_process; lc0++) | |
{ | |
if(fgets(buffer, maxlinelength, stdin) != NULL) | |
{ | |
if(buffer[strlen(buffer)-1]=='\n') | |
buffer[strlen(buffer)-1]='\0'; | |
if (sscanf(buffer, "%s", plan[lc0])==1) | |
{ | |
for(lc1 = 0; lc1 < strlen(buffer); lc1++) | |
{ | |
if(buffer[lc1]==' ') | |
{ | |
plan[lc0][lc1]=' '; | |
} | |
if(buffer[lc1]=='*') | |
{ | |
plan[lc0][lc1]='*'; | |
} | |
plan[lc0][lc1+1]='\0'; | |
} | |
} | |
} | |
} | |
char plan_inverted[number_of_lines_to_process][maxlinelength+1]; | |
for(lc0 = 0; lc0 < number_of_lines_to_process; lc0++) | |
{ | |
strcpy(plan_inverted[lc0], plan[number_of_lines_to_process-1-lc0]); | |
} | |
int width_of_base = (int)strlen(plan[number_of_lines_to_process-1]); | |
int no_of_design_rows = ( (number_of_lines_to_process * 2) +1 ) + (width_of_base * 2); | |
int no_of_design_columns = (width_of_base * 4)+1+1;//one extra for '\0' terminator | |
char design[no_of_design_rows][no_of_design_columns]; | |
for(lc0 = 0; lc0 < no_of_design_rows; lc0++) | |
{ | |
for(lc1 = 0; lc1 < no_of_design_columns-1; lc1++) | |
{ | |
design[lc0][lc1]=' '; | |
design[lc0][lc1+1]='\0'; | |
} | |
} | |
for(lc0 = 0; lc0 < number_of_lines_to_process; lc0++) | |
{ | |
for(lc1 = 0; lc1 < strlen(plan_inverted[lc0]); lc1++) | |
{ | |
if(plan_inverted[lc0][lc1]=='*') | |
{ | |
//nothing left and above and right but something below | |
if( | |
plan_inverted[lc0][lc1-1] != '*' && | |
plan_inverted[lc0+1][lc1] != '*' && | |
plan_inverted[lc0-1][lc1] == '*' && | |
plan_inverted[lc0][lc1+1] != '*' | |
) | |
{ | |
design[lc0*2+0][lc1*4+0] = '+'; | |
design[lc0*2+1][lc1*4+0] = '|'; | |
design[lc0*2+2][lc1*4+0] = '+'; | |
design[lc0*2+2][lc1*4+1] = '-'; | |
design[lc0*2+2][lc1*4+2] = '-'; | |
design[lc0*2+2][lc1*4+3] = '-'; | |
design[lc0*2+0][lc1*4+4] = '+'; | |
design[lc0*2+1][lc1*4+4] = '|'; | |
design[lc0*2+2][lc1*4+4] = '+'; | |
if(lc0==0) | |
{ | |
design[lc0*2+0][lc1*4+1] = '-'; | |
design[lc0*2+0][lc1*4+2] = '-'; | |
design[lc0*2+0][lc1*4+3] = '-'; | |
design[lc0*2+0][lc1*4+4] = '-'; | |
} | |
} | |
//nothing left and above and right or below | |
if( | |
plan_inverted[lc0][lc1-1] != '*' && | |
plan_inverted[lc0+1][lc1] != '*' && | |
plan_inverted[lc0-1][lc1] != '*' && | |
plan_inverted[lc0][lc1+1] != '*' | |
) | |
{ | |
design[lc0*2+0][lc1*4+0] = '+'; | |
design[lc0*2+1][lc1*4+0] = '|'; | |
design[lc0*2+2][lc1*4+0] = '+'; | |
design[lc0*2+2][lc1*4+1] = '-'; | |
design[lc0*2+2][lc1*4+2] = '-'; | |
design[lc0*2+2][lc1*4+3] = '-'; | |
design[lc0*2+0][lc1*4+4] = '+'; | |
design[lc0*2+1][lc1*4+4] = '|'; | |
design[lc0*2+2][lc1*4+4] = '+'; | |
if(lc0==0) | |
{ | |
design[lc0*2+0][lc1*4+1] = '-'; | |
design[lc0*2+0][lc1*4+2] = '-'; | |
design[lc0*2+0][lc1*4+3] = '-'; | |
design[lc0*2+0][lc1*4+4] = '-'; | |
} | |
} | |
//nothing left and above, but something right and below | |
else if( | |
plan_inverted[lc0][lc1-1] != '*' && | |
plan_inverted[lc0+1][lc1] != '*' && | |
plan_inverted[lc0-1][lc1] == '*' && | |
plan_inverted[lc0][lc1+1] == '*' | |
) | |
{ | |
if(plan_inverted[lc0-1][lc1-1] != '*') | |
design[lc0*2+0][lc1*4+0] = '|'; | |
else | |
design[lc0*2+0][lc1*4+0] = '+'; | |
design[lc0*2+1][lc1*4+0] = '|'; | |
design[lc0*2+2][lc1*4+0] = '+'; | |
design[lc0*2+2][lc1*4+1] = '-'; | |
design[lc0*2+2][lc1*4+2] = '-'; | |
design[lc0*2+2][lc1*4+3] = '-'; | |
design[lc0*2+2][lc1*4+4] = '-'; | |
if(lc0==0) | |
{ | |
design[lc0*2+0][lc1*4+1] = '-'; | |
design[lc0*2+0][lc1*4+2] = '-'; | |
design[lc0*2+0][lc1*4+3] = '-'; | |
design[lc0*2+0][lc1*4+4] = '-'; | |
} | |
} | |
//nothing left and above and below but something right | |
else if( | |
plan_inverted[lc0][lc1-1] != '*' && | |
plan_inverted[lc0+1][lc1] != '*' && | |
plan_inverted[lc0-1][lc1] != '*' && | |
plan_inverted[lc0][lc1+1] == '*' | |
) | |
{ | |
design[lc0*2+0][lc1*4+0] = '+'; | |
design[lc0*2+1][lc1*4+0] = '|'; | |
design[lc0*2+2][lc1*4+0] = '+'; | |
design[lc0*2+2][lc1*4+1] = '-'; | |
design[lc0*2+2][lc1*4+2] = '-'; | |
design[lc0*2+2][lc1*4+3] = '-'; | |
design[lc0*2+2][lc1*4+4] = '-'; | |
if(lc0==0) | |
{ | |
design[lc0*2+0][lc1*4+1] = '-'; | |
design[lc0*2+0][lc1*4+2] = '-'; | |
design[lc0*2+0][lc1*4+3] = '-'; | |
design[lc0*2+0][lc1*4+4] = '-'; | |
} | |
} | |
//nothing left but something above right and below | |
else if( | |
plan_inverted[lc0][lc1-1] != '*' && | |
plan_inverted[lc0+1][lc1] == '*' && | |
plan_inverted[lc0-1][lc1] == '*' && | |
plan_inverted[lc0][lc1+1] == '*' | |
) | |
{ | |
if(plan_inverted[lc0-1][lc1-1] == '*') | |
design[lc0*2+0][lc1*4+0] = '+'; | |
else | |
design[lc0*2+0][lc1*4+0] = '|'; | |
design[lc0*2+1][lc1*4+0] = '|'; | |
design[lc0*2+2][lc1*4+0] = '|'; | |
if(lc0==0) | |
{ | |
design[lc0*2+0][lc1*4+1] = '-'; | |
design[lc0*2+0][lc1*4+2] = '-'; | |
design[lc0*2+0][lc1*4+3] = '-'; | |
design[lc0*2+0][lc1*4+4] = '-'; | |
} | |
} | |
//nothing left and below but something above right | |
else if( | |
plan_inverted[lc0][lc1-1] != '*' && | |
plan_inverted[lc0+1][lc1] == '*' && | |
plan_inverted[lc0-1][lc1] != '*' && | |
plan_inverted[lc0][lc1+1] == '*' | |
) | |
{ | |
design[lc0*2+0][lc1*4+0] = '+'; | |
design[lc0*2+1][lc1*4+0] = '|'; | |
design[lc0*2+2][lc1*4+0] = '|'; | |
if(lc0==0) | |
{ | |
design[lc0*2+0][lc1*4+1] = '-'; | |
design[lc0*2+0][lc1*4+2] = '-'; | |
design[lc0*2+0][lc1*4+3] = '-'; | |
design[lc0*2+0][lc1*4+4] = '-'; | |
} | |
} | |
//nothing right but something above and left and below | |
else if( | |
plan_inverted[lc0][lc1-1] == '*' && | |
plan_inverted[lc0+1][lc1] == '*' && | |
plan_inverted[lc0-1][lc1] == '*' && | |
plan_inverted[lc0][lc1+1] != '*' | |
) | |
{ | |
if(plan_inverted[lc0-1][lc1+1] != '*') | |
design[lc0*2+0][lc1*4+4] = '|'; | |
else | |
design[lc0*2+0][lc1*4+4] = '+'; | |
design[lc0*2+1][lc1*4+4] = '|'; | |
design[lc0*2+2][lc1*4+4] = '|'; | |
if(lc0==0) | |
{ | |
design[lc0*2+0][lc1*4+1] = '-'; | |
design[lc0*2+0][lc1*4+2] = '-'; | |
design[lc0*2+0][lc1*4+3] = '-'; | |
design[lc0*2+0][lc1*4+4] = '-'; | |
} | |
} | |
//nothing right and below but something above and left | |
else if( | |
plan_inverted[lc0][lc1-1] == '*' && | |
plan_inverted[lc0+1][lc1] == '*' && | |
plan_inverted[lc0-1][lc1] != '*' && | |
plan_inverted[lc0][lc1+1] != '*' | |
) | |
{ | |
design[lc0*2+0][lc1*4+4] = '+'; | |
design[lc0*2+1][lc1*4+4] = '|'; | |
design[lc0*2+2][lc1*4+4] = '|'; | |
if(lc0==0) | |
{ | |
design[lc0*2+0][lc1*4+1] = '-'; | |
design[lc0*2+0][lc1*4+2] = '-'; | |
design[lc0*2+0][lc1*4+3] = '-'; | |
} | |
} | |
//something left and right, but not above and below | |
else if( | |
plan_inverted[lc0][lc1-1] == '*' && | |
plan_inverted[lc0+1][lc1] != '*' && | |
plan_inverted[lc0-1][lc1] != '*' && | |
plan_inverted[lc0][lc1+1] == '*' | |
) | |
{ | |
design[lc0*2+2][lc1*4+0] = '-'; | |
design[lc0*2+2][lc1*4+1] = '-'; | |
design[lc0*2+2][lc1*4+2] = '-'; | |
design[lc0*2+2][lc1*4+3] = '-'; | |
design[lc0*2+2][lc1*4+4] = '-'; | |
if(lc0==0) | |
{ | |
design[lc0*2+0][lc1*4+1] = '-'; | |
design[lc0*2+0][lc1*4+2] = '-'; | |
design[lc0*2+0][lc1*4+3] = '-'; | |
design[lc0*2+0][lc1*4+4] = '-'; | |
} | |
} | |
//something left, right, and above and below | |
else if( | |
plan_inverted[lc0][lc1-1] == '*' && | |
plan_inverted[lc0+1][lc1] == '*' && | |
plan_inverted[lc0-1][lc1] == '*' && | |
plan_inverted[lc0][lc1+1] == '*' | |
) | |
{ | |
if(lc0==0) | |
{ | |
design[lc0*2+0][lc1*4+1] = '-'; | |
design[lc0*2+0][lc1*4+2] = '-'; | |
design[lc0*2+0][lc1*4+3] = '-'; | |
design[lc0*2+0][lc1*4+4] = '-'; | |
} | |
} | |
//something left, right, and below but nothing above | |
else if( | |
plan_inverted[lc0][lc1-1] == '*' && | |
plan_inverted[lc0+1][lc1] != '*' && | |
plan_inverted[lc0-1][lc1] == '*' && | |
plan_inverted[lc0][lc1+1] == '*' | |
) | |
{ | |
design[lc0*2+2][lc1*4+1] = '-'; | |
design[lc0*2+2][lc1*4+2] = '-'; | |
design[lc0*2+2][lc1*4+3] = '-'; | |
design[lc0*2+2][lc1*4+4] = '-'; | |
if(lc0==0) | |
{ | |
design[lc0*2+0][lc1*4+1] = '-'; | |
design[lc0*2+0][lc1*4+2] = '-'; | |
design[lc0*2+0][lc1*4+3] = '-'; | |
design[lc0*2+0][lc1*4+4] = '-'; | |
} | |
} | |
//something left, right, and above but nothing below | |
else if( | |
plan_inverted[lc0][lc1-1] == '*' && | |
plan_inverted[lc0+1][lc1] == '*' && | |
plan_inverted[lc0-1][lc1] != '*' && | |
plan_inverted[lc0][lc1+1] == '*' | |
) | |
{ | |
if(lc0==0) | |
{ | |
design[lc0*2+0][lc1*4+1] = '-'; | |
design[lc0*2+0][lc1*4+2] = '-'; | |
design[lc0*2+0][lc1*4+3] = '-'; | |
design[lc0*2+0][lc1*4+4] = '-'; | |
} | |
} | |
//something left, nothing right and above and below | |
else if( | |
plan_inverted[lc0][lc1-1] == '*' && | |
plan_inverted[lc0+1][lc1] != '*' && | |
plan_inverted[lc0-1][lc1] != '*' && | |
plan_inverted[lc0][lc1+1] != '*' | |
) | |
{ | |
design[lc0*2+2][lc1*4+0] = '-'; | |
design[lc0*2+2][lc1*4+1] = '-'; | |
design[lc0*2+2][lc1*4+2] = '-'; | |
design[lc0*2+2][lc1*4+3] = '-'; | |
design[lc0*2+1][lc1*4+4] = '|'; | |
design[lc0*2+2][lc1*4+4] = '+'; | |
if(lc0==0) | |
{ | |
design[lc0*2+0][lc1*4+1] = '-'; | |
design[lc0*2+0][lc1*4+2] = '-'; | |
design[lc0*2+0][lc1*4+3] = '-'; | |
design[lc0*2+0][lc1*4+4] = '+'; | |
} | |
} | |
//something left and below, nothing right and above, | |
else if( | |
plan_inverted[lc0][lc1-1] == '*' && | |
plan_inverted[lc0+1][lc1] != '*' && | |
plan_inverted[lc0-1][lc1] == '*' && | |
plan_inverted[lc0][lc1+1] != '*' | |
) | |
{ | |
if(plan_inverted[lc0-1][lc1+1] != '*') | |
design[lc0*2+0][lc1*4+4] = '|'; | |
else | |
design[lc0*2+0][lc1*4+4] = '+'; | |
design[lc0*2+2][lc1*4+0] = '-'; | |
design[lc0*2+2][lc1*4+1] = '-'; | |
design[lc0*2+2][lc1*4+2] = '-'; | |
design[lc0*2+2][lc1*4+3] = '-'; | |
design[lc0*2+1][lc1*4+4] = '|'; | |
design[lc0*2+2][lc1*4+4] = '+'; | |
if(lc0==0) | |
{ | |
design[lc0*2+0][lc1*4+1] = '-'; | |
design[lc0*2+0][lc1*4+2] = '-'; | |
design[lc0*2+0][lc1*4+3] = '-'; | |
design[lc0*2+0][lc1*4+4] = '+'; | |
} | |
} | |
} | |
if(plan_inverted[lc0][lc1]==' ') | |
{ | |
;//do nothing as it's all intialised to ' ' anyway | |
} | |
} | |
} | |
//need a door on lowest level | |
int tallest_1=0, count_peaks=0; | |
for(lc1 = 0; lc1 < strlen(plan_inverted[0]); lc1++) | |
{ | |
if(plan_inverted[number_of_lines_to_process-1][lc1]=='*') | |
{ | |
tallest_1=lc1; | |
count_peaks++; | |
} | |
} | |
if (count_peaks==1) | |
{ | |
design[0*2+1][tallest_1*4+1]='|'; | |
design[0*2+1][tallest_1*4+3]='|'; | |
} | |
else | |
{ | |
tallest_1=width_of_base/2; | |
design[0*2+1][tallest_1*4+1]='|'; | |
design[0*2+1][tallest_1*4+3]='|'; | |
} | |
//need windows | |
int wall_counter=0, start_wall=0, random_window; | |
char after_door='n'; | |
srand(time(NULL)); | |
for(lc0 = 0; lc0 < no_of_design_rows; lc0++) | |
{ | |
for(lc1 = 0; lc1 < no_of_design_columns; lc1++) | |
{ | |
if( (design[lc0][lc1] == '|') && wall_counter == 0) | |
{ | |
wall_counter++; | |
start_wall=lc1; | |
lc1++; | |
do | |
{ | |
wall_counter++; | |
lc1++; | |
} while (design[lc0][lc1] != '|' && design[lc0][lc1] != '+'); | |
wall_counter--; | |
if(wall_counter > 2 && lc0 == 1) | |
{ | |
if(after_door=='n') | |
{ | |
for(lc2=2; lc2 < wall_counter-1; lc2=lc2+4) | |
{ | |
random_window = rand() % 2; | |
if(random_window==1) | |
{ | |
design[lc0][start_wall + lc2]='o'; | |
} | |
} | |
after_door='y'; | |
} | |
else | |
{ | |
for(lc2=3; lc2 < wall_counter; lc2=lc2+4) | |
{ | |
random_window = rand() % 2; | |
if(random_window==1) | |
{ | |
design[lc0][start_wall + lc2]='o'; | |
} | |
} | |
} | |
} | |
if(wall_counter > 2 && lc0 != 1) | |
{ | |
for(lc2=2; lc2 < wall_counter; lc2=lc2+4) | |
{ | |
random_window = rand() % 2; | |
if(random_window==1) | |
{ | |
design[lc0][start_wall + lc2]='o'; | |
} | |
} | |
} | |
lc1++; | |
wall_counter=0; | |
start_wall=0; | |
after_door=0; | |
} | |
} | |
} | |
//need roofs | |
int roof_counter=0, start_roof=0, end_roof=0; | |
for(lc0 = 1; lc0 < no_of_design_rows; lc0++) | |
{ | |
for(lc1 = 0; lc1 < no_of_design_columns; lc1++) | |
{ | |
if( design[lc0][lc1] == '+' && roof_counter == 0) | |
{ | |
roof_counter++; | |
start_roof=lc1; | |
lc1++; | |
while (design[lc0][lc1] == '-') | |
{ | |
roof_counter++; | |
lc1++; | |
} | |
end_roof=lc1; | |
for(lc2=1; lc2 <= roof_counter/2; lc2++) | |
{ | |
design[lc0+lc2][start_roof+lc2]='/'; | |
design[lc0+lc2][end_roof-lc2]='\\'; | |
if(design[lc0+lc2][start_roof+lc2]=='/' && design[lc0+lc2][start_roof+lc2+2]=='\\') | |
{ | |
design[lc0+lc2+1][start_roof+lc2+1]='A'; | |
break; | |
} | |
} | |
roof_counter=0; | |
start_roof=0; | |
end_roof=0; | |
} | |
} | |
} | |
//print final plan | |
for(lc0 = no_of_design_rows-1; lc0 >= 0; lc0--) | |
{ | |
if( strchr(design[lc0], 'A')!=NULL || | |
strchr(design[lc0], '-')!=NULL || | |
strchr(design[lc0], '|')!=NULL || | |
strchr(design[lc0], '+')!=NULL || | |
strchr(design[lc0], '/')!=NULL | |
) | |
{ | |
printf("[%s]\n", design[lc0]); | |
} | |
} | |
} | |
return 0; | |
} | |
int main(void) | |
{ | |
process_file(); | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[******@localhost HouseThatASCIIBuilt]$ ./asciihouse < sample_input1.txt | |
[ A ] | |
[ / \ ] | |
[ A A +---+ A ] | |
[ / \ / \| o |/ \ ] | |
[ / \ +---+ +---+ A ] | |
[ / \| o |/ \ ] | |
[+-------+ +---+] | |
[| o | | o |] | |
[+-----------------------+] | |
[******@localhost HouseThatASCIIBuilt]$ ./asciihouse < sample_input2.txt | |
[ A ] | |
[ / \ ] | |
[ A +---+ A ] | |
[ / \| |/ \ ] | |
[+---+ +---+] | |
[| o o o |] | |
[| o o o |] | |
[| o o |] | |
[| o |] | |
[| o |] | |
[| o |] | |
[| o |] | |
[| o |] | |
[| o o |] | |
[| o o |] | |
[| | | o |] | |
[+-----------+] | |
[******@localhost HouseThatASCIIBuilt]$ ./asciihouse < challenge_input1.txt | |
[ A A ] | |
[ / \ / \ ] | |
[ / \ / \ ] | |
[ / \ / \ ] | |
[ / \ +-------+] | |
[ / \ | o |] | |
[+-----------+ A | |] | |
[| o |/ \| o |] | |
[| +---+ |] | |
[| o o | | o |] | |
[+-----------------------+] | |
[******@localhost HouseThatASCIIBuilt]$ ./asciihouse < challenge_input2.txt | |
[ A A ] | |
[ / \ / \ ] | |
[ / \ A / \ ] | |
[ / \ / \ / \ ] | |
[ / \ A / \ A / \ ] | |
[ / \ / \ / \ / \ / \ ] | |
[+-----------+ A / \ A / \ A / \ A +-----------+] | |
[| o | / \ / \ / \ / \ / \ / \ / \ | |] | |
[| o o | / \ +-------+ / \ +-----------+ / \ +-------+ / \ | o o |] | |
[| o o | / \| |/ \| |/ \| o |/ \ | o o |] | |
[| o o o | A +-------+ +-------+ +-------+ +-------+ | o o |] | |
[| o o | / \ | o o o o o | | o |] | |
[| | / \ | o o o o | A | o |] | |
[| o | / \ | o o o o o o o o o | / \ | o o o |] | |
[| o o | / \ | o o o o o o o o | / \ | o |] | |
[| o |/ \| o o o o o o o o |/ \| o |] | |
[| o +-----------+ +-------+ |] | |
[| o o o o o o o o o o o o o o |] | |
[| o o o o o o o o o o o o o o |] | |
[| o o o o o | | o o o o o o o |] | |
[+-------------------------------------------------------------------------------------------------------+] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment