Skip to content

Instantly share code, notes, and snippets.

@David-Mimnagh
Created December 13, 2016 13:13
Show Gist options
  • Save David-Mimnagh/378aaaa522419b6df1e39e15fa3b693b to your computer and use it in GitHub Desktop.
Save David-Mimnagh/378aaaa522419b6df1e39e15fa3b693b 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 = 300;
public class Bot
{
public int Low { get; set; }
public int High { 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 MoveValues(string command, List<Bot> botList, int[] outputs)
{
int start = command.IndexOf("bot ") + "bot ".Length;
int to = command.IndexOf(" ");
int botGivingID = Int32.Parse(command.Substring(start, to-1));
start = command.IndexOf("to ") + "to ".Length;
to = command.IndexOf(" and") - " and".Length;
string lowValTo = command.Substring(start, to);
string firstWord = lowValTo.Substring(0, lowValTo.IndexOf(" and"));
switch(firstWord)
{
case "bot":
{
lowValTo = lowValTo.Replace(firstWord,String.Empty);
int botGettingID = Int32.Parse(lowValTo);
int currentLow = botList[botGettingID].Low;
int currentHigh = botList[botGettingID].High;
if (botList[botGivingID].Low < currentHigh)
botList[botGettingID].Low = botList[botGivingID].Low;
else
botList[botGettingID].High = botList[botGivingID].Low;
}
break;
case "output":
{
lowValTo = lowValTo.Replace(firstWord, String.Empty);
int outputID = Int32.Parse(lowValTo);
outputs[outputID] = botList[botGivingID].Low;
}
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);
int currentLow = botList[botGettingID].Low;
int currentHigh = botList[botGettingID].High;
if (botList[botGivingID].High < currentHigh)
botList[botGettingID].Low = botList[botGivingID].High;
else
botList[botGettingID].High = botList[botGivingID].High;
}
break;
case "output":
{
highValTo = highValTo.Replace(firstWord, String.Empty);
int outputID = Int32.Parse(highValTo);
outputs[outputID] = botList[botGivingID].High;
}
break;
default: { } break;
}
}
static void TraverseCommands(List<string> commandList, List<Bot> botList, int[] outputs)
{
string firstWord = null;
for (int i = 0; i < commandList.Count; i++)
{
if(commandList[i].Contains("value"))
{
Console.Write("\n\tVALUE COMMAND");
GiveValueToBot(commandList[i], botList);
commandList.Remove(commandList[i]);
}
}
foreach (string command in commandList)
{
firstWord = command.Substring(0, command.IndexOf(" "));
switch (firstWord)
{
case "bot":
Console.Write("\n\tBOT COMMAND");
MoveValues(command, botList, outputs);
break;
default: { } break;
}
}
}
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("Bot ID: " + botToLookForID);
Console.Read();
}
}
}
@setiawanjemy88
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment