Paste: Ubongo! MK2

Author: yac
Mode: factor
Date: Fri, 5 Aug 2011 13:13:07
Plain Text |
#! /usr/bin/env factor
USING: kernel math math.ranges math.parser math.order
    accessors assocs sequences sorting strings arrays splitting
    io io.files io.encodings.utf8 command-line ;
IN: ubongo

TUPLE: player n oc ;
: <player> ( n oc -- player ) player boa ;

: >players ( lines -- players )
    dup length [1,b] swap zip [ first2 <player> ] map ;

! missing word for this - handcrafted with love ;)
: ubongo' ( oseq seq -- oseq seq )
    dup first 
    [
        dup dup first [ = not ] curry find drop
        dup [ tail ] dip swap [ suffix ] dip
        ubongo'
    ] when ;
: ubongo ( seq -- oseq )
    { } swap natural-sort f suffix ubongo' drop [ >=< ] sort ;

! sorting goodness
: winner' ( plrs -- n )
    [ [ oc>> ] bi@ >=< ] sort
    dup first2 [ oc>> ] bi@ = [ drop 0 ] [ first n>> ] if ;
: winner ( lines -- n )
    [ dup empty?
        [ drop { } ] [ { CHAR: \s } split ubongo ] if
    ] map >players winner' ;

: main ( -- )
    (command-line) rest dup empty? 
        [ drop "Need an input file as only argument" ]
        [ first utf8 file-lines winner number>string ] 
    if print ;

main

New Annotation

Summary:
Author:
Mode:
Body: