Paste: aoc12p2
Author: | jonenst |
Mode: | factor |
Date: | Sat, 12 Dec 2020 09:53:06 |
Plain Text |
TUPLE: ship pos way ;
: <ship> ( -- ship ) { 0 0 } { 10 1 } \ ship boa ;
: move ( ship len dir -- )
n*v [ v+ ] curry change-way drop ;
: rotate ( ship degrees -- )
[ dup way>> ] dip {
{ 90 [ [ second ] [ first neg ] bi 2array ] }
{ 180 [ [ neg ] map ] }
{ 270 [ [ second neg ] [ first ] bi 2array ] }
} case >>way drop ;
: forward ( ship n -- )
over way>> n*v [ v+ ] curry change-pos drop ;
: 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