Paste: My attempt at AOC day 2

Author: Leo Mehraban
Mode: factor
Date: Wed, 4 Dec 2024 16:16:47
Plain Text |
! day two
: seperate-maximum ( seq -- seq max ) [ 0 swap V{ } clone [ overd [ < ] 1check [ spin [ 0 > ] 1check [ suffix ] [ drop ] if ] [ suffix ] if ] reduce ] keep like swap ;

: increments? ( seq -- ? ) [ tuck < ] collector [ 0 swap reduce ] dip nip [ ] all? ;
: decrements? ( seq -- ? ) [ tuck > ] collector [ [ seperate-maximum ] dip reduce ] dip nip [ ] all? ;
: consistant-incdec? ( seq -- ? ) dup first2 < [ increments? ] [ decrements? ] if ;
: row-safe? ( row -- ? ) dup consistant-incdec? [ [ tuck - abs [ 1 >= ] [ 3 <= ] bi and ] collector [ [ unclip ] dip reduce ] dip nip [ ] all? ] [ drop f ] if ;

: solve-day-two ( rows -- count ) [ row-safe? ] map sift length ;

: parse-day-two-input ( string -- rows ) split-lines harvest [ split-words harvest [ dec> ] map ] map ;

: fix-row ( row -- fixed? ) [ row-safe? ] 1check [ dup [ remove-nth row-safe? nip ] curry find-index drop ] unless ;

: solve-day-two-part-two ( rows -- count ) [ fix-row ] map sift length ;

New Annotation

Summary:
Author:
Mode:
Body: