Data update

This commit is contained in:
Ingy döt Net 2025-08-11 18:05:26 -07:00
parent 4d5544505c
commit 4924dd0264
3073 changed files with 55820 additions and 4408 deletions

View file

@ -41,8 +41,8 @@ public program()
{
var shuffled_s := s.Shuffled;
console.printLine("The best shuffle of ",s," is ",shuffled_s,"(",shuffled_s.score(s),")")
Console.printLine("The best shuffle of ",s," is ",shuffled_s,"(",shuffled_s.score(s),")")
};
console.readChar()
Console.readChar()
}

View file

@ -0,0 +1,26 @@
USING: arrays combinators.short-circuit formatting kernel random
sequences sequences.extras ;
:: best-shuffle ( str -- str' )
str clone :> new-str
str length :> n
n <iota> >array randomize :> range1
n <iota> >array randomize :> range2
range1 [| i |
range2 [| j |
{
[ i j = ]
[ i new-str nth j new-str nth = ]
[ i str nth j new-str nth = ]
[ i new-str nth j str nth = ]
} 0|| [
i j new-str exchange
] unless
] each
] each
new-str ;
: best-shuffle. ( str -- )
dup best-shuffle 2dup [ = ] 2count "%s, %s, (%d)\n" printf ;

View 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)