Data update
This commit is contained in:
parent
4d5544505c
commit
4924dd0264
3073 changed files with 55820 additions and 4408 deletions
44
Task/Best-shuffle/YAMLScript/best-shuffle.ys
Normal file
44
Task/Best-shuffle/YAMLScript/best-shuffle.ys
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
!YS-v0
|
||||
|
||||
defn main(input):
|
||||
say: read-string(input)
|
||||
.map(best-shuffle):vec
|
||||
|
||||
defn best-shuffle(s):
|
||||
ref =: s:group-indices:cycles
|
||||
prm =: concat.apply(map(partial(rotate 1) ref))
|
||||
ref =: concat(ref*)
|
||||
|
||||
map(vector ref prm):
|
||||
.sort-by(first _)
|
||||
.map(second)
|
||||
.map(partial(get s))
|
||||
.str(*)
|
||||
.call(\([s _ s.score(_)]) _)
|
||||
|
||||
defn score(before after):
|
||||
map(== before after)
|
||||
.filter(true?).#
|
||||
|
||||
defn group-indices(s):
|
||||
map-indexed(vector s)
|
||||
.merge-vecs({}):vals
|
||||
.sort-by(count _):reverse
|
||||
|
||||
defn cycles(coll):
|
||||
n =: coll.0.#
|
||||
cycle =: range(n):cycle
|
||||
coll =: concat(coll*)
|
||||
map(vector coll cycle):
|
||||
.merge-vecs([])
|
||||
|
||||
defn rotate(n coll):
|
||||
c =: coll.#
|
||||
n =: (c + n) % c
|
||||
concat coll.drop(n): coll.take(n)
|
||||
|
||||
defn merge-vecs(vecs init):
|
||||
reduce _ init vecs:
|
||||
fn(counts [index x]):
|
||||
assoc counts x:
|
||||
counts.$x.or([]).conj(index)
|
||||
Loading…
Add table
Add a link
Reference in a new issue