package Ġeu . jan v db . a oc 2 0 2 0 . day 1 2 Ċ Ċ import Ġeu . jan v db . a oc util . kotlin . point 2 d . Direction Ċ import Ġeu . jan v db . a oc util . kotlin . point 2 d . Point 2 D Ċ import Ġeu . jan v db . a oc util . kotlin . read Lines Ċ Ċ val ĠIN STR UCTION _ REGEX Ġ= ĠRegex (" ([ N SE WL RF ]) (\\ d + )") Ċ Ċ fun Ġmain () Ġ{ Ċ ĉ val Ġinstructions Ġ= Ġread Lines ( 2 0 2 0 , Ġ" input 1 2 . txt "). map (:: parse Instruction ) ĊĊ ĉ var Ġposition 1 Ġ= ĠPosition 1 ( Point 2 D ( 0 , Ġ 0 ), ĠDirection . E ) Ċ ĉ instructions . forEach Ġ{ Ġposition 1 Ġ= Ġposition 1 . step ( it ) Ġ} Ċ ĉ println (" At Ġ$ position 1 Ġwith Ġdistance Ġ${ position 1 . coordinate . man h att an Distance () }") ĊĊ ĉ var Ġposition 2 Ġ= ĠPosition 2 ( Point 2 D ( 0 , Ġ 0 ), ĠPoint 2 D ( 1 0 , Ġ 1 )) Ċ ĉ instructions . forEach Ġ{ Ġposition 2 Ġ= Ġposition 2 . step ( it ) Ġ} Ċ ĉ println (" At Ġ$ position 2 Ġwith Ġdistance Ġ${ position 2 . coordinate . man h att an Distance () }") Ċ } Ċ Ċ fun Ġparse Instruction ( instruction : ĠString ): ĠInstruction Ġ{ Ċ ĉ val Ġmatch Result Ġ= ĠIN STR UCTION _ REGEX . match Ent ire ( instruction ) Ġ?: Ġthrow ĠIllegalArgumentException ( instruction ) Ċ ĉ return ĠInstruction ( Instruction Type . valueOf ( match Result . group Values [ 1 ]), Ġmatch Result . group Values [ 2 ]. toInt ()) Ċ } Ċ Ċ enum Ġclass ĠInstruction Type Ġ{ Ċ ĉ N , ĠS , ĠE , ĠW , ĠL , ĠR , ĠF Ċ } Ċ Ċ data Ġclass ĠInstruction ( val Ġinstruction Type : ĠInstruction Type , Ġval Ġamount : ĠInt ) Ċ Ċ data Ġclass ĠPosition 1 ( val Ġcoordinate : ĠPoint 2 D , Ġval Ġdirection : ĠDirection ) Ġ{ Ċ ĉ fun Ġstep ( instruction : ĠInstruction ): ĠPosition 1 Ġ{ Ċĉ ĉ val Ġamount Ġ= Ġinstruction . amount Ċĉ ĉ return Ġwhen Ġ( instruction . instruction Type ) Ġ{ Ċĉĉ ĉ Instruction Type . N Ġ-> ĠPosition 1 ( coordinate . move ( Direction . N , Ġamount ), Ġdirection ) Ċĉĉ ĉ Instruction Type . E Ġ-> ĠPosition 1 ( coordinate . move ( Direction . E , Ġamount ), Ġdirection ) Ċĉĉ ĉ Instruction Type . S Ġ-> ĠPosition 1 ( coordinate . move ( Direction . S , Ġamount ), Ġdirection ) Ċĉĉ ĉ Instruction Type . W Ġ-> ĠPosition 1 ( coordinate . move ( Direction . W , Ġamount ), Ġdirection ) Ċĉĉ ĉ Instruction Type . L Ġ-> ĠPosition 1 ( coordinate , Ġdirection . rotate Left ( amount )) Ċĉĉ ĉ Instruction Type . R Ġ-> ĠPosition 1 ( coordinate , Ġdirection . rotate Right ( amount )) Ċĉĉ ĉ Instruction Type . F Ġ-> ĠPosition 1 ( coordinate . move ( direction , Ġamount ), Ġdirection ) Ċĉ ĉ } Ċ ĉ } Ċ } Ċ Ċ data Ġclass ĠPosition 2 ( val Ġcoordinate : ĠPoint 2 D , Ġval Ġway point : ĠPoint 2 D ) Ġ{ Ċ ĉ fun Ġstep ( instruction : ĠInstruction ): ĠPosition 2 Ġ{ Ċĉ ĉ val Ġamount Ġ= Ġinstruction . amount Ċĉ ĉ return Ġwhen Ġ( instruction . instruction Type ) Ġ{ Ċĉĉ ĉ Instruction Type . N Ġ-> ĠPosition 2 ( coordinate , Ġway point . move ( Direction . N , Ġamount )) Ċĉĉ ĉ Instruction Type . E Ġ-> ĠPosition 2 ( coordinate , Ġway point . move ( Direction . E , Ġamount )) Ċĉĉ ĉ Instruction Type . S Ġ-> ĠPosition 2 ( coordinate , Ġway point . move ( Direction . S , Ġamount )) Ċĉĉ ĉ Instruction Type . W Ġ-> ĠPosition 2 ( coordinate , Ġway point . move ( Direction . W , Ġamount )) Ċĉĉ ĉ Instruction Type . L Ġ-> ĠPosition 2 ( coordinate , Ġway point . rotate Left ( amount )) Ċĉĉ ĉ Instruction Type . R Ġ-> ĠPosition 2 ( coordinate , Ġway point . rotate Right ( amount )) Ċĉĉ ĉ Instruction Type . F Ġ-> ĠPosition 2 ( coordinate . move ( waypoint , Ġamount ), Ġway point ) Ċĉ ĉ } Ċ ĉ } Ċ } Ċ