Skip to content

Instantly share code, notes, and snippets.

@michaelrepper
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelrepper/6303ebab868e0a20c729 to your computer and use it in GitHub Desktop.
Save michaelrepper/6303ebab868e0a20c729 to your computer and use it in GitHub Desktop.
r/Dailyprogrammer Challenge #200 Easy
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' r/DailyProgrammer Challenge #200 Easy '
' coded in QuickBasic by Michael Repper '
' query [ a * t ] gridoodle [ d * o * t ] com '
' '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
CALL process_img(37, 22, 8, 12, "@")
CALL process_img(16, 15, 2, 2, "@")
CALL process_img(9, 9, 8, 3, ",")
FUNCTION pop$ (array$())
max = UBOUND(array$)
IF max = 0 THEN RETURN
last$ = array$(max)
CALL resize_array(array$(), max - 1)
pop = last$
END FUNCTION
SUB push (array$(), push_string$)
max = UBOUND(array$)
CALL resize_array(array$(), max + 1)
array$(max + 1) = push_string$
END SUB
SUB resize_array (array$(), new_size)
REDIM copy_size AS INTEGER
current_size = UBOUND(array$)
IF new_size > current_size THEN
copy_size = current_size
ELSEIF new_size < current_size THEN
copy_size = new_size
END IF
REDIM temp_array$(copy_size)
FOR i = 1 TO copy_size
temp_array$(i) = array$(i)
NEXT i
REDIM array$(new_size)
FOR i = 1 TO copy_size
array$(i) = temp_array$(i)
NEXT i
END SUB
FUNCTION str_stor$ (x, y)
str_stor = STR$(x) + "," + LTRIM$(STR$(y))
END FUNCTION
FUNCTION str_unstor (stor_str$, which$)
break = INSTR(stor_str$, ",")
final = LEN(stor_str$)
IF which$ = "x" THEN
return_val = VAL(LEFT$(stor_str$, break - 1))
ELSEIF which$ = "y" THEN
return_val = VAL(RIGHT$(stor_str$, final - break))
END IF
str_unstor = return_val
END FUNCTION
SUB flood_fill (img_array$(), x, y, new_char$)
img_array_size_x = UBOUND(img_array$, 1)
img_array_size_y = UBOUND(img_array$, 2)
REDIM processed(img_array_size_x, img_array_size_y)
REDIM popped(2)
REDIM temp_popped AS STRING
REDIM current_char AS STRING
REDIM fill_stack$(1)
replace_char$ = img_array$(x, y)
fill_stack$(1) = str_stor$(x, y)
FOR i = 1 TO x
FOR j = 1 TO y
processed(i, j) = 0
NEXT j
NEXT i
WHILE UBOUND(fill_stack$) > 0
temp_popped$ = pop$(fill_stack$())
popped(1) = str_unstor(temp_popped$, "x")
popped(2) = str_unstor(temp_popped$, "y")
current_char$ = img_array$(popped(1), popped(2))
IF current_char$ = replace_char$ THEN
img_array$(popped(1), popped(2)) = new_char$
processed(popped(1), popped(2)) = 1
IF popped(1) > 1 THEN
IF processed(popped(1) - 1, popped(2)) = 0 THEN
CALL push(fill_stack$(), str_stor$(popped(1) - 1, popped(2)))
END IF
END IF
IF popped(1) < img_array_size_x THEN
IF processed(popped(1) + 1, popped(2)) = 0 THEN
CALL push(fill_stack$(), str_stor$(popped(1) + 1, popped(2)))
END IF
END IF
IF popped(2) > 1 THEN
IF processed(popped(1), popped(2) - 1) = 0 THEN
CALL push(fill_stack$(), str_stor$(popped(1), popped(2) - 1))
END IF
END IF
IF popped(2) < img_array_size_y THEN
IF processed(popped(1), popped(2) + 1) = 0 THEN
CALL push(fill_stack$(), str_stor$(popped(1), popped(2) + 1))
END IF
END IF
END IF
WEND
END SUB
SUB process_img (x, y, fill_x, fill_y, fill_char$)
file_name$ = "img_" + LTRIM$(STR$(x)) + "_" + LTRIM$(STR$(y)) + ".txt"
row = 1
column = 1
REDIM temp$(x, y)
OPEN file_name$ FOR BINARY AS #1
DO WHILE NOT EOF(1)
GET #1, , variable%%
IF variable%% <> 13 THEN
IF variable%% <> 10 THEN
temp$(column, row) = CHR$(variable%%)
column = column + 1
IF column > x THEN
column = 1
END IF
END IF
ELSE
row = row + 1
IF row > y THEN
row = 1
END IF
END IF
LOOP
CLOSE #1
CALL flood_fill(temp$(), fill_x + 1, fill_y + 1, fill_char$)
CALL print_array(temp$())
END SUB
SUB print_array (this_array$())
x_max = UBOUND(this_array$, 1)
y_max = UBOUND(this_array$, 2)
FOR y = 1 TO y_max
FOR x = 1 TO x_max
IF x = x_max THEN
PRINT this_array$(x, y)
ELSE PRINT this_array$(x, y);
END IF
NEXT x
NEXT y
END SUB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment