Last active
December 16, 2016 16:42
-
-
Save David-Mimnagh/aed03a2e7d8bf3bcd9d4e0a381782a70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.IO; | |
namespace AdventOfCode_Day12 | |
{ | |
class Program | |
{ | |
static List<string> StringSetup(string input) | |
{ | |
List<string> CommandList = new List<string>(); | |
string[] Commands = input.Split('\n'); | |
for (int i = 0; i < Commands.Length; i++) | |
{ | |
Commands[i] = Commands[i].Replace("\r", String.Empty); | |
CommandList.Add(Commands[i]); | |
} | |
return CommandList; | |
} | |
static bool CheckInt(string input) | |
{ | |
try | |
{ | |
int x = Int32.Parse(input); | |
return true; | |
} | |
catch | |
{ | |
return false; | |
} | |
} | |
static void Main(string[] args) | |
{ | |
String input = File.ReadAllText("../../input.txt"); | |
List<string> CommandList = StringSetup(input); | |
int a = 0; | |
int b = 0; | |
int c = 1; | |
int d = 0; | |
string currentCommand = ""; | |
for (int i = 0; i < CommandList.Count; i++) | |
{ | |
//Console.Write("\nRunning... Index: " + i); | |
currentCommand = CommandList[i]; | |
string actualCommand = currentCommand.Substring(0, 4); | |
currentCommand= currentCommand.Replace(actualCommand,String.Empty); | |
string xCommand = ""; | |
string yCommand = ""; | |
if (currentCommand.Length > 1) | |
{ | |
xCommand = currentCommand.Substring(0, currentCommand.IndexOf(" ")); | |
currentCommand = currentCommand.Replace(xCommand, String.Empty); | |
yCommand = currentCommand.Substring(0); | |
actualCommand = actualCommand.Trim(); | |
xCommand = xCommand.Trim(); | |
yCommand = yCommand.Trim(); | |
}else | |
{ | |
actualCommand = actualCommand.Trim(); | |
xCommand = currentCommand; | |
} | |
switch(actualCommand) | |
{ | |
case "cpy": | |
{ | |
if(CheckInt(xCommand)) | |
{ | |
int xValue = Int32.Parse(xCommand); | |
if (yCommand.Contains("a")) | |
a = xValue; | |
else if (yCommand.Contains("b")) | |
b = xValue; | |
else if (yCommand.Contains("c")) | |
c = xValue; | |
else if (yCommand.Contains("d")) | |
d = xValue; | |
} | |
else | |
{ | |
if (xCommand.Contains("a")) | |
{ | |
if (yCommand.Contains("b")) | |
b = a; | |
else if (yCommand.Contains("c")) | |
c = a; | |
else if (yCommand.Contains("d")) | |
d = a; | |
} | |
else if (xCommand.Contains("b")) | |
{ | |
if (yCommand.Contains("a")) | |
a = b; | |
else if (yCommand.Contains("c")) | |
c = b; | |
else if (yCommand.Contains("d")) | |
d = b; | |
} | |
else if (xCommand.Contains("c")) | |
{ | |
if (yCommand.Contains("a")) | |
a = c; | |
else if (yCommand.Contains("b")) | |
b = c; | |
else if (yCommand.Contains("d")) | |
d = c; | |
} | |
else if (xCommand.Contains("d")) | |
{ | |
if (yCommand.Contains("a")) | |
a = d; | |
else if (yCommand.Contains("b")) | |
b = d; | |
else if (yCommand.Contains("c")) | |
c = d; | |
} | |
} | |
break; | |
} | |
case "jnz": | |
{ | |
int yValue = Int32.Parse(yCommand); | |
if (CheckInt(xCommand)) | |
{ | |
int xValue = Int32.Parse(xCommand); | |
if (xValue > 0) | |
{ | |
i += yValue-1; | |
} | |
} | |
else | |
{ | |
if (xCommand.Contains("a")) | |
{ | |
if (a > 0) | |
i += yValue - 1; | |
} | |
else if (xCommand.Contains("b")) | |
{ | |
if (b > 0) | |
i += yValue - 1; | |
} | |
else if (xCommand.Contains("c")) | |
{ | |
if (c > 0) | |
i += yValue - 1; | |
} | |
else if (xCommand.Contains("d")) | |
{ | |
if (d > 0) | |
i += yValue - 1; | |
} | |
} | |
break; | |
} | |
case "inc": | |
{ | |
if (xCommand.Contains("a")) | |
a += 1; | |
else if (xCommand.Contains("b")) | |
b += 1; | |
else if (xCommand.Contains("c")) | |
c += 1; | |
else if (xCommand.Contains("d")) | |
d += 1; | |
break; | |
} | |
case "dec": | |
{ | |
if (xCommand.Contains("a")) | |
a -= 1; | |
else if (xCommand.Contains("b")) | |
b -= 1; | |
else if (xCommand.Contains("c")) | |
c -= 1; | |
else if (xCommand.Contains("d")) | |
d -= 1; | |
break; | |
} | |
} | |
//Console.Write("\nA: " + a + "\nB: " + b + "\nC: " + c + "\nD: " + d); | |
} | |
Console.Write("\n\n\tComplete. The value of 'a' is: " + a); | |
Console.Read(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment