Paste: aoc10

Author: Sam
Mode: factor
Date: Sun, 10 Dec 2017 15:29:13
Plain Text |
USING: circular formatting grouping io.encodings.ascii io.files
kernel math math.parser math.ranges sequences sequences.extras
splitting vectors ;
IN: p10

: knot ( skip current seq n -- skip current seq )
  [ [ over rotate 0 ] dip rot [ <slice> reverse! drop ] keep
    over neg rotate ] keep
  swap [ + over + [ 1 + ] dip ] dip
  [ length mod ] keep ;

: knots ( offsets rounds -- seq )
  [ 0 0 ] 2dip 256 [0,b) >vector
  -rot swap [ [ knot ] each ] curry times [ 2drop ] dip ;

: part1 ( str -- n )
  "," split [ 10 base> ] map 1 knots [ 0 2 ] dip <slice> product ;

: part2 ( str -- hash )
  { 17 31 73 47 23 } append 64 knots 16 <groups>
  [ 0 [ bitxor ] reduce "%02x" sprintf ] map-concat ;

: p10 ( -- )
  "input" ascii file-contents [ CHAR: \n = ] trim-tail
  [ part1 ] [ part2 ] bi "P1:%d\nP2:%s\n" printf ;

New Annotation

Summary:
Author:
Mode:
Body: