Paste: aoc11
Author: | jon |
Mode: | factor |
Date: | Fri, 11 Dec 2020 16:29:38 |
Plain Text |
USING: math.combinatorics math.ranges math.vectors io.encodings.ascii ;
: Mi,j [ first2 swap ] dip nth nth ;
: mmap-index -- grid' )
[ [ swap 2array ] ] dip compose [ curry map-index ] curry
map-index ; inline
: neighbours
-1 1 [a,b] 2 selections [ { 0 0 } = ] reject [ v+ ] with map ;
: grid-neighbours
[ neighbours ] dip [
[ length ] [ first length ] bi 2array
[ [ [ drop 0 >= ] [ < ] 2bi and ] 2all? ] curry filter
] keep [ Mi,j ] curry map ;
: stepcell
rot {
{ 76 [ grid-neighbours [ 35 = ] count zero? 35 76 ? ] }
{ 35 [ grid-neighbours [ 35 = ] count 4 >= 76 35 ? ] }
[ 2nip ]
} case ;
: neighbour-dirs
-1 1 [a,b] 2 selections [ { 0 0 } = ] reject ;
: ingrid?
[ length ] [ first length ] bi 2array
[ [ drop 0 >= ] [ < ] 2bi and ] 2all? ;
: (grid-see)
[ f ] 3dip [ swap [ v+ ]
dip 2dup ingrid? [ [ Mi,j nip ] 2keep pick CHAR: . = ] [ f ] if ]
curry loop 2drop
;
: grid-see
neighbour-dirs [
(grid-see)
] with with map ;
: stepcell2
rot {
{ 76 [ grid-see [ 35 = ] count zero? 35 76 ? ] }
{ 35 [ grid-see [ 35 = ] count 5 >= 76 35 ? ] }
[ 2nip ]
} case ;
: stepgrid dup [ stepcell ] curry mmap-index ;
: stepgrid2 dup [ stepcell2 ] curry mmap-index ;
: dbg
"====" print dup [ >string ] map "\n" join print ;
: p1
"/tmp/input" ascii file-lines [ >array ] map dup
[ stepgrid dbg [ = not ] keep dup rot ] loop nip
concat [ CHAR: # = ] count
;
: p2
"/tmp/input" ascii file-lines [ >array ] map dup
[ stepgrid2 dbg [ = not ] keep dup rot ] loop nip
concat [ CHAR: # = ] count
;
p1
p2
New Annotation