namofure

AIプログラミング③

Dec 24th, 2025
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.02 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4.  
  5. class Program
  6. {
  7.     const uint A = 0x41C64E6D;
  8.     const uint C = 0x3039;
  9.     const int InitialConsume = 2;   //事前消費
  10.     const int CompareCount = 10;
  11.  
  12.     static void Main()
  13.     {
  14.         Console.WriteLine("DQMJ2P 初期SEED絞り込みツール");
  15.         Console.WriteLine();
  16.  
  17.         Console.Write("起動時間 (yyyy/MM/dd HH:mm:ss) 空欄 = 現在時刻: ");
  18.         string timeInput = Console.ReadLine();
  19.  
  20.         DateTime baseTime;
  21.         if (string.IsNullOrWhiteSpace(timeInput))
  22.         {
  23.             baseTime = DateTime.Now;
  24.         }
  25.         else
  26.         {
  27.             baseTime = DateTime.ParseExact(
  28.                 timeInput,
  29.                 "yyyy/MM/dd HH:mm:ss",
  30.                 CultureInfo.InvariantCulture
  31.             );
  32.         }
  33.  
  34.         Console.Write("観測値 (0~6 を10個まで、スペース区切り): ");
  35.         string valueInput = Console.ReadLine();
  36.  
  37.         string[] tokens = valueInput.Split(' ', StringSplitOptions.RemoveEmptyEntries); //観測値のリスト
  38.  
  39.         List<int> observed = new List<int>();
  40.         for (int i = 0; i < tokens.Length && i < CompareCount; i++)
  41.         {
  42.             observed.Add(int.Parse(tokens[i]));
  43.         }
  44.  
  45.         Console.WriteLine();
  46.         Console.WriteLine("Searching...");
  47.  
  48.         List<(uint init, uint current)> results = new List<(uint, uint)>();
  49.  
  50.         for (int sec = -60; sec <= 60; sec++)   //±1分の範囲
  51.         {
  52.             DateTime t = baseTime.AddSeconds(sec);
  53.             uint initSeed = TimeToSeed(t);
  54.  
  55.             uint seed = initSeed;
  56.  
  57.             for (int i = 0; i < InitialConsume; i++)
  58.             {
  59.                 seed = Next(seed);
  60.             }
  61.  
  62.             bool match = true;
  63.             uint current = seed;
  64.  
  65.             for (int i = 0; i < observed.Count; i++)
  66.             {
  67.                 current = Next(current);
  68.                 int value = (int)((current >> 16) % 7);
  69.  
  70.                 if (value == observed[i]) {}
  71.                 else
  72.                 {
  73.                     match = false;
  74.                     break;
  75.                 }
  76.             }
  77.  
  78.             if (match)
  79.             {
  80.                 results.Add((initSeed, current));
  81.             }
  82.         }
  83.  
  84.         Console.WriteLine();
  85.         Console.WriteLine("Found " + results.Count + " candidates");
  86.  
  87.         for (int i = 0; i < results.Count; i++)
  88.         {
  89.             Console.WriteLine(
  90.                 "InitSeed=0x" + results[i].init.ToString("X8") +
  91.                 "  CurrentSeed=0x" + results[i].current.ToString("X8"));
  92.         }
  93.     }
  94.  
  95.     static uint Next(uint seed)
  96.     {
  97.         return unchecked(A * seed + C);
  98.     }
  99.  
  100.     static uint TimeToSeed(DateTime time)
  101.     {
  102.         DateTime baseTime = new DateTime(
  103.             2000, 1, 1, 0, 0, 0,
  104.             DateTimeKind.Local
  105.         );
  106.  
  107.         TimeSpan span = time - baseTime;
  108.         long seconds = (long)span.TotalSeconds;
  109.  
  110.         return (uint)seconds;
  111.     }
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment