Program.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace D14._2
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. if (args.Length == 0) return;
  12. string inputStr = args[0];
  13. int [] input = inputStr.Select(c => c - '0').ToArray();
  14. int ccount = 0;
  15. int count = 2;
  16. int elf1 = 0;
  17. int elf2 = 1;
  18. var recipes = new List<int>() { 3, 7 };
  19. do
  20. {
  21. int nrecipe = recipes[elf1] + recipes[elf2];
  22. if (nrecipe < 10)
  23. {
  24. recipes.Add(nrecipe);
  25. CheckInput(input, ref ccount, ref count, nrecipe);
  26. }
  27. else
  28. {
  29. foreach (var nr in new[] { 1, nrecipe - 10 })
  30. {
  31. recipes.Add(nr);
  32. CheckInput(input, ref ccount, ref count, nr);
  33. if (ccount == input.Length) break;
  34. }
  35. }
  36. elf1 = (elf1 + 1 + recipes[elf1]) % recipes.Count;
  37. elf2 = (elf2 + 1 + recipes[elf2]) % recipes.Count;
  38. } while (ccount != input.Length);
  39. Console.WriteLine(count.ToString());
  40. }
  41. private static void CheckInput(int[] input, ref int ccount, ref int count, int nrecipe)
  42. {
  43. if (input[ccount] == nrecipe) ccount++;
  44. else if (input[0] == nrecipe)
  45. {
  46. count += ccount;
  47. ccount = 1;
  48. }
  49. else
  50. {
  51. count += ccount + 1;
  52. ccount = 0;
  53. }
  54. }
  55. }
  56. }