Perl solution. Most of the decisions are represented by the hash tables at the beginning of the code:
#!/usr/bin/perlusewarnings;usestrict;usefeatureqw{ say };useList::Utilqw{ first };useenumqw( X Y FACING TURN );useenumqw( LEFT STRAIGHT RIGHT );useconstant{MOVE=>{'<'=>[-1,0],'>'=>[1,0],'v'=>[0,1],'^'=>[0,-1],},BEND=>{'^/'=>'>','^\\'=>'<','v/'=>'<','v\\'=>'>','</'=>'v','<\\'=>'^','>/'=>'^','>\\'=>'v',},FACES=>['^','>','v','<'],};my(@map,@carts,%crash);while(<>){chomp;while(/([<>^v])/g){push@carts,[pos()-1,$.-1,$1,LEFT];++$crash{$carts[-1][X]}{$carts[-1][Y]};}s/[<>]/-/g;s/[v^]/|/g;push@map,[split//,$_,-1];}while(1){formy$cart(sort{$a->[Y]<=>$b->[Y]||$a->[X]<=>$b->[X]}@carts){--$crash{$cart->[X]}{$cart->[Y]};my$move=MOVE->{$cart->[FACING]};$cart->[$_]+=$move->[$_]forX,Y;if($crash{$cart->[X]}{$cart->[Y]}++){say"$cart->[X],$cart->[Y]";exit}my$current=$map[$cart->[Y]][$cart->[X]];nextif$current=~/[-|]/;if('+'eq$current){my$face_index=first{$cart->[FACING]eqFACES->[$_]}0..$#{(FACES)};$face_index+=(-1,0,1)[$cart->[TURN]];$cart->[FACING]=FACES->[$face_index%4];++$cart->[TURN];$cart->[TURN]%=3;}else{$cart->[FACING]=BEND->{$cart->[FACING].$current};}}}
In part 2, I just replaced the code that printed the collision with a new one that removes the two carts involved.
#!/usr/bin/perlusewarnings;usestrict;usefeatureqw{ say };useList::Utilqw{ first };useenumqw( X Y FACING TURN );useenumqw( LEFT STRAIGHT RIGHT );useconstant{MOVE=>{'<'=>[-1,0],'>'=>[1,0],'v'=>[0,1],'^'=>[0,-1],},BEND=>{'^/'=>'>','^\\'=>'<','v/'=>'<','v\\'=>'>','</'=>'v','<\\'=>'^','>/'=>'^','>\\'=>'v',},FACES=>['^','>','v','<'],};my@map;my@carts;my%crash;while(<>){chomp;while(/([<>^v])/g){push@carts,[pos()-1,$.-1,$1,LEFT];++$crash{$carts[-1][X]}{$carts[-1][Y]};}s/[<>]/-/g;s/[v^]/|/g;push@map,[split//,$_,-1];}while(1){formy$cart(sort{$a->[Y]<=>$b->[Y]||$a->[X]<=>$b->[X]}@carts){nextunless$cart->[FACING];--$crash{$cart->[X]}{$cart->[Y]};my$move=MOVE->{$cart->[FACING]};$cart->[$_]+=$move->[$_]forX,Y;if($crash{$cart->[X]}{$cart->[Y]}++){$crash{$cart->[X]}{$cart->[Y]}-=2;$cart->[FACING]=0;my$other_cart=first{$_->[FACING]&&$_->[X]==$cart->[X]&&$_->[Y]==$cart->[Y]}@carts;$other_cart->[FACING]=0;next}my$current=$map[$cart->[Y]][$cart->[X]];nextif$current=~/[-|]/;if('+'eq$current){my$face_index=first{$cart->[FACING]eqFACES->[$_]}0..$#{(FACES)};$face_index+=(-1,0,1)[$cart->[TURN]];$cart->[FACING]=FACES->[$face_index%4];++$cart->[TURN];$cart->[TURN]%=3;}else{$cart->[FACING]=BEND->{$cart->[FACING].$current};}}my@left=grep$carts[$_][FACING],0..$#carts;nextunless1==@left;sayjoin',',@{$carts[$left[0]]}[X,Y];exit}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Perl solution. Most of the decisions are represented by the hash tables at the beginning of the code:
In part 2, I just replaced the code that printed the collision with a new one that removes the two carts involved.