advent-of-code-forth/2015/day-01/lib.fs

40 lines
637 B
Forth

: parse-instructions ( ulen addr -- )
0 >r
0 u+do
c@+
dup 40 = if
r> r> r>
1 +
>r >r >r
then
41 = if
r> r> r>
1 -
>r >r >r
then
loop
drop
r>
;
256 Constant max-line
Create line-buffer max-line 2 + allot
0 Value fd-in
: open-input ( addr u -- )
r/o open-file throw to fd-in
;
: close-input ( -- )
fd-in close-file throw
;
: parse-file ( -- u )
0
begin
line-buffer max-line fd-in read-line throw
while
line-buffer swap parse-instructions +
repeat
drop
;