Skip to content

Instantly share code, notes, and snippets.

@quangnle
Created May 10, 2016 10:25
Show Gist options
  • Save quangnle/7acbd4b7e367d23b0a738c464d11a8b7 to your computer and use it in GitHub Desktop.
Save quangnle/7acbd4b7e367d23b0a738c464d11a8b7 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tuenti3
{
class Program
{
class Instruction
{
public string Name { get; set; }
public string Param { get; set; }
}
class Command
{
public string Name { get; set; }
public List<Instruction> Instructions { get; set; }
}
class State
{
public string Name { get; set; }
public List<Command> Commands { get; set; }
}
class Script
{
public List<State> CodeSegment { get; set; }
public List<string> TapeSegment { get; set; }
public string Execute(string input)
{
var sbInput = new StringBuilder(input);
var curState = CodeSegment[0];
var curCmdName = "";
var isEnd = false;
var pointer = 0;
while (!isEnd)
{
if (pointer < 0 || pointer >= sbInput.Length)
{
if (pointer < 0)
{
pointer = 0;
sbInput.Insert(0, " ");
}
else
{
sbInput.Append(" ");
pointer = sbInput.Length - 1;
}
}
curCmdName = "'" + sbInput[pointer] + "'";
var curCmd = curState.Commands.FirstOrDefault(cmd => cmd.Name == curCmdName);
for (int j = 0; j < curCmd.Instructions.Count; j++)
{
var inst = curCmd.Instructions[j];
if (inst.Name == "move")
{
if (inst.Param == "right")
pointer++;
else
pointer--;
}
else if (inst.Name == "state")
{
if (inst.Param == "end")
isEnd = true;
else
curState = CodeSegment.FirstOrDefault(s => s.Name == inst.Param);
}
else
{
var val = inst.Param.Replace("'", "");
if (pointer < sbInput.Length)
sbInput.Replace(sbInput[pointer].ToString(), val, pointer, 1);
else
sbInput.Append(val);
}
}
}
return sbInput.ToString();
}
}
static void Main(string[] args)
{
var script = LoadScript("submitInput.txt");
var result = "";
var i = 0;
foreach (var input in script.TapeSegment)
{
i++;
result += "Tape #" + i.ToString() + ": "+ script.Execute(input) + "\n";
}
File.WriteAllText("submitOutput.txt", result);
}
static Script LoadScript(string path)
{
Script script = null;
var lines = File.ReadAllLines(path);
var lineIdx = 0;
State curState = null;
Command curCmd = null;
var inTapeSection = false;
var isEnd = false;
while (!isEnd)
{
var line = lines[lineIdx];
if (line == "---")
script = new Script();
else if (line == "...")
isEnd = true;
else if (line == "code:")
{
script.CodeSegment = new List<State>();
}
else if (line == "tapes:")
{
script.TapeSegment = new List<string>();
inTapeSection = true;
}
else
{
var prefix = line.Substring(0, 6);
if (prefix == " ")
{
var instruction = new Instruction();
instruction.Name = line.Trim().Split(':')[0].Trim();
instruction.Param = line.Trim().Split(':')[1].Trim();
curCmd.Instructions.Add(instruction);
}
else
{
prefix = line.Substring(0, 4);
if (prefix == " ")
{
curCmd = new Command();
curCmd.Name = line.Trim().Replace(":","");
curCmd.Instructions = new List<Instruction>();
curState.Commands.Add(curCmd);
}
else
{
prefix = line.Substring(0, 2);
if (prefix == " ")
{
if (inTapeSection)
{
var input = line.Split(':')[1].Trim().Replace("'", "");
script.TapeSegment.Add(input);
}
else
{
curState = new State();
curState.Name = line.Trim().Replace(":", "");
curState.Commands = new List<Command>();
script.CodeSegment.Add(curState);
}
}
}
}
}
lineIdx++;
}
return script;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment