Paste: AoC day 10

Author: switchy
Mode: factor
Date: Sat, 10 Dec 2022 06:14:25
Plain Text |
USING: advent kernel arrays sequences sequences.extras math math.parser math.vectors ascii grouping strings formatting ;
IN: advent.day10

<PRIVATE

: (input) ( -- input )
    "day10.in" get-input
    [ dup "noop" = [ 1array ] [ "noop" swap 2array ] if ] map-concat ;

: (compute) ( input -- seq )
    1 [
        [ [ digit? ] [ CHAR: - = ] bi or not ] drop-while
        string>number
        [ + ] when*
    ] accumulate nip
    ;

: (strengths) ( seq -- n )
    { 20 60 100 140 180 220 } swap
    [ dup 1 v-n ] dip
    nths v* sum
    ;

PRIVATE>

: day10-part1 ( -- seq )
    (input) (compute) (strengths)
    ;

: day10-part2 ( -- )
    (input) (compute)
    [ 40 mod - abs 1 <= CHAR: # CHAR: . ? ] map-index
    40 <groups>
    [ >string "%s\n" printf ] each
    ;

New Annotation

Summary:
Author:
Mode:
Body: