David Lambert/AOC18

From J Wiki
Jump to navigation Jump to search

https://adventofcode.com/2018/day/13


TRACK=: ];._2 CR-.~1!:1<'d:/tmp/tracks'
CARTS=: (,TRACK)-.(a.-.'<>v^') NB. >^^^v>><^vv^vvvv>
COORDS=: (($TRACK)#:I.) (,TRACK) e. '<>v^'
TURNS=: 0 $~ #CARTS
TRACK=:((CARTS i.~'<>v^'){'--||')COORDS}TRACK
D=:];._2]0 :0
<-<<<
</vvv
<\^^^
<+v<^
>->>>
>/^^^
>\vvv
>+^>v
v|vvv
v/<<<
v\>>>
v+>v<
^|^^^
^/>>>
^\<<<
^+<^>
)
IS=:2&{.&.:|:D
BECOMES=:2&}.&.:|:D

prep2=: 3 : 0
 if. '' -: y do. y =. <'d:/tmp/tracks' end.
 TRACK=: ];._1 LF,CR-.~1!:1 boxopen y
 CARTS=: (,TRACK)-.(a.-.'<>v^') NB. >^^^v>><^vv^vvvv>
 COORDS=: (($TRACK)#:I.) (,TRACK) e. '<>v^'
 SCRAP=: (($TRACK)#:(#CARTS){.I.) (,TRACK) e. ' '
 TRACK=: '+' (;/SCRAP)} TRACK
 TURNS=: 0 $~ #CARTS
 CRASHED=: 0 $~ #CARTS
 TRACK=:((CARTS i.~'<>v^'){'--||')COORDS}TRACK
 D=.(16 5$'<-<<<</vvv<\^^^<+v<^>->>>>/^^^>\vvv>+^>vv|vvvv/<<<v\>>>v+>v<^|^^^^/>>>^\<<<^+<^>')
 IS=:2&{.&.:|:D
 BECOMES=:2&}.&.:|:D
 EMPTY
)

step2=: 3 : 0
 track=. (;/COORDS){TRACK
 CARTS=: (3 | TURNS) {"0 1 (IS i. (CARTS ,. track)) { BECOMES
 TURNS=: TURNS + '+' = track
 V=. ('<>v^' i. CARTS) { (|."1) _1 0,1 0,0 1,:0 _1
 locations=. COORDS + V
 NB.for_i. i. # CARTS do.
 for_i. /: COORDS do.
  if. -. i { CRASHED do.
   location=. i { locations
   if. (j =. COORDS i. location) = # CARTS do.
    COORDS=: location i}COORDS
   else.
    echo location
    CRASHED=: 1 (i, j)} CRASHED
    COORDS=: (i { SCRAP) i} COORDS
    COORDS=: (j { SCRAP) j} COORDS
   end.
  end.
 end.
)

main=: 3 :0
 prep2''
 whilst. 1 < (+/ 0 = CRASHED) do. step2'' end.
 < COORDS #~ -. CRASHED
)