aoc-forth

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.alexwennerberg.com/aoc-forth
Log | Files | Refs | README

02.fs (1111B) - raw


      1 h# 5000 constant filespace
      2 
      3 : chartonum h# 30 - ;
      4 
      5 : readline \ read a line to filespace
      6     0 begin dup 
      7     filespace + dup 1 fileread 
      8     swap c@ h# 0a <> and while ( not [newline or eof] )
      9     1+ repeat drop
     10 ;
     11 
     12 variable xpos
     13 variable ydepth
     14 variable aim
     15 
     16 : getcommand 
     17   filespace c@ \ get direction
     18   filespace 20 32 scan drop 1+ \ read until space
     19   c@ chartonum swap \ get number
     20 ;
     21 : solvea 
     22   1000 0 do readline getcommand
     23   dup 102 = if \ 'f'
     24   over xpos +!
     25   then dup 100 = if \ 'd'
     26   over ydepth +!
     27   then dup 117 = if \ 'u'
     28   over negate ydepth +!
     29   then 
     30   2drop loop
     31   xpos @ ydepth @ um*
     32 ;
     33 
     34 : solveb \ TODO figure out how to store double
     35   s" input02.txt" filename
     36   1000 0 do readline getcommand
     37   dup 102 = if \ 'f'
     38   over xpos +!
     39   over aim @ * ydepth +!
     40   then dup 100 = if \ 'd'
     41   over aim +!
     42   then dup 117 = if \ 'u'
     43   over negate aim +!
     44   then 
     45   xpos @ ydepth @ aim @ . . . cr
     46   2drop loop
     47   xpos @ ydepth @ .s um*
     48 ;
     49 
     50 : boot
     51     s" input02.txt" filename
     52     solvea d. cr \ TODO double math??
     53     0 xpos ! 0 ydepth !
     54     solveb d. cr
     55     bye ;
     56 save 02.rom \ Replace with soln number
     57 bye