#! /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 ; : ( n oc -- player ) player boa ; : >players ( lines -- players ) dup length [1,b] swap zip [ first2 ] 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