Paste: AoC 2022 day 4

Author: xr
Mode: factor
Date: Sun, 4 Dec 2022 13:50:25
Plain Text |
! Copyright (C) 2022 Your name.
! See http://factorcode.org/license.txt for BSD license.
USING: aoc splitting math sequences accessors math.parser kernel math.bitwise bit-arrays ;
IN: aoc2022-4

TUPLE: range { lo integer }  { hi integer } ;
C: <range> range
: >range ( seq -- range ) first2 <range> ;

: at-or-above-lo ( range range -- ? )
  [ lo>> ] bi@ >= ;

: at-or-below-hi ( range range -- ? )
  [ hi>> ] bi@ <= ;

: fully-contained? ( range range -- ? )
  [ at-or-above-lo ] [ at-or-below-hi ] 2bi and ;

: fully-contained-pair? ( range range -- ? )
  [ fully-contained? ] [ swap fully-contained? ] 2bi or ;

: last2 ( seq -- x y ) 2 tail* first2 ;

: infix-split ( string quote -- str str )
  '[ _ split1-slice [ @ ] bi@ ] call ; inline

: parse-range ( str -- range )
  "-" [ dec> ] infix-split <range> ;

: parse-range-pairs ( str -- range range )
  "," [ parse-range ] infix-split ;

: part1 ( -- seq )
  "/home/xander/factor/factor/work/aoc2022-4/input.txt" load-file
  [ parse-range-pairs fully-contained-pair? ] map
  >bit-array bit-count
;

: overlap? ( range range -- ? )
  [ [ hi>> ] [ lo>> ] bi* >= ] [ at-or-below-hi ] 2bi and ;

: overlapping-pair? ( range range -- ? )
  [ overlap? ] [ swap overlap? ] 2bi or ;

: part2 ( -- n )
  "/home/xander/factor/factor/work/aoc2022-4/input.txt" load-file
  [ parse-range-pairs overlapping-pair? ] map
  >bit-array bit-count
;

Annotation: AoC 2022 day 4

Author: xr
Mode: factor
Date: Sun, 4 Dec 2022 14:22:14
Plain Text |
! Copyright (C) 2022 Your name.
! See http://factorcode.org/license.txt for BSD license.
USING: aoc splitting math sequences accessors math.parser kernel math.bitwise bit-arrays ;
IN: aoc2022-4

TUPLE: range { lo integer }  { hi integer } ;
C: <range> range

: at-or-above-lo ( range range -- ? )
  [ lo>> ] bi@ >= ;

: at-or-below-hi ( range range -- ? )
  [ hi>> ] bi@ <= ;

: fully-contained? ( range range -- ? )
  [ at-or-above-lo ] [ at-or-below-hi ] 2bi and ;

: fully-contained-pair? ( range range -- ? )
  [ fully-contained? ] [ swap fully-contained? ] 2bi or ;

: infix-split ( string quote -- str str )
  '[ _ split1-slice [ @ ] bi@ ] call ; inline

: parse-range ( str -- range )
  "-" [ dec> ] infix-split <range> ;

: parse-range-pairs ( str -- range range )
  "," [ parse-range ] infix-split ;

: part1 ( -- seq )
  "resource:work/aoc2022-4/input.txt" load-file
  [ parse-range-pairs fully-contained-pair? ] map
  >bit-array bit-count
;

: overlap? ( range range -- ? )
  [ [ hi>> ] [ lo>> ] bi* >= ] [ at-or-below-hi ] 2bi and ;

: overlapping-pair? ( range range -- ? )
  [ overlap? ] [ swap overlap? ] 2bi or ;

: part2 ( -- n )
  "resource:work/aoc2022-4/input.txt" load-file
  [ parse-range-pairs overlapping-pair? ] map
  >bit-array bit-count
;

New Annotation

Summary:
Author:
Mode:
Body: