! Copyright (C) 2017 Jon Harper. ! See http://factorcode.org/license.txt for BSD license. USING: arrays circular grouping io.encodings.ascii io.encodings.string kernel math math.parser math.ranges sequences splitting ; IN: aoc.2017.10 CONSTANT: input "46,41,212,83,1,255,157,65,139,52,39,254,2,86,0,204" : rev! ( start length circular -- ) [ over + [a,b) ] dip [ nths reverse ] [ [ set-nth ] curry 2each ] 2bi ; : round ( skip start lengths circular -- skip' start' ) [ [ rev! ] [ drop [ 2drop 1 + ] [ + + ] 3bi ] 3bi ] curry each ; : p1 ( str -- n ) "," split [ string>number ] map [ 0 0 ] dip 256 >array [ round 2drop ] keep first2 * ; : p2 ( str -- hash ) ascii encode { 17 31 73 47 23 } append [ 0 0 ] dip 256 >array [ [ round ] 2curry 64 swap times 2drop ] keep 16 group [ unclip [ bitxor ] reduce ] map bytes>hex-string ;