Paste: aoc14

Author: jon
Mode: factor
Date: Mon, 14 Dec 2020 07:24:33
Plain Text |
:: p1 ( -- n )
64 CHAR: X <array> :> mask!
H{ } clone :> mem
"/tmp/input" ascii file-lines [
  {
    { [ "mask = " ?head ] [ mask! ] }
    { [ "mem[" ?head ] [
        "] =" split [ first ] [ last ] bi
        [ string>number ] bi@
        mask <reversed> [ swap {
          { CHAR: X [ drop ] }
          { CHAR: 0 [ clear-bit ] }
          { CHAR: 1 [ set-bit ] }          
        } case ] each-index
        swap mem set-at
    ] }
  } cond
] each
mem values sum
;


: addresses ( add mask -- adds )
dup empty? [ 2drop { "" } ] [
[ unclip-slice ] bi@ swapd {
          { CHAR: X [ drop addresses [
              CHAR: 0 CHAR: 1 [ prefix ] bi-curry@ bi 2array
            ] map concat
          ] }
          { CHAR: 0 [ [ addresses ] dip [ prefix ] curry map ] }
          { CHAR: 1 [ drop addresses [ CHAR: 1 prefix ] map ] }          
        } case ] if ;
:: p2 ( -- n )
64 CHAR: X <array> :> mask!
H{ } clone :> mem
"/tmp/input" ascii file-lines [
  {
    { [ "mask = " ?head ] [ mask! ] }
    { [ "mem[" ?head ] [
        "] =" split [ first ] [ last ] bi
        [ string>number ] bi@
        [ >bin 36 CHAR: 0 pad-head mask addresses [ bin> ] map ] dip [
          swap mem set-at
        ] curry each
    ] }
  } cond
] each
mem values sum
;

New Annotation

Summary:
Author:
Mode:
Body: