Paste: aoc08

Author: jon
Mode: factor
Date: Fri, 8 Dec 2017 15:53:19
Plain Text |
! Copyright (C) 2017 Your name.
! See http://factorcode.org/license.txt for BSD license.
USING: assocs combinators io.encodings.ascii io.files kernel
math math.parser sequences splitting ;
IN: aoc.2017.08

: conditionok? ( insn assoc -- ? )
  swap 4 tail
  [ first swap at 0 or ] [ third string>number ] [ second ] tri
  {
    { "<" [ < ] }
    { ">" [ > ] }
    { "<=" [ <= ] }
    { ">=" [ >= ] }
    { "==" [ = ] }
    { "!=" [ = not ] }
  } case ;  
SYMBOL: maxreg
: (do-insn) ( n/f insn -- n' )
  [ 0 or ] dip
  [ third string>number ] [ second "inc" = ] bi
  [ + ] [ - ] if 
  dup maxreg get 0 or > [ dup maxreg set ] when ;
  
: do-insn ( line assoc -- )
  [ " " split ] dip
  2dup conditionok? [
    over [ first ] 2dip [ (do-insn) ] curry change-at
  ] [ 2drop ] if ; 


: p1 ( -- n )
  "/tmp/input" ascii file-lines
  H{ } clone [
    [ do-insn ] curry each
  ] keep values supremum ;

: p2 ( -- n )
  0 maxreg [ p1 drop maxreg get ] with-variable ;

New Annotation

Summary:
Author:
Mode:
Body: