Skip to content

Instantly share code, notes, and snippets.

@David-Mimnagh
Created December 15, 2016 09:13
Show Gist options
  • Save David-Mimnagh/72ad817274b16ac04386bb142a85bd26 to your computer and use it in GitHub Desktop.
Save David-Mimnagh/72ad817274b16ac04386bb142a85bd26 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace AdventOfCode_Day10
{
class Program
{
const int BOTCOUNT = 215;
static List<Bot> readyBots = new List<Bot>();
static List<int> readyBotsNum = new List<int>();
public class Bot
{
public int Low { get; set; }
public int High { get; set; }
public int DestLow {get;set;}
public int DestHigh {get;set;}
public int OutDestLow {get;set;}
public int OutDestHigh {get;set;}
public Bot(int low, int high)
{
Low = low; High = high;
}
}
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 void GiveValueToBot(string command, List<Bot> botList)
{
int start = command.IndexOf("value ") + "value ".Length;
int to = command.IndexOf(" goes") - " goes".Length;
int valueToGive = Int32.Parse(command.Substring(start, to ));
start = command.IndexOf("bot ") + "bot ".Length;
int botToGiveTo = Int32.Parse(command.Substring(start));
int currentLow = botList[botToGiveTo].Low;
int currentHigh = botList[botToGiveTo].High;
if (valueToGive < currentHigh)
botList[botToGiveTo].Low = valueToGive;
else
botList[botToGiveTo].High = valueToGive;
}
static void AssignBotDestValues(string command, List<Bot> botList)
{
int start = "bot ".Length;
int to = command.IndexOf(" ", start);
int botGivingID = Int32.Parse(command.Substring(start, to - start));
string firstWord = null;
start = command.IndexOf("low to ") + "low to ".Length;
to = command.LastIndexOf(" and high");
string lowValTo = command.Substring(start, to - start);
firstWord = lowValTo.Substring(0, lowValTo.IndexOf(" "));
switch (firstWord)
{
case "bot":
{
lowValTo = lowValTo.Replace(firstWord, String.Empty);
int botGettingID = Int32.Parse(lowValTo);
botList[botGivingID].DestLow = botGettingID;
}
break;
case "output":
{
lowValTo = lowValTo.Replace(firstWord, String.Empty);
int outputID = Int32.Parse(lowValTo);
botList[botGivingID].OutDestLow = outputID;
}
break;
default: { } break;
}
start = command.LastIndexOf("high to ") + "high to ".Length;
string highValTo = command.Substring(start);
firstWord = highValTo.Substring(0, highValTo.IndexOf(" "));
switch (firstWord)
{
case "bot":
{
highValTo = highValTo.Replace(firstWord, String.Empty);
int botGettingID = Int32.Parse(highValTo);
botList[botGivingID].DestHigh = botGettingID;
}
break;
case "output":
{
highValTo = highValTo.Replace(firstWord, String.Empty);
int outputID = Int32.Parse(highValTo);
botList[botGivingID].OutDestHigh = outputID;
}
break;
default: { } break;
}
}
static void checkForTwoChips(List<Bot> botList)
{
for (int i = 0; i < botList.Count; i++)
{
if (botList[i].High != 0 && botList[i].Low != 0)
{
if (botList[i].DestHigh != 0 && botList[i].DestLow != 0)
{
if (botList[i].Low == 17 && botList[i].High == 61)
Console.Write("FOUND THE BOT: " + i);
readyBots.Add(botList[i]);
readyBotsNum.Add(i);
}
}
}
}
static void MoveValues(Bot currBot, int currLow, int currHigh,int destLow, int destHigh, List<Bot>botList, int[] outputs)
{
if (currBot.OutDestLow == 0)
{
if (currBot.OutDestLow == 0)
{
List<int> numbers = new List<int>() { botList[destLow].High, botList[destLow].Low, currLow };
numbers.Sort();
botList[destLow].Low = numbers[1];
botList[destHigh].High = numbers[2];
numbers = new List<int>() { botList[destLow].High, botList[destLow].Low, currHigh };
numbers.Sort();
botList[destLow].Low = numbers[1];
botList[destHigh].High = numbers[2];
currBot.Low = -1;
currBot.High = -1;
}
else
{
outputs[currBot.OutDestHigh] = currBot.High;
currBot.High = -1;
}
}
else{
outputs[currBot.OutDestLow] = currBot.Low;
currBot.Low = -1;
}
}
static void TraverseCommands(List<string> commandList, List<Bot> botList, int[] outputs)
{
for (int i = 0; i < commandList.Count; i++)
{
if(i>0)
if (commandList[i-1].Contains("value"))
{
GiveValueToBot(commandList[i-1], botList);
commandList.Remove(commandList[i-1]);
}
if(commandList[i].Contains("value"))
{
GiveValueToBot(commandList[i], botList);
commandList.Remove(commandList[i]);
}
}
for (int i = 0; i < commandList.Count; i++)
{
AssignBotDestValues(commandList[i], botList);
}
//foreach(string command in commandList){
//Apply instruction to relevant bots
checkForTwoChips(botList);
while (readyBots.Count() > 0)
{
Console.Write("\nChecking");
for (int i = readyBots.Count - 1; i > -1; i--)
{
MoveValues(readyBots[i], readyBots[i].Low, readyBots[i].High, readyBots[i].DestLow, readyBots[i].DestHigh, botList, outputs);
readyBots.RemoveAt(i);
readyBotsNum.RemoveAt(i);
}
checkForTwoChips(botList);
}
}
static int FindBot(int low, int high, List<Bot> botList)
{
for(int i =0;i<botList.Count;i++)
{
if (botList[i].Low == low)
if (botList[i].High == high)
return i;
}
return 0;
}
static void Main(string[] args)
{
String input = File.ReadAllText("../../input.txt");
List<string> Commands = StringSetup(input);
int[] outputs = new int[30];
List<Bot> BotList = new List<Bot>();
Bot newBot = new Bot(0,0);
for (int i = 0; i < BOTCOUNT; i++)
BotList.Add(new Bot(0,0));
TraverseCommands(Commands, BotList, outputs);
int botToLookForID = FindBot(17, 61, BotList);
Console.Write("\n\nBot ID holding 17 and 61: " + botToLookForID);
Console.Read();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment