Paste: AOC day 8 better

Author: chunes
Mode: factor
Date: Thu, 9 Dec 2021 03:17:50
Plain Text |
USING: combinators io.encodings.ascii io.files kernel literals
math math.functions prettyprint sequences sets sorting splitting
match ;
IN: aoc.2021.08

CONSTANT: input $[ "input.txt" ascii file-lines ]

input [ " | " split1 nip " " split ] map concat
[ length { 2 3 4 7 } member? ] count .           ! part 1

: decode ( {1,4} s -- n )
    [ [ intersect length ] curry map ] keep length prefix
    {
        { { 2 _ _ } [ 1 ] }
        { { 3 _ _ } [ 7 ] }
        { { 4 _ _ } [ 4 ] }
        { { 5 2 _ } [ 3 ] }
        { { 5 _ 3 } [ 5 ] }
        { { 5 _ _ } [ 2 ] }
        { { 6 1 _ } [ 6 ] }
        { { 6 _ 4 } [ 9 ] }
        { { 6 _ _ } [ 0 ] }
        { _ [ 8 ] }
    } match-cond ;

input [   ! part 2
    " | " split1 [ " " split ] bi@ swap
    [ length { 2 4 } member? ] filter [ length ] sort-with
    swap [ decode ] with map reverse 0 [ 10^ * + ] reduce-index
] map-sum .

New Annotation

Summary:
Author:
Mode:
Body: