Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace AOC09
- {
- class IntcodeComputer
- {
- public Dictionary<int,int> Program;
- public int instructionPointer = -2;
- public List<int> Inputs;
- public bool PhaseUsed = false;
- public int RelativeBase = 0;
- public IntcodeComputer(int[] program)
- {
- Program = new Dictionary<int, int>();
- var i = 0;
- foreach (var p in program)
- {
- Program.Add(i, p);
- i++;
- }
- Inputs = new List<int>();
- }
- public int Parse()
- {
- instructionPointer += 2;
- while (true)
- {
- var current = Program[instructionPointer];
- var currentS = current.ToString("D5");
- var opcode = current % 100;
- var modes = currentS.Substring(0, currentS.Length - 2).Select(d => int.Parse(d.ToString())).ToList();
- modes.Reverse();
- switch (opcode)
- {
- case 1: // a * b at c
- var a = GetParameter(0);
- var b = GetParameter(1);
- var c = GetValue(instructionPointer + 3);
- SetValue(c, a + b);
- instructionPointer += 4;
- break;
- case 2:
- a = GetParameter(0);
- b = GetParameter(1);
- c = GetValue(instructionPointer + 3);
- SetValue(c, a * b);
- instructionPointer += 4;
- break;
- case 3:
- SetValue(GetValue(instructionPointer + 1), Inputs[0]);
- Inputs.RemoveAt(0);
- instructionPointer += 2;
- break;
- case 4:
- return GetParameter(0);
- case 5:
- if (GetParameter(0) != 0)
- {
- instructionPointer = GetParameter(1);
- }
- else
- {
- instructionPointer += 3;
- }
- break;
- case 6:
- if (GetParameter(0) == 0)
- {
- instructionPointer = GetParameter(1);
- }
- else
- {
- instructionPointer += 3;
- }
- break;
- case 7:
- SetValue(GetValue(instructionPointer + 3), GetParameter(0) < GetParameter(1) ? 1 : 0);
- instructionPointer += 4;
- break;
- case 8:
- SetValue(GetValue(instructionPointer + 3), GetParameter(0) == GetParameter(1) ? 1 : 0);
- instructionPointer += 4;
- break;
- case 99:
- return int.MaxValue;
- case 9:
- RelativeBase = GetParameter(0);
- instructionPointer += 2;
- break;
- default:
- instructionPointer++;
- break;
- }
- //Gets a value from memory at the given index.
- int GetValue(int index)
- {
- if (Program.ContainsKey(index)) { return Program[index]; }
- return 0;
- }
- //Sets a value in memory at the given index.
- void SetValue(int index, int value)
- {
- if (Program.ContainsKey(index)) { Program[index] = value; }
- else { Program.Add(index, value); }
- }
- //Gets a value from memory based on the given parameter mode and parameter index.
- int GetParameter(int i)
- {
- switch (modes[i])
- {
- case 0:
- return GetValue(GetValue(instructionPointer + i + 1));
- case 1:
- return GetValue(instructionPointer + i + 1);
- case 2:
- return GetValue(GetValue(instructionPointer + i + 1) + RelativeBase);
- default:
- Console.WriteLine("ERROR: INVALID PARAMETER MODE. MUST BE 0,1,2");
- return 0;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement