Snippets

marinus PPCGmas

Created by marinus
ppcgmas{
   0::'PPCGmas is cancelled!'

   ⍝ good kids
   good/⍨×3|⊃⎕UCS¨

   ⍝ no good kids, christmas is cancelled
   0=≢good:÷0

   ⍝ gifts
   odd_gifts/⍨2|
   even_gifts~odd_gifts

   ⍝ children
   odd_kidsgood/⍨2|⎕A⍳⊃¨good
   even_kidsgood~odd_kids

   ⍝ distribute gifts among even kids
   distr{=1:0~⍨¨↑⍉↑1=|⍳⍴}
   oddsodd_gifts distr odd_kids
   evenseven_gifts distr even_kids
   giftsodds,evens

   ⍝ take excess gifts away
   max_gifts2+⌊/¨gifts
   giftsgifts⍨¨max_gifts⌊≢¨gifts

   ⍝ if any child has no gifts christmas is cancelled
   0∊≢¨gifts:÷0

   ⍝ empty gift lists for bad children
   gifts_bad  (bad~good)↑⊂

   ⍝ join all kids with all gifts
   kidsodd_kids,even_kids,bad
   giftsgifts,gifts_bad

   ⍝ return kids
   kids,[1.5]gifts
}

Comments (0)