Paste: aoc3

Author: Krenium
Mode: factor
Date: Wed, 4 Dec 2019 18:08:43
Plain Text |
USING: combinators io.encodings.ascii io.files kernel math
math.distances math.parser math.vectors prettyprint sequences
sets splitting ;
IN: aoc.2019.03

: >move< ( str -- len dir )
    [ rest string>number ] [
        first {
            { CHAR: U [ { 0 -1 } ] }
            { CHAR: R [ { 1 0 } ] }
            { CHAR: D [ { 0 1 } ] }
            { CHAR: L [ { -1 0 } ] }
        } case
    ] bi ;

: grow-wire ( wire command -- new-wire )
    [ dup last ] dip >move< [ v+ dup ] curry replicate nip
    append ;

: make-wire ( input -- wire )
    { { 0 0 } } swap "," split [ grow-wire ] each ;

: parse-input ( -- wire1 wire2 )
    "resource:work/aoc/2019/03/input.txt" ascii file-lines
    first2 [ make-wire ] bi@ ;

: dist ( loc -- n ) { 0 0 } manhattan-distance ;

: part1 ( -- )
    parse-input intersect rest [ dist ] infimum-by dist . ;

: part2 ( -- )
    parse-input [ intersect rest ] 2keep overd
    [ [ index ] curry map ] 2bi@ [ + ] 2map infimum . ;

New Annotation

Summary:
Author:
Mode:
Body: