Paste: AOC day 12
Author: | chunes |
Mode: | factor |
Date: | Sun, 13 Dec 2020 17:30:20 |
Plain Text |
USING: math.distances ;
IN: aoc.2020.12
CONSTANT: input $[
"input.txt" ascii file-lines
[ unclip [ dec> ] dip 2array ] map
]
TUPLE: turtle
{ loc initial: { 0 0 } } { heading initial: { 0 1 } } ;
: move [ v+ ] curry change-loc ;
: north neg 0 2array move ;
: south 0 2array move ;
: east 0 swap 2array move ;
: west neg 0 swap 2array move ;
: (left) first2 neg swap 2array ;
: (right) first2 swap neg 2array ;
: turn
[ 90 / ] dip [ change-heading ] curry times ; inline
: left [ (left) ] turn ;
: right [ (right) ] turn ;
: forward over heading>> n*v move ;
: go
{
{ CHAR: N [ north ] }
{ CHAR: S [ south ] }
{ CHAR: E [ east ] }
{ CHAR: W [ west ] }
{ CHAR: L [ left ] }
{ CHAR: R [ right ] }
{ CHAR: F [ forward ] }
} case ;
: part1
turtle new input [ go ] assoc-each loc>> { 0 0 }
manhattan-distance . ;
TUPLE: wpturtle
{ loc initial: { 0 0 } } { wp initial: { -1 10 } } ;
: wpmove [ v+ ] curry change-wp ;
: wpnorth neg 0 2array wpmove ;
: wpsouth 0 2array wpmove ;
: wpeast 0 swap 2array wpmove ;
: wpwest neg 0 swap 2array wpmove ;
: wpturn
[ 90 / ] dip [ change-wp ] curry times ; inline
: wpleft [ (left) ] wpturn ;
: wpright [ (right) ] wpturn ;
: wpforward over wp>> n*v move ;
: wpgo
{
{ CHAR: N [ wpnorth ] }
{ CHAR: S [ wpsouth ] }
{ CHAR: E [ wpeast ] }
{ CHAR: W [ wpwest ] }
{ CHAR: L [ wpleft ] }
{ CHAR: R [ wpright ] }
{ CHAR: F [ wpforward ] }
} case ;
: part2
wpturtle new input [ wpgo ] assoc-each loc>> { 0 0 }
manhattan-distance . ;
New Annotation