Paste: aoc8

Author: jon
Mode: factor
Date: Wed, 9 Dec 2020 19:10:28
Plain Text |
:: p1 ( program -- looped? acc ) 
0 :> ip!
0 :> acc!
HS{ } clone :> seen
[ 
  ip seen in?
  ip program length = or
] [
  ip seen adjoin
  ip program nth {
    { [ "nop " ?head ] [ drop ip 1 + ip! ] }
    { [ "acc " ?head ] [ string>number acc + acc! ip 1 + ip! ] }
    { [ "jmp " ?head ] [ string>number ip + ip! ] }
  } cond
] until
ip program length = acc 
;

"/tmp/input" ascii file-lines
p1 nip .

: toggle ( str -- str' ) {
 { [ "nop " ?head ] [ "jmp " prepend ] }
 { [ "jmp " ?head ] [ "nop " prepend ] }
[ ]
} cond ;

: make-programs ( str -- seq )
[ length <iota> ] keep
[ clone [ [ toggle ] change-nth ] keep ] curry map ;

: p2 ( -- acc )
"/tmp/input" ascii file-lines make-programs
[ p1 and ] map-find drop ;


p2 .

New Annotation

Summary:
Author:
Mode:
Body: