Skip to content

Instantly share code, notes, and snippets.

@halka9000stg
Last active March 23, 2024 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halka9000stg/0896309def1cb9be64bab863c1a88485 to your computer and use it in GitHub Desktop.
Save halka9000stg/0896309def1cb9be64bab863c1a88485 to your computer and use it in GitHub Desktop.
CelebrationElection Math

CelebrationElection: 合格祝賀会における選出

Enter Content と思われる予備校の開催する合格祝賀会において一番大きな巨大数を構成できた参加者が勝ちのゲームで、ルールは次の通り。

import "dart:math";
void main(){
final int voterCount = 30;
final int maxVote = 100;
final int sampleCount = 7;
for(_ in Iterable<int>generate(sampleCount, (int i) => i)){
print(celebrationElectionSample(voterCount, maxVote));
}
}
Iterable<int> celebrationElectionSample(int voterCount, int maxVote, [Random? r]) => celebrationElect(Iterable<int>.generate(voterCount, (_) => (r ?? Random()).nextInt(maxVote)));
Iterable<int> celebrationElect(Iterable<int> votes, {int electCount = 4, int cutbackMargin = 11}) => _celebrationElect(votes..sort(), electCount: electCount, cutbackMargin: cutbackMargin);
Iterable<int> _celebrationElect(Iterable<int> votes, {int electCount = 4, int cutbackMargin = 11}) => votes.where((int v) => v < Iterable<(int, int)>.generate(votes.length - 1, (int i) => (votes[i + 1], votes[i])).where(((int, int) e) => e.$1 - e.$2 >= cutbackMargin).map<int>(((int, int) e) => e.$1).fold<int>(votes.fold<int>(0, (int prev, int curr) => max<int>(prev, curr) + 1), (int prev, int curr) => min<int>(prev, curr))).reversed.take(electCount).reversed;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment