commit ed3423eea3ecc45400646025ecd090949da2871b
parent 1c11e3ce2fc9777e11fd0d8baed6d094cbd112be
Author: alex wennerberg <alex@alexwennerberg.com>
Date: Sun, 11 Dec 2022 12:36:41 -0800
use deferred words
Diffstat:
1 file changed, 17 insertions(+), 10 deletions(-)
diff --git a/2022/08.fs b/2022/08.fs
@@ -1,6 +1,6 @@
10 constant (cr)
\ size is forest size
-5 constant size
+99 constant size
create forest size size * allot ;
create visible size size * allot ;
@@ -18,7 +18,7 @@ variable (max)
\ "functional programming"
: inv size 1- swap - swap size 1- swap - swap ;
-: down ;
+: down noop ;
: up inv down ;
: right swap ;
: left inv right ;
@@ -26,23 +26,24 @@ variable (max)
: set-if-vis ( x y u -- )
dup (max) @ > if (max) ! mark-vis else 2drop drop then ;
-variable director
+defer direct
: count-visible ( xt -- )
size 0 do reset-max
size 0 do
-i j director @ execute
+i j direct
2dup fidx set-if-vis
loop loop ;
: total-vis 0 size size * 0 do i visible + c@ + loop ;
-: part1 ['] down director !
+: part1
+ ['] down is direct
count-visible
- ['] up director !
+ ['] up is direct
count-visible
- ['] right director !
+ ['] right is direct
count-visible
- ['] left director !
+ ['] left is direct
count-visible
total-vis . cr ;
@@ -56,10 +57,16 @@ variable highest-score
: below 1+ ;
: above 1- ;
-: scenic-score ( x y -- u ) ;
+variable treehouse
+variable score
+: scenic-score ( x y -- u )
+2dup treehouse fidx treehouse !
+0 score !
+dup size do dup i below fidx > if 1 score +! then loop ; \ ETC
\ reuse visibile array
+
: part2 size 0 do size 0 do i j scenic-score highest-score >?! loop loop ;
: solve read-input
- part1 part2 ;
+ part1 ;
solve