Paste: AOC day 5

Author: chunes
Mode: factor
Date: Sat, 5 Dec 2020 13:50:55
Plain Text |
USING: arrays combinators io.encodings.ascii io.files kernel
literals math math.functions math.order math.ranges
math.statistics prettyprint sequences sets ;
IN: aoc.2020.05

CONSTANT: input
    $[ "resource:work/aoc/2020/05/input.txt" ascii file-lines ]

: front ( low high -- low' high' ) 2dup + 2/ nip ;

: back ( low high -- low' high' ) 2dup + 2 / ceiling spin drop ;

: partition ( low high ch -- low' high' )
    {
        { CHAR: F [ front ] }
        { CHAR: B [ back ] }
        { CHAR: L [ front ] }
        [ drop back ]
    } case ;

: row ( bsp-str -- row )
    0 127 rot 3 head* [ partition ] each drop ;

: col ( bsp-str -- col )
    0 7 rot 3 tail* [ partition ] each drop ;

: seat ( bsp-str -- row col ) [ row ] [ col ] bi ;

: id ( row col -- id ) [ 8 * ] [ + ] bi* ;

: part1 ( -- ) input [ seat id ] [ max ] map-reduce . ;

: part2 ( -- )
    input [ seat id ] map dup minmax [a,b] swap diff first . ;

New Annotation

Summary:
Author:
Mode:
Body: