Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace AOC09
  8. {
  9. class IntcodeComputer
  10. {
  11. public Dictionary<int,int> Program;
  12. public int instructionPointer = -2;
  13. public List<int> Inputs;
  14. public bool PhaseUsed = false;
  15. public int RelativeBase = 0;
  16.  
  17. public IntcodeComputer(int[] program)
  18. {
  19. Program = new Dictionary<int, int>();
  20. var i = 0;
  21. foreach (var p in program)
  22. {
  23. Program.Add(i, p);
  24. i++;
  25. }
  26. Inputs = new List<int>();
  27. }
  28.  
  29. public int Parse()
  30. {
  31. instructionPointer += 2;
  32.  
  33. while (true)
  34. {
  35. var current = Program[instructionPointer];
  36. var currentS = current.ToString("D5");
  37. var opcode = current % 100;
  38. var modes = currentS.Substring(0, currentS.Length - 2).Select(d => int.Parse(d.ToString())).ToList();
  39. modes.Reverse();
  40.  
  41. switch (opcode)
  42. {
  43. case 1: // a * b at c
  44. var a = GetParameter(0);
  45. var b = GetParameter(1);
  46. var c = GetValue(instructionPointer + 3);
  47. SetValue(c, a + b);
  48. instructionPointer += 4;
  49. break;
  50. case 2:
  51. a = GetParameter(0);
  52. b = GetParameter(1);
  53. c = GetValue(instructionPointer + 3);
  54. SetValue(c, a * b);
  55. instructionPointer += 4;
  56. break;
  57. case 3:
  58. SetValue(GetValue(instructionPointer + 1), Inputs[0]);
  59. Inputs.RemoveAt(0);
  60. instructionPointer += 2;
  61. break;
  62. case 4:
  63. return GetParameter(0);
  64. case 5:
  65. if (GetParameter(0) != 0)
  66. {
  67. instructionPointer = GetParameter(1);
  68. }
  69. else
  70. {
  71. instructionPointer += 3;
  72. }
  73. break;
  74. case 6:
  75. if (GetParameter(0) == 0)
  76. {
  77. instructionPointer = GetParameter(1);
  78. }
  79. else
  80. {
  81. instructionPointer += 3;
  82. }
  83. break;
  84. case 7:
  85. SetValue(GetValue(instructionPointer + 3), GetParameter(0) < GetParameter(1) ? 1 : 0);
  86. instructionPointer += 4;
  87. break;
  88. case 8:
  89. SetValue(GetValue(instructionPointer + 3), GetParameter(0) == GetParameter(1) ? 1 : 0);
  90. instructionPointer += 4;
  91. break;
  92. case 99:
  93. return int.MaxValue;
  94. case 9:
  95. RelativeBase = GetParameter(0);
  96. instructionPointer += 2;
  97. break;
  98. default:
  99. instructionPointer++;
  100. break;
  101. }
  102.  
  103. //Gets a value from memory at the given index.
  104. int GetValue(int index)
  105. {
  106. if (Program.ContainsKey(index)) { return Program[index]; }
  107. return 0;
  108. }
  109.  
  110. //Sets a value in memory at the given index.
  111. void SetValue(int index, int value)
  112. {
  113. if (Program.ContainsKey(index)) { Program[index] = value; }
  114. else { Program.Add(index, value); }
  115. }
  116.  
  117. //Gets a value from memory based on the given parameter mode and parameter index.
  118. int GetParameter(int i)
  119. {
  120. switch (modes[i])
  121. {
  122. case 0:
  123. return GetValue(GetValue(instructionPointer + i + 1));
  124. case 1:
  125. return GetValue(instructionPointer + i + 1);
  126. case 2:
  127. return GetValue(GetValue(instructionPointer + i + 1) + RelativeBase);
  128. default:
  129. Console.WriteLine("ERROR: INVALID PARAMETER MODE. MUST BE 0,1,2");
  130. return 0;
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement