Skip to content

Instantly share code, notes, and snippets.

@aetos382
Last active August 2, 2022 05:10
Show Gist options
  • Save aetos382/f9f734e10b4072cdec00626cf37f781f to your computer and use it in GitHub Desktop.
Save aetos382/f9f734e10b4072cdec00626cf37f781f to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
const int MaxAttempts = 100;
foreach (var s in GenerateDdsk().DoWhile(x => !x.LoveInjected).Take(MaxAttempts).Select(x => x.Ddsk))
{
Console.WriteLine(s);
}
static IEnumerable<DdskResult> GenerateDdsk()
{
foreach (var d in GenerateRandom(2).Select(ToDdsk).Window(4).Select(DdskUnit.Create))
{
var s = string.Create(
2 * 4 + (d.IsDdsk ? 5 : 0),
d,
static (span, d) => {
foreach (var e in d.Value)
{
var s = ToDdskString(e);
s.CopyTo(span);
span = span.Slice(s.Length);
}
if (d.IsDdsk)
{
"ラブ注入♡".CopyTo(span);
}
});
yield return new DdskResult(s, d.IsDdsk);
}
}
static IEnumerable<int> GenerateRandom(
int maxExclusive)
{
if (maxExclusive < 0)
{
throw new ArgumentOutOfRangeException(nameof(maxExclusive));
}
while (true)
{
yield return Random.Shared.Next(maxExclusive);
}
}
static Ddsk ToDdsk(
int i)
{
return i switch {
0 => Ddsk.Dd,
1 => Ddsk.Sk,
_ => throw new ArgumentOutOfRangeException(nameof(i))
};
}
static string ToDdskString(
Ddsk ddsk)
{
return ddsk switch {
Ddsk.Dd => "ドド",
Ddsk.Sk => "スコ",
_ => throw new ArgumentOutOfRangeException(nameof(ddsk))
};
}
enum Ddsk
{
Dd,
Sk
}
readonly struct DdskUnit
{
public DdskUnit(
IReadOnlyList<Ddsk> a)
{
ArgumentNullException.ThrowIfNull(a);
this.Value = a.ToArray();
this.IsDdsk = a is [Ddsk.Dd, Ddsk.Sk, Ddsk.Sk, Ddsk.Sk];
}
public IReadOnlyList<Ddsk> Value { get; }
public bool IsDdsk { get; }
public static DdskUnit Create(
IReadOnlyList<Ddsk> a)
{
return new DdskUnit(a);
}
}
readonly record struct DdskResult(
string Ddsk,
bool LoveInjected);
static class EnumerableExtensions
{
public static IEnumerable<IReadOnlyList<T>> Window<T>(
this IEnumerable<T> source,
int count)
{
ArgumentNullException.ThrowIfNull(source);
if (count < 0)
{
throw new ArgumentOutOfRangeException(nameof(count));
}
var result = new Queue<T>(count);
using var e = source.GetEnumerator();
for (int i = 0; i < count && e.MoveNext(); ++i)
{
result.Enqueue(e.Current);
}
yield return result.ToArray();
while (e.MoveNext())
{
result.Dequeue();
result.Enqueue(e.Current);
yield return result.ToArray();
}
}
public static IEnumerable<T> DoWhile<T>(
this IEnumerable<T> source,
Func<T, bool> predicate)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(predicate);
foreach (var e in source)
{
yield return e;
if (!predicate(e))
{
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment