Long stairs
An evil wizard has trapped you in the stairwell of his castle. Your only hope of escape is to run up all 100 steps to reach the top, and you can run one step per second. Unfortunately the wizard uses magic to lengthen the staircase by five steps per second. The new steps are inserted randomly between existing steps, so if you're lucky some of them might be beneath you and not ahead of you.
Can you escape, or are you doomed to run up an ever-lengthening staircase forever?
Write a program to simulate your escape attempt. How are you doing after ten minutes? For every second between 600 and 609 seconds inclusive print the number of steps behind you and the number still ahead of you.
If you escaped, run 10,000 tests and print the average time taken and the average final length of the staircase.
C
<lang c>
- include <stdio.h>
- include <stdlib.h>
- include <time.h>
int main(void) {
int trial, secs_tot=0, steps_tot=0; //keep track of time and steps over all the trials int sbeh, slen, wiz, secs; //all the variables related to an individual trial time_t t; srand((unsigned) time(&t)); //random number seed printf( "Seconds steps behind steps ahead\n" ); for( trial=1;trial<=10000;trial++ ) { //10000 attempts for the runner sbeh = 0; slen = 100; secs = 0; // initialise this trial while(sbeh<slen) { //as long as the runner is still on the stairs sbeh+=1; //runner climbs a step for(wiz=1;wiz<=5;wiz++) { //evil wizard conjures five new steps if(rand()%slen < sbeh) sbeh+=1; //maybe a new step is behind us slen+=1; //either way, the staircase is longer } secs+=1; //one second has passed if(trial==1&&599<secs&&secs<610) printf("%d %d %d\n", secs, sbeh, slen-sbeh ); } secs_tot+=secs; steps_tot+=slen; } printf( "Average secs taken: %f\n", secs_tot/10000.0 ); printf( "Average final length of staircase: %f\n", steps_tot/10000.0 ); return 0;
}</lang>
- Output:
Seconds steps behind steps ahead 600 2147 953 601 2151 954 602 2156 954 603 2160 955 604 2166 954 605 2169 956 606 2174 956 607 2180 955 608 2184 956 609 2188 957 Average secs taken: 2921.493200 Average final length of staircase: 14707.466000
Fermat
<lang fermat> time_tot:=0; {we'll keep track of the total time and steps across all the trials} steps_tot:=0; !!('Seconds ','Steps behind ','Steps ahead'); for trial = 1 to 10 do
s_behind:=0; {initialise staircase and runner for this trial} stair_len:=100; time:=0; while s_behind < stair_len do s_behind:=s_behind + 1; for wiz = 1 to 5 do stair_len:=stair_len + 1; {evil wizard creates a step} {(mwaahahaha)} if (Rand|stair_len) < s_behind then s_behind:=s_behind + 1; fi od; time:=time+1; {another second has passed} if trial = 1 and 599<time and time<610 then !!(time, ' ', s_behind, ' ',stair_len-s_behind) fi; od; time_tot:=time_tot+time; steps_tot:=steps_tot+stair_len;
od;
!!(time_tot/10000); !!(steps_tot/10000);</lang>
- Output:
Seconds Steps behind Steps ahead
600 2125 975 601 2129 976 602 2132 978 603 2136 979 604 2142 978 605 2146 979 606 2151 979 607 2156 979 608 2162 978 609 2166 979 3697083 / 1250 3722083 / 250
FreeBASIC
<lang freebasic> randomize timer dim as uinteger steps_behind = 0, stairs_length = 100, seconds, j dim as uinteger seconds_tot, steps_tot print "Seconds", "steps behind", "steps ahead" for trial as uinteger = 1 to 10000 'We'll have the runner try this 10000 times
steps_behind = 0 'runner starts at the bottom seconds = 0 'reset time taken stairs_length = 100 'Staircase has 100 steps while steps_behind < stairs_length 'if the runner hasn't reached the top steps_behind += 1 'go up one step for j = 1 to 5 'The evil wizard conjures another five steps if int(rnd*stairs_length) < steps_behind then steps_behind += 1 'there's a chance that a new step will be behind you stairs_length += 1 'but either way the staircase is one step longer next j seconds += 1 'that all took one second if trial = 1 and seconds >599 and seconds < 610 then print seconds, steps_behind, stairs_length - steps_behind 'for the first attempt, see how the runner is doing after ten minutes wend seconds_tot += seconds 'if the runner escaped, track the time taken and the length of the stairs steps_tot += stairs_length
next trial
print "Average time taken: ";seconds_tot/10000; " seconds." print "Average final staircase length: ";steps_tot/10000; " steps." 'if you noticed that last number is about 100*exp(5), that's no coincidence</lang>
- Output:
Seconds steps behind steps ahead 600 2032 1068 601 2038 1067 602 2042 1068 603 2045 1070 604 2048 1072 605 2053 1072 606 2055 1075 607 2060 1075 608 2064 1076 609 2068 1077 Average time taken: 2921.9457 seconds. Average final staircase length: 14709.7285 steps.
GW-BASIC
<lang gwbasic> 10 RANDOMIZE TIMER 20 TIMET = 0 : STEPST = 0 30 FOR TRIAL = 1 TO 10000 40 TIME = 0 50 SBEH = 0 60 SLEN = 100 70 SBEH = SBEH + 1 80 IF SBEH >= SLEN THEN GOTO 160 90 FOR WIZ = 1 TO 5 100 IF INT(RND*SLEN)<SBEH THEN SBEH = SBEH + 1 110 SLEN = SLEN + 1 120 NEXT WIZ 130 TIME = TIME + 1 140 IF TRIAL = 1 AND 599<TIME AND TIME <610 THEN PRINT TIME, SBEH, SLEN-SBEH 150 GOTO 70 160 TIMET = TIMET+TIME+1 170 STEPST = STEPST + SLEN 180 NEXT TRIAL 190 PRINT TIMET/10000 200 PRINT STEPST/10000</lang>
Pascal
Free Pascal
<lang pascal> program WizardStaircase; const
StartStairLength = 100; StairsPerSpell = 5; rounds = 10000;
procedure OutActual(trials,behind,infront:Uint32); begin
writeln(trials:5,infront+behind:12,behind:10,inFront:9);
end;
function OneRun(WithOutput:boolean):NativeUint; var
inFront,behind,total,i,trials: Uint32;
begin
trials := 0; total := StartStairLength; inFront := total; behind := 0; repeat inc(trials); inc(behind); dec(infront); //spell for i := 1 to StairsPerSpell do begin if random(total)+1 <= behind then inc(behind) else inc(infront); inc(total) end;
if WithOutput then begin if (trials <= 609)AND(trials>=600) then OutActual(trials,behind,infront); end;
until infront = 0; OneRun := total;
end;
var
i, attempt, minStairs, maxStairs, total : NativeUint;
begin
minStairs := High(minStairs); maxStairs := low(maxStairs); randomize; writeln('Seconds steps total behind ahead'); OneRun(true); total := 0; For i := rounds-1 downto 0 do begin attempt:= OneRun(false); if minStairs>attempt then minstairs := attempt; if maxStairs<attempt then maxstairs := attempt; inc(total,attempt); end; writeln(' average stairs minimum maximum'); writeln(total/rounds:14:3,minStairs:9,maxStairs:10); writeln; writeln((total-StartStairLength)/rounds/StairsPerSpell:10:3,' average needed seconds');
end.</lang>
- @ TIO.RUN:
Seconds steps total behind ahead 600 3100 2171 929 601 3105 2174 931 602 3110 2178 932 603 3115 2182 933 604 3120 2187 933 605 3125 2192 933 606 3130 2196 934 607 3135 2200 935 608 3140 2204 936 609 3145 2209 936 average stairs minimum maximum 14691.943 7195 30765 2938.387 average needed seconds
Raku
<lang perl6>my ($trials, $t-total, $s-total) = 10000;
say 'Seconds steps behind steps ahead';
race for ^$trials {
my $stairs = 100; my $location = 0; my $seconds = 0;
loop { ++$seconds; ++$location; last if $location > $stairs; for (1..$stairs).roll(5) { ++$location if $_ <= $location; ++$stairs; } say " $seconds $location {$stairs-$location}" if !$_ && (599 < $seconds < 610); }
$t-total += $seconds; $s-total += $stairs;
}
say "Average seconds: {$t-total/$trials}, Average steps: {$s-total/$trials}";</lang>
- Sample output:
Seconds steps behind steps ahead 600 2143 957 601 2149 956 602 2153 957 603 2158 957 604 2164 956 605 2170 955 606 2175 955 607 2178 957 608 2183 957 609 2187 958 Average seconds: 2716.0197, Average steps: 13677.143
Wren
<lang ecmascript>import "random" for Random import "./fmt" for Fmt
var totalSecs = 0 var totalSteps = 0 var rand = Random.new() System.print("Seconds steps behind steps ahead") System.print("------- ------------ -----------") for (trial in 1..10000) {
var sbeh = 0 var slen = 100 var secs = 0 while (sbeh < slen) { sbeh = sbeh + 1 for (wiz in 1..5) { if (rand.int(slen) < sbeh) sbeh = sbeh + 1 slen = slen + 1 } secs = secs + 1 if (trial == 1 && secs > 599 && secs < 610) { Fmt.print("$d $d $d", secs, sbeh, slen - sbeh) } } totalSecs = totalSecs + secs totalSteps = totalSteps + slen
} Fmt.print("\nAverage secs taken: $h", totalSecs/10000) Fmt.print("Average final length of staircase: $h", totalSteps/10000)</lang>
- Output:
Sample run:
Seconds steps behind steps ahead ------- ------------ ----------- 600 2112 988 601 2115 990 602 2121 989 603 2126 989 604 2130 990 605 2135 990 606 2141 989 607 2146 989 608 2150 990 609 2155 990 Average secs taken: 2914.465 Average final length of staircase: 14672.325