1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
//http://codegolf.stackexchange.com/a/70115/42682
module census
import StdEnv
//Start = v 538 [("CA",38000000.0), ("NH",1300000.0)]
//Start = v 2 [("A",1.0)]
//Start = v 538 [("DC", 1000000.0), ("RH", 1.0)]
Start=votes 100 [("A",12.0),("B",8.0),("C",3.0),("D",0.0)]
f=fst
d=snd
q=sqrt
l=length
v n s#d=filter(((==)"DC")o f)s
=sortBy(\(a,b)(c,d).b>d)([(t,3.0)\\t<-d]++w(n-3*(l s))[(t,1.0)\\t<-removeMembers s d])
w 0 s=map(\(p,r).(p,r+2.0))s
w n s#s=sortBy(\a b.A a>A b)s
#(p,r)=hd s
=w(n-1)[(p,r+1.0):tl s]
A ((_,p),r)=p/q(r*r+r)
votes n states
# dc = filter (((==)"DC")o fst) states
= sortBy (\(a,b)(c,d).b>d) ([(t,3.0) \\ t <- dc] ++ votes` (n-3*length states) [(t,1.0)\\t<-removeMembers states dc])
where
votes` 0 states = map (\(p,r).(p,r+2.0)) states
votes` n states
# states = sortBy (\a b.A a > A b) states
# (p,r) = hd states
= votes` (n-1) [(p,r+1.0):tl states]
A ((_,p),r) = p / sqrt(r*r+r)
|