2011-09-25 15:25:52 -04:00
|
|
|
! Copyright (C) 2011 John Benediktsson
|
|
|
|
! See http://factorcode.org/license.txt for BSD license
|
|
|
|
|
|
|
|
USING: accessors combinators fry hash-sets hashtables kernel
|
2011-09-25 18:33:49 -04:00
|
|
|
locals math math.parser sequences sets vectors ;
|
2011-09-25 15:25:52 -04:00
|
|
|
|
2011-09-25 18:33:49 -04:00
|
|
|
IN: benchmark.ant
|
2011-09-25 15:25:52 -04:00
|
|
|
|
|
|
|
! There is an ant which can walk around on a planar grid. The ant
|
|
|
|
! can move one space at a time left, right, up or down. That is,
|
|
|
|
! from (x, y) the ant can go to (x+1, y), (x-1, y), (x, y+1), and
|
|
|
|
! (x, y-1).
|
|
|
|
!
|
|
|
|
! Points where the sum of the digits of the x coordinate plus the
|
|
|
|
! sum of the digits of the y coordinate are greater than 25 are
|
|
|
|
! inaccessible to the ant. For example, the point (59,79) is
|
|
|
|
! inaccessible because 5 + 9 + 7 + 9 = 30, which is greater than
|
|
|
|
! 25.
|
|
|
|
!
|
|
|
|
! How many points can the ant access if it starts at (1000, 1000),
|
|
|
|
! including (1000, 1000) itself?
|
|
|
|
|
|
|
|
: sum-digits ( n -- x )
|
|
|
|
0 swap [ dup zero? ] [ 10 /mod swap [ + ] dip ] until drop ;
|
|
|
|
|
2011-10-02 18:30:25 -04:00
|
|
|
TUPLE: point x y ;
|
|
|
|
C: <point> point
|
2011-09-25 15:25:52 -04:00
|
|
|
|
2011-10-02 18:30:25 -04:00
|
|
|
! USE: alien.c-types
|
|
|
|
! USE: classes.struct
|
|
|
|
! STRUCT: point { x uint } { y uint } ;
|
|
|
|
! : <point> ( x y -- point ) point <struct-boa> ; inline
|
2011-09-25 15:25:52 -04:00
|
|
|
|
|
|
|
: walkable? ( point -- ? )
|
|
|
|
[ x>> ] [ y>> ] bi [ sum-digits ] bi@ + 25 <= ; inline
|
|
|
|
|
2011-10-02 18:30:25 -04:00
|
|
|
:: ant-benchmark ( -- )
|
2011-09-25 15:25:52 -04:00
|
|
|
200000 <hashtable> hash-set boa :> seen
|
|
|
|
100000 <vector> :> stack
|
|
|
|
0 :> total!
|
|
|
|
|
|
|
|
1000 1000 <point> stack push
|
|
|
|
|
|
|
|
[ stack empty? ] [
|
|
|
|
stack pop :> p
|
|
|
|
p seen in? [
|
|
|
|
p seen adjoin
|
|
|
|
p walkable? [
|
|
|
|
total 1 + total!
|
|
|
|
p clone [ 1 + ] change-x stack push
|
|
|
|
p clone [ 1 - ] change-x stack push
|
|
|
|
p clone [ 1 + ] change-y stack push
|
|
|
|
p clone [ 1 - ] change-y stack push
|
|
|
|
] when
|
|
|
|
] unless
|
2011-09-25 22:08:56 -04:00
|
|
|
] until total 148848 assert= ;
|
|
|
|
|
2011-10-02 18:30:25 -04:00
|
|
|
MAIN: ant-benchmark
|