Paste: AOC Day 4

Author: Kacarott
Mode: factor
Date: Sun, 4 Dec 2022 08:10:00
Plain Text |
! Copyright (C) 2022 Keldan Chapman.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel AOC prettyprint splitting sequences math.parser
       math.intervals ;
IN: AOC.2022.4

: entry>intervals ( entry -- i1 i2 )
    "-," split [ dec> ] map first4 [ [a,b] ] 2bi@ ;

: covers? ( entry -- ? )
    entry>intervals 2dup swap [ interval-subset? ] 2bi@ or ;

: overlaps? ( entry -- ? )
    entry>intervals intervals-intersect? ;

: count-with ( input quot -- result )
    [ split-lines harvest ] dip count ; inline

: part-1 ( input -- result ) [ covers? ] count-with ;
: part-2 ( input -- result ) [ overlaps? ] count-with ;

MAIN: [ 4 read-day-input [ part-1 . ] [ part-2 . ] bi ]

New Annotation

Summary:
Author:
Mode:
Body: