Paste: aoc10 (good imports)
Author: | Sam |
Mode: | factor |
Date: | Sun, 10 Dec 2017 15:32:12 |
Plain Text |
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 [ <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 ;
Author: | jon |
Mode: | factor |
Date: | Mon, 11 Dec 2017 09:14:10 |
Plain Text |
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 <iota> >array [
<circular> round 2drop
] keep first2 * ;
: p2 ( str -- hash )
ascii encode { 17 31 73 47 23 } append
[ 0 0 ] dip
256 <iota> >array [
<circular>
[ round ] 2curry 64 swap times 2drop
] keep 16 group [ unclip [ bitxor ] reduce ] map
bytes>hex-string ;
New Annotation