Paste: AoC day 12
Author: | Garklein |
Mode: | factor |
Date: | Tue, 13 Dec 2022 01:26:13 |
Plain Text |
USING: arrays assocs combinators io.encodings.utf8 io.files
kernel math math.functions math.matrices math.vectors
path-finding sequences splitting ;
IN: aoc.12
: input ( -- lines ) "~/factor/aoc/12/12.in" utf8 file-lines [ >array ] map ;
: neighbour-indices ( idx -- neighbours ) { [ { 0 1 } v+ ] [ { 0 -1 } v+ ] [ { 1 0 } v+ ] [ { -1 0 } v+ ] } cleave 4array ;
:: neighbours ( elt i j m -- seq ) { i j } neighbour-indices [ [ m dimension v< ] [ { 0 0 } v>= ] bi append [ ] all? ] filter
[ m matrix-nth elt - 1 <= ] filter ;
: I ( -- astar ) input [ "S" "a" replace "E" "z" replace ] map dup [ neighbours ] curry matrix-map-index
input dimension <coordinate-matrix> [ concat ] bi@ swap zip <bfs> ;
: m-find ( matrix char -- {row,col} ) [ [ first length ] [ concat ] bi ] dip swap index swap /mod 2array ;
: to-end ( start -- n ) input CHAR: E m-find I find-path length 1 - ;
: part1 ( -- n ) input CHAR: S m-find to-end ;
: part2 ( -- n ) input dimension first <iota> [ 1 2array ] map [ to-end ] map infimum 1 + ;
New Annotation