2015/01: Add solution for stage 1

This commit is contained in:
Tom Willemse 2024-11-30 22:06:45 -08:00
parent ec99b4770b
commit 9843b1d259
7 changed files with 81 additions and 56 deletions

1
.gitattributes vendored Normal file
View file

@ -0,0 +1 @@
input linguist-vendored=true

1
2015/day-01/input vendored Normal file

File diff suppressed because one or more lines are too long

40
2015/day-01/lib.fs Normal file
View file

@ -0,0 +1,40 @@
: 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
;

View file

@ -1,56 +0,0 @@
#! /usr/bin/env gforth
: 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
r> ;
s" (())"
parse-instructions
." return: " . cr drop
s" ()()"
parse-instructions
." return: " . cr drop
s" ((("
parse-instructions
." return: " . cr drop
s" (()(()("
parse-instructions
." return: " . cr drop
s" ))((((("
parse-instructions
." return: " . cr drop
s" ())"
parse-instructions
." return: " . cr drop
s" ))("
parse-instructions
." return: " . cr drop
s" )))"
parse-instructions
." return: " . cr drop
s" )())())"
parse-instructions
." return: " . cr drop
bye

9
2015/day-01/stage-1.fs Executable file
View file

@ -0,0 +1,9 @@
#! /usr/bin/env gforth
require lib.fs
s" input" open-input
parse-file . cr
close-input
bye

18
2015/day-01/test.fs Executable file
View file

@ -0,0 +1,18 @@
#! /usr/bin/env gforth
require lib.fs
require ../../lib/tap.fs
8 plan
s" (())" parse-instructions 0 = ok
s" ()()" parse-instructions 0 = ok
s" (((" parse-instructions 3 = ok
s" (()(()(" parse-instructions 3 = ok
s" ))(((((" parse-instructions 3 = ok
s" ())" parse-instructions -1 = ok
s" ))(" parse-instructions -1 = ok
s" )))" parse-instructions -3 = ok
s" )())())" parse-instructions -3 = ok
bye

12
lib/tap.fs Normal file
View file

@ -0,0 +1,12 @@
Variable test#
: plan ( n -- )
." 1.." . cr
;
: ok ( f -- )
0= if ." not " then
." ok "
test# dup @ 1+ dup . swap !
cr
;