Paste: AOC 2024 Day 2
Author: | knottio |
Mode: | factor |
Date: | Thu, 5 Dec 2024 19:15:21 |
Plain Text |
USING: accessors combinators.short-circuit grouping
io.encodings.utf8 io.files kernel math math.order math.parser
sequences sequences.private sequences.snipped
sequences.windowed.fixed splitting ;
IN: aoc.2024.02
CONSTANT: input-file "vocab:aoc/2024/02/input.txt"
: parsed-input ( -- input )
input-file utf8 file-lines
[ " " split [ string>number ] map ] map ;
: gradual? ( seq -- ? )
2 <clumps> [ first2 - abs 1 3 between? ] all? ;
: strictly-monotonic? ( seq -- ? )
2 <clumps> [ first2 <=> ] map
2 <clumps> [ first2 = ] all? ;
: safe? ( seq -- ? )
{ [ gradual? ] [ strictly-monotonic? ] } 1&& ;
: part1 ( -- n )
parsed-input [ safe? ] count ;
: safe-enough? ( seq -- ? )
[ length <iota> ] keep [ <removed> safe? ] curry any? ;
: part2 ( -- n )
parsed-input [ safe-enough? ] count ;
New Annotation