From 9843b1d2592a921fba8eaef4ad66114fca641124 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Sat, 30 Nov 2024 22:06:45 -0800 Subject: [PATCH] 2015/01: Add solution for stage 1 --- .gitattributes | 1 + 2015/day-01/input | 1 + 2015/day-01/lib.fs | 40 ++++++++++++++++++++++++++++++ 2015/day-01/main.fs | 56 ------------------------------------------ 2015/day-01/stage-1.fs | 9 +++++++ 2015/day-01/test.fs | 18 ++++++++++++++ lib/tap.fs | 12 +++++++++ 7 files changed, 81 insertions(+), 56 deletions(-) create mode 100644 .gitattributes create mode 100644 2015/day-01/input create mode 100644 2015/day-01/lib.fs delete mode 100755 2015/day-01/main.fs create mode 100755 2015/day-01/stage-1.fs create mode 100755 2015/day-01/test.fs create mode 100644 lib/tap.fs diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..83e0e17 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +input linguist-vendored=true diff --git a/2015/day-01/input b/2015/day-01/input new file mode 100644 index 0000000..f1311c4 --- /dev/null +++ b/2015/day-01/input @@ -0,0 +1 @@ +()()(()()()(()()((()((()))((()((((()()((((()))()((((())(((((((()(((((((((()(((())(()()(()((()()(()(())(()((((()((()()()((((())((((((()(()(((()())(()((((()))())(())(()(()()))))))))((((((((((((()())()())())(())))(((()()()((((()(((()(()(()()(()(()()(()(((((((())(())(())())))((()())()((((()()((()))(((()()()())))(())))((((())(((()())(())(()))(()((((()())))())((()(())(((()((((()((()(())())))((()))()()(()(()))))((((((((()())((((()()((((()(()())(((((()(()())()))())(((()))()(()(()(()((((()(())(()))(((((()()(()()()(()(((())())(((()()(()()))(((()()(((())())(()(())())()()(())()()()((()(((()(())((()()((())()))((()()))((()()())((((()(()()(()(((()))()(()))))((()(((()()()))(()(((())()(()((()())(()(()()(()())(())()(((()(()())()((((()((()))))())()))((()()()()(())()())()()()((((()))))(()(((()()(((((((())()))()((((()((())()(()())(())()))(()(()())(((((((())))(((()))())))))()))())((())(()()((())()())()))))()((()()())(())((())((((()())())()()()(((()))())))()()))())(()()()(()((((((()()))())()))()(((()(((())((((()()()(()))())()()))))())()))())((())()())(((((())())((())())))(((())(((())(((((()(((((())(()(()())())(()(())(()))(()((((()))())()))))())))((()(()))))())))(((((())()))())()))))()))))(((()))()))))((()))((()((()(()(())()())))(()()()(())()))()((((())))))))(())(()((()()))(()))(()))(()((()))))))()()((((()()))()())()))))))()()()))(()((())(()))((()()()())()(((()((((())())))()((((()(()))))))())))()()())()))(()))))(()())()))))))((())))))))())()))()((())())))(()((()))()))(())))))(()))()())()()))((()(()))()()()()))))())()()))())(())()()))()))((()))))()()(()())))))()()()))((((()))()))))(()(())))(()())))((())())(()))()))))()())))()())()())))))))))()()))))())))((())((()))))())))(((()())))))))(()))()()))(()))()))))()())))))())((((()())))))))())))()()))))))))()))()))))()))))))(())))))))))())))))))))))))))())())((())))))))))()))((())))()))))))))())()(()))))))())))))()()()())()(()()()(()())(()))()()()(()())))())())))()))))())))))))()()()()())(())())()())()))))(()()()()()))))()))())())))((()())()())))()))()))))(()())))()))))))))(((()))()()))))))))))))))))))))(()))(()((()))())))())(()))(()(()(())))))()(()))()))()()))))))))))))()((()())(())())()(())))))())()())((()()))))(()()))))())()(())()))))))))))))))))))))()))(()(()())))))))()()((()))()))))))((())))()))))))))((()))())()()))())()()))((()))())))))))))))(()())()))(())((()(()()))(()())(())))()())(()(())()()))))()))()(()))))))(()))))))))))(()))())))))))))())))))())))(())))))()))))(())())))))))))()(()))))()())))())(()))()())))))))))))))())()()))))()))))))())))))()))))(())(()()()()((())()))())(()))((())()))())())(())(()()))))()))(())()()((())(())))(())))()))())))))))))()(((((())())))(())()))))(())))((()))()(((((((()))))()()))(())))))()(()))))(()()))()))())))))))(()())()))))))))())))(()))())()))(())()((())())()())())(()(()))))()))))))((()())(())()()(()())))()()))(())(())(()))())))()))(()))()()))((((()))))()))((()()()))))()))()))())))(()))()))))(())))()))())()(()))()())))())))))))())))())))()()))))))(()))())())))()))()()())())))))))))))))())))()))(()()))))())))())()(())))())))))))))))))))))()()())())))))()()()((()(()))()()(())()())()))()))))()()()))))))((()))))))))()(()(()((((((()()((()())))))))))))()))())))))((())())(()))())))())))))())()()())(())))())))()())())(())))))))()()(())))()))())))())())())()))))))))()))(()()()())())())))(())())))))))()()())()))))())))())()(())())))))))()())()))(()()(())())))()(()((()()((()()(((((())(()())()))(())()))(())))(())))))))()))()))((()))()))()))))))))()))))))))((()()())(()))(((()))(())))()))((())(((())))()())))())))))((())))))(())())((((((())())()(()))()(()((()())))((())()(()(()))))(())(()()())(())))())((()(((())())))(((()())())))())()(())())((((()()))))())((()))()()()()(())(((((((()()()((()))())(()())))(())())((((()()(()))))()((())))((())()))()(((()))())))()))((()(()))(())(()((((())((((()()(()()))(((())(()))))((((()(()))(())))))((()))(()))((()(((()(()))(()(()((()(())(()(()(()(()()((()))())(((())(()(()))))(()))()()))(())))(())()(((())(()))()((((()()))))())(()))))((())()((((()(((()))())())(((()))()())((())(())())(())()(())()(()()((((((()()))))()()(((()()))))()())()(((()(()))(()(()())(()(()))))(((((()(((())())))))(((((()((()()((())())((((((()(())(()()((()()()()()()()(()()))()(((()))()))(((((((())(((()((()())()((((())(((()(())))()((()(()()()((())((()())()))()))())))())((((((()))(()(()()()))(()((()(()(()))()((()(((()()()((())(((((())()(()))())())((()(())))(()(()())(())((())())())(((()()()(())))))())(()))))))()))))))())((()()()))((()((((((()))(((()((((()()()(((()))())()(()()(((()((()()()()())()()))()()()(()(())((()))))(()))())))))))()(()()(((((())()(()(((((()((()(()()())(()((((((((()((((((())()((((()()()((()((()((((((()))((())))))))())()))((()(()))()(()()(()((())((()()((((((((((((()())(()()()))((((()((((((())(()))())(()()((()()))()(((((((()((()()((((((()(((())))((())))((((((((()()(((((((())(((((()())(((())((())()((((()(((((((()(()(((()((((((()(((()(((((((((((()()((()()(()))((()()(((()(((())))((((())()(()(((())()(()(((())(((((((((((()))())))((((((())((()()((((()())())((((()()))((())(((((()(()()(()()()((())(()((()()((((()(((((()((()(()((((()())((((((()(((((()()(()(()((((())))(())(())(())((((()(()()((((()((((()()((()((((((())))(((((()))))()))(()((((((((()(((())())(((())))(()(()((())(((()((()()(((((()((()()(((())()(()))(((((((())(()(((((()))((()((()((()))(())())((((()((((())()(()))(((()(((((((((((((((())(((((((((()))(((()(()()()()((((((()((())()((((((((()(())(((((((((((()(()((())()((()()(()(()()((((()()((())(()((()()(()()((((()(((((((())))((((())(())()(((()()((()()((((()((()(((()((())(((()()()((((()((((()()(()(()((((((((())(()(((((())(()())(((((((()())()(()((((()((())(()()())((((()()(((()((((())(())(()()(((((((((()()))()(((())(()(()((((((())(()()())(()))()()(((()(((()((())(()(((((((()(()(()((()(((((()(()((()(()((((((()((((()()((((()(((()((())(()(()((()()((((()()(())()(())(((())(()((((((((()())(((((((((()(())()((((())))()))()()(((((()()((((((())(()()(((()(()(((((((()(()(((((((())(())((((()((()(())))((((()()())(()))((()())((((()(((((()(()(())(()(()()())(((((()(((((()((((()()((((((((()()))(()((((((())((((())()(()(((()()()(((()(()(())(())(((((()(())())((((())(())(()(((()(((((())((((())())((()(((((((()(((())(()(()))(((((((((()((()((()()(()((((())(((()((())((((())(()(((()(((()(()((((()(((())(()(((()(()()(()(()((()()(()())(())())((()(()(((()(((()(((()()(((((((((()(((((((((()()(((()(((()())((((()(()(((()()()((())((((((((((())(()(((()((((()())((((()((()))(((()()()(((((()(((((((())((()())(()((((())((((((((())(()((()((((((((((()()((()((()()))(((()())()())()(((()())()()(()(()(((((((())()))(())()))())()()((())()((()((((()((()((())(((((()((((((()(())))(()))())(((()))((()()(()(((()))((((())()(((()))))()(()(())()(((((())(()(()(())(())()((()()()((((()(())((()())(()(()))(()(()(()()(())()()(()((())()((()))))()))((()(()()()()((()())(()))())()(()(((((((((())())((()((()((((((())()((((())(((())((()(()()()((())(()((())(((()((((()()((()(()(((((())()))()((((((()))((())(((()()))(((())(())()))(((((((())(())())()(())(((((()))()((()))()(()()((()()()()()())((((((( diff --git a/2015/day-01/lib.fs b/2015/day-01/lib.fs new file mode 100644 index 0000000..31fb9ab --- /dev/null +++ b/2015/day-01/lib.fs @@ -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 +; diff --git a/2015/day-01/main.fs b/2015/day-01/main.fs deleted file mode 100755 index d196542..0000000 --- a/2015/day-01/main.fs +++ /dev/null @@ -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 diff --git a/2015/day-01/stage-1.fs b/2015/day-01/stage-1.fs new file mode 100755 index 0000000..f8ec6e5 --- /dev/null +++ b/2015/day-01/stage-1.fs @@ -0,0 +1,9 @@ +#! /usr/bin/env gforth + +require lib.fs + +s" input" open-input +parse-file . cr +close-input + +bye diff --git a/2015/day-01/test.fs b/2015/day-01/test.fs new file mode 100755 index 0000000..c5f2a3e --- /dev/null +++ b/2015/day-01/test.fs @@ -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 diff --git a/lib/tap.fs b/lib/tap.fs new file mode 100644 index 0000000..0d4f70a --- /dev/null +++ b/lib/tap.fs @@ -0,0 +1,12 @@ +Variable test# + +: plan ( n -- ) + ." 1.." . cr +; + +: ok ( f -- ) + 0= if ." not " then + ." ok " + test# dup @ 1+ dup . swap ! + cr +;