Paste: AoC day 4

Author: Garklein
Mode: factor
Date: Sun, 4 Dec 2022 14:38:14
Plain Text |
USING: arrays grouping io.encodings.utf8 io.files kernel
math.parser math.rectangles math.statistics sequences splitting ;
IN: aoc.4

: pair>rect ( {x,w} -- r ) dup differences [ first 1 2array ] bi@ <rect> ;

: input ( -- input ) "~/factor/aoc/4/4.in" utf8 file-lines
    [ "-," split [ dec> ] map 2 <groups> [ pair>rect ] map ] map ;

: split-pair ( pair -- x y ) [ first ] [ last ] bi ;

: fully-contains? ( {r1,r2} -- ? ) dup split-pair (rect-union) <extent-rect> swap member? ;

: part1 ( -- n ) input [ fully-contains? ] count ;

: part2 ( -- n ) input [ split-pair contains-rect? ] count ;

Annotation: oh intervals is a thing...

Author: Garklein
Mode: factor
Date: Sun, 4 Dec 2022 14:55:05
Plain Text |
USING: grouping io.encodings.utf8 io.files kernel math.intervals
math.parser sequences splitting ;
IN: aoc.4

: split-pair ( pair -- x y ) [ first ] [ last ] bi ;

: input ( -- input ) "~/factor/aoc/4/4.in" utf8 file-lines
    [ "-," split [ dec> ] map 2 <groups> [ split-pair [a,b] ] map ] map ;

: fully-contains? ( {i1,i2} -- ? ) dup split-pair interval-union swap member? ;

: part1 ( -- n ) input [ fully-contains? ] count ;

: part2 ( -- n ) input [ split-pair intervals-intersect? ] count ;

New Annotation

Summary:
Author:
Mode:
Body: