Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net.Http;
- using System.Threading;
- using System.Threading.Tasks;
- namespace PMPHF005_P99J7Y
- {
- internal class PMPHF005_P99J7Y
- {
- static int GetOperandValue(string operand, int[] registers)
- {
- if (Char.IsLetter(operand[0]))
- {
- return registers[operand[0] - 'A'];
- }
- else
- {
- return int.Parse(operand);
- }
- }
- static void Main(string[] args)
- {
- int[] registers = new int[4];
- string[] lines = File.ReadAllLines("input.txt");
- string[] initialRegisters = lines[0].Split(',');
- for (int i = 0; i < 4; i++)
- {
- registers[i] = int.Parse(initialRegisters[i]);
- }
- int currentLine = 1;
- while (currentLine < lines.Length)
- {
- string line = lines[currentLine];
- string[] parts = line.Split(' ');
- string command = parts[0];
- if (command == "MOV")
- {
- char destReg = parts[1][0];
- string src = parts[2];
- int value = GetOperandValue(src, registers);
- registers[destReg - 'A'] = value;
- }
- else if (command == "ADD")
- {
- char destReg = parts[1][0];
- string src1 = parts[2];
- string src2 = parts[3];
- int value1 = GetOperandValue(src1, registers);
- int value2 = GetOperandValue(src2, registers);
- registers[destReg - 'A'] = value1 + value2;
- }
- else if (command == "SUB")
- {
- char destReg = parts[1][0];
- string src1 = parts[2];
- string src2 = parts[3];
- int value1 = GetOperandValue(src1, registers);
- int value2 = GetOperandValue(src2, registers);
- registers[destReg - 'A'] = value1 - value2;
- }
- else if (command == "JNE")
- {
- int jumpLine = int.Parse(parts[1]);
- char srcReg1 = parts[2][0];
- string srcReg2 = parts[3];
- int value1 = registers[srcReg1 - 'A'];
- int value2 = GetOperandValue(srcReg2, registers);
- if (value1 != value2)
- {
- currentLine = jumpLine;
- }
- }
- currentLine++;
- }
- string output = string.Join(",", registers);
- File.WriteAllText("output.txt", output);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement