Paste: aoc4

Author: erg
Mode: factor
Date: Sun, 4 Dec 2022 05:15:19
Plain Text |
! Copyright (C) 2022 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors interval-sets kernel math math.intervals math.parser
sequences splitting ;
IN: 2022-aoc4

: i1-in-i2? ( i1 i2 -- ? )
    [ [ from>> first ] dip interval-contains? ]
    [ [ to>> first ] dip interval-contains? ] 2bi and ;

: aoc4-1 ( string -- n )
    split-lines 0 [
        "," split1 [ "-" split1 [ dec> ] bi@ [a,b] ] bi@
        [ i1-in-i2? ] [ swap i1-in-i2? ] 2bi or 1 0 ? +
    ] reduce ;

: i1-overlap-i2? ( i1 i2 -- ? )
    [ [ from>> first ] dip interval-contains? ]
    [ [ to>> first ] dip interval-contains? ] 2bi or ;

: aoc4-2 ( string -- n )
    split-lines 0 [
        "," split1 [ "-" split1 [ dec> ] bi@ [a,b] ] bi@
        [ i1-overlap-i2? ] [ swap i1-overlap-i2? ] 2bi or 1 0 ? +
    ] reduce ;

Annotation: better

Author: erg
Mode: factor
Date: Sun, 4 Dec 2022 05:22:59
Plain Text |
! Copyright (C) 2022 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: combinators.short-circuit kernel math.intervals
math.parser sequences splitting ;
IN: 2022-aoc4

: parse-line ( string -- i1 i2 )
    "," split1 [ "-" split1 [ dec> ] bi@ [a,b] ] bi@ ;

: aoc4-1 ( string -- n )
    split-lines [
        parse-line
        { [ interval-subset? ] [ swap interval-subset? ] } 2||
    ] count ;

: aoc4-2 ( string -- n )
    split-lines [
        parse-line
        { [ intervals-intersect? ] [ swap intervals-intersect? ] } 2||
    ] count ;

Annotation: no inverse op for aoc4-2

Author: erg
Mode: factor
Date: Sun, 4 Dec 2022 15:44:15
Plain Text |
! Copyright (C) 2022 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: combinators.short-circuit kernel math.intervals
math.parser sequences splitting ;
IN: 2022-aoc4

: parse-line ( string -- i1 i2 )
    "," split1 [ "-" split1 [ dec> ] bi@ [a,b] ] bi@ ;

: aoc4-1 ( string -- n )
    split-lines [
        parse-line [ interval-subset? ] [ swap interval-subset? ] 2bi or
    ] count ;

: aoc4-2 ( string -- n )
    split-lines [
        parse-line intervals-intersect?
    ] count ;

New Annotation

Summary:
Author:
Mode:
Body: