! Copyright (c) 2007 Aaron Schaefer.
! See http://factorcode.org/license.txt for BSD license.
-USING: io.files kernel math math.parser namespaces sequences sorting splitting
- strings system vocabs ascii ;
+USING: ascii io.files kernel math project-euler.common sequences sorting splitting ;
IN: project-euler.022
! http://projecteuler.net/index.php?section=problems&id=22
"extra/project-euler/022/names.txt" resource-path
file-contents [ quotable? ] subset "," split ;
-: alpha-value ( str -- n )
- [ string>digits sum ] keep length 9 * - ;
-
: name-scores ( seq -- seq )
dup length [ 1+ swap alpha-value * ] 2map ;
source-022 natural-sort name-scores sum ;
! [ euler022 ] 100 ave-time
-! 59 ms run / 1 ms GC ave time - 100 trials
+! 123 ms run / 4 ms GC ave time - 100 trials
! source-022 [ natural-sort name-scores sum ] curry 100 ave-time
-! 45 ms run / 1 ms GC ave time - 100 trials
+! 93 ms run / 2 ms GC ave time - 100 trials
MAIN: euler022
USING: arrays combinators.lib kernel math math.functions math.miller-rabin
math.matrices math.parser math.primes.factors math.ranges namespaces
- sequences sorting ;
+ sequences sorting unicode.case ;
IN: project-euler.common
! A collection of words used by more than one Project Euler solution
! Problems using each public word
! -------------------------------
+! alpha-value - #22, #42
! cartesian-product - #4, #27, #29, #32, #33
! collect-consecutive - #8, #11
! log10 - #25, #134
PRIVATE>
+: alpha-value ( str -- n )
+ >lower [ CHAR: a - 1+ ] sigma ;
+
: cartesian-product ( seq1 seq2 -- seq1xseq2 )
swap [ swap [ 2array ] map-with ] map-with concat ;