USING: 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 [ 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 product ; : part2 ( str -- hash ) { 17 31 73 47 23 } append 64 knots 16 [ 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 ;