Paste: AoC Day 3

Author: CapitalEx
Mode: factor
Date: Sun, 4 Dec 2022 15:15:45
Plain Text |
USING: io.backend io.encodings.utf8 io.files kernel math.parser
ranges sequences sets splitting ;
IN: advent-of-code.day-04

: get-input-one ( -- seq )
    "vocab:advent-of-code/day-04/_input/one.txt" 
        normalize-path utf8 file-lines ;

: parse-range ( string -- range )
    "-" split first2 [ dec> ] bi@ [a..b] ;

: split-pair ( string -- seq )
    "," split [ parse-range ] map ;

: overlaps? ( seq seq -- ? )
    [ subset? ] [ swap subset? ] 2bi or ;

: ranges-overlap? ( line -- ? )
    split-pair first2 overlaps? ;

: count-overlaps ( seq -- n )
    [ ranges-overlap? ] count ; 

: solve-part-one ( -- solution )
    get-input-one count-overlaps ;

: count-intersections ( seq -- n )
    [ split-pair first2 intersects? ] count ;

: solve-part-two ( -- solution )
    get-input-one count-intersections ;

New Annotation

Summary:
Author:
Mode:
Body: