aboutsummaryrefslogtreecommitdiff
path: root/challenge-091/paulo-custodio/forth/ch-2.fs
blob: c88fb04ccbf03f489c72034301944537fa817f90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#! /usr/bin/env gforth

\ Challenge 091
\
\ TASK #2: Jump Game
\
\ You are given an array of positive numbers @N, where value at each index
\ determines how far you are allowed to jump further. Write a script to decide
\ if you can jump to the last index. Print 1 if you are able to reach the last
\ index otherwise 0.


0 VALUE tbl-size      \ save table size

\ collect_args, append them to heap
: collect_args  ( -- )
    0 TO tbl-size
    BEGIN NEXT-ARG DUP 0> WHILE
        S>NUMBER? 0= THROW DROP ,
        tbl-size 1+ TO tbl-size
    REPEAT
;

CREATE tbl
collect_args

\ run the table
: run ( -- f )
    0           ( pos )
    BEGIN
        DUP tbl-size 1- = IF    \ pos = tbl-size - 1
            DROP 1 EXIT         \ found end
        ELSE
            DUP tbl-size 1- > IF \ pos > tbl-size - 1
                DROP 0 EXIT     \ behond end
            THEN
        THEN
        DUP CELLS tbl + @       ( pos tbl[pos] )
        DUP 0= IF               \ tbl[pos] = 0
            DROP 0 EXIT         \ no solution
        THEN
        +
    AGAIN
;

run . CR BYE