Created
December 13, 2016 17:09
-
-
Save David-Mimnagh/3cbed0229067b49d85fb8d7a382ecfed 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_Day10 | |
{ | |
class Program | |
{ | |
const int BOTCOUNT = 215; | |
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(List<string> commandList, List<Bot> botList, int[] outputs) | |
{ | |
int index = 0; | |
do | |
{ | |
Console.Write("\nIndex: " + index + "\t COUNT: " + commandList.Count); | |
bool addedLow=false; bool addedHigh = false; | |
string command = commandList[index]; | |
int start = "bot ".Length; | |
int to = command.IndexOf(" ",start); | |
int botGivingID = Int32.Parse(command.Substring(start, to- start)); | |
int botGivingLow = botList[botGivingID].Low; | |
int botGivingHigh = botList[botGivingID].High; | |
string firstWord = null; | |
if (botGivingLow > 0) | |
{ | |
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); | |
int currentLow = botList[botGettingID].Low; | |
int currentHigh = botList[botGettingID].High; | |
if (botList[botGivingID].Low < currentHigh) | |
{ | |
botList[botGettingID].Low = botList[botGivingID].Low; | |
botList[botGivingID].Low = 0; | |
} | |
else | |
{ | |
botList[botGettingID].High = botList[botGivingID].Low; | |
botList[botGivingID].Low = 0; | |
} | |
addedLow = true; | |
} | |
break; | |
case "output": | |
{ | |
lowValTo = lowValTo.Replace(firstWord, String.Empty); | |
int outputID = Int32.Parse(lowValTo); | |
outputs[outputID] = botList[botGivingID].Low; | |
botList[botGivingID].Low = 0; | |
addedLow = true; | |
} | |
break; | |
default: { } break; | |
} | |
} | |
if (botGivingHigh > 0) | |
{ | |
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; | |
botList[botGivingID].High = 0; | |
} | |
else | |
{ | |
botList[botGettingID].High = botList[botGivingID].High; botList[botGivingID].High = 0; | |
} | |
addedHigh = true; | |
} | |
break; | |
case "output": | |
{ | |
highValTo = highValTo.Replace(firstWord, String.Empty); | |
int outputID = Int32.Parse(highValTo); | |
outputs[outputID] = botList[botGivingID].High; | |
botList[botGivingID].High = 0; | |
addedHigh = true; | |
} | |
break; | |
default: { } break; | |
} | |
} | |
int bot2ChipsCount = 0; | |
//for (int i = 0; i < botList.Count;i++) | |
//{ | |
// if (botList[i].Low > 0 && botList[i].High > 0) | |
// { | |
// bot2ChipsCount++; | |
// if (botList[i].Low == 17 && botList[i].High == 61) | |
// Console.Write("FUCKING FOUND IT"); | |
// } | |
//} | |
if (addedLow && addedHigh) | |
commandList.Remove(commandList[index]); | |
if (index < commandList.Count - 1) | |
index++; | |
else | |
index = 0; | |
Console.Write("\n\nBots with 2 chips: " + bot2ChipsCount); | |
} while (commandList.Count > 0); | |
} | |
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]); | |
} | |
} | |
MoveValues(commandList, botList, outputs); | |
} | |
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