NB. packages in the sleigh http://adventofcode.com/day/24 p=: \:~".(LF,' ')charsub CR-.~1!:1{:ARGV comb=: 4 : 0 NB. http://www.jsoftware.com/jwiki/Essays/Combinations k=. i.>:d=.y-x z=. (d$ ,&.>/\. >:&.> z end. ; z ) test=: 4 : 0 NB. x - number of groups w=. +/y NB. total weight t=. <.w%x NB. target weight of each group assert w=t*x NB. i.e. w is divisible by 3 n=.>:+/t>+/\y NB. min number of items in a group m=.<.x%~#y NB. max number of items in a group - 1/x-th of all for_k. n+i.>:m-n do. c=. k comb #y NB. n..m comb #y s=. c#~t=+/"1 c{y NB. it would be good to check if other parts are ok... if. 0<#s do. <./*/"1 s{y return. end. NB. but some other time :) end. 0 ) echo 3 test p NB. 10723906903 - works good with downsorted, bad otherwise echo 4 test p NB. 74850409 exit 0