~pmikkelsen/dyalog-competition-2022

ref: ab47f9cd8c7f00b2cb4daec8c9c3dde413c6820a dyalog-competition-2022/vote.apln -rw-r--r-- 940 bytes
ab47f9cdPeter Mikkelsen Add vote problem 9 months ago
                                                                                
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
:Namespace vote
      Ballot{
          voter{
              ⍺:?
              n?
              (n)@(n↑?)0
          }
          ,⍨∘(>0)voter¨⍴|
      }
      IRV{
           IRVruns (⍋⌷⊢)[;1]{,+}⌸⍨1⍨¨[;2]
      }
      IRVruns{
          eliminate{
              ⍝ A loser rating of 0 is worst,
              ⍝ so give it a large value
              res[;1],⍪↓⍉0@⊢⍉r-r>1 0(,r)@(=0)(r[;2])[;]
              ⍝ remove the blank votes now
              res⌿⍨~∨/res0⊃⌽⍴r
          }
          1=≢⍵: ⍝ We have a winner
          losers[(⍸⌊⍷⊢)[;2];1]
          =losers: ⍝ it was a tie
          (),IRVeliminatelosers,⊂ ⍝ Calculate next rounds with the loses removed
      }
:EndNamespace