Paste: aoc12p1
Author: | jon |
Mode: | factor |
Date: | Sat, 12 Dec 2020 09:54:06 |
Plain Text |
TUPLE: ship pos dir ;
: <ship> ( -- ship ) { 0 0 } { 1 0 } \ ship boa ;
: move ( ship len dir -- )
n*v [ v+ ] curry change-pos drop ;
: rotate ( ship degrees -- )
[ dup dir>> ] dip {
{ 90 [ [ second ] [ first neg ] bi 2array ] }
{ 180 [ [ neg ] map ] }
{ 270 [ [ second neg ] [ first ] bi 2array ] }
} case >>dir drop ;
: forward ( ship n -- ) over dir>> move ;
: sail ( line ship -- )
swap unclip [ string>number ] dip {
{ CHAR: N [ { 0 +1 } move ] }
{ CHAR: S [ { 0 -1 } move ] }
{ CHAR: E [ { +1 0 } move ] }
{ CHAR: W [ { -1 0 } move ] }
{ CHAR: L [ 360 swap - rotate ] }
{ CHAR: R [ rotate ] }
{ CHAR: F [ forward ] }
} case ;
"/tmp/input" ascii file-lines
<ship> [
[ sail ] curry each
] keep pos>> l1-norm
New Annotation