Paste: AOC day 8

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

! ======= I hope you like some spaghetti today :D =======

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 )
    {1,4} first2 :> ( one four )
    s length {
        { 2 [ 1 ] }
        { 3 [ 7 ] }
        { 4 [ 4 ] }
        { 5
            [
                {
                    { [ s one intersect length 2 = ] [ 3 ] }
                    { [ s four intersect length 3 = ] [ 5 ] }
                    [ 2 ]
                } cond
            ]
        }
        { 6
            [
                {
                    { [ s one intersect length 1 = ] [ 6 ] }
                    { [ s four intersect length 4 = ] [ 9 ] }
                    [ 0 ]
                } cond
            ]
        }
        [ drop 8 ]
    } case ;

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: