static void Main(string[] args) { List <string> inputList = AoCUtilities.GetInputLines(); // Part 1 long sumP1 = 0; foreach (string expression in inputList) { long result = Evaluate(expression); sumP1 += result; } Console.WriteLine(sumP1); // Part 2 long sumP2 = 0; foreach (string expression in inputList) { long result = Evaluate(expression, true); sumP2 += result; } Console.WriteLine(sumP2); Console.ReadLine(); }
static void Main(string[] args) { List <string> inputList = AoCUtilities.GetInputLines(); List <Seat> seats = inputList.ConvertAll(str => new Seat(str)); seats = seats.OrderBy(seat => seat.id).ToList(); int lastSeatId = 0; foreach (Seat seat in seats) { if (lastSeatId != 0 && seat.id != lastSeatId + 1) { break; } else { lastSeatId = seat.id; } } Console.WriteLine(seats.Last().id); Console.WriteLine(lastSeatId + 1); Console.ReadLine(); }
static void Main(string[] args) { List <string> inputList = AoCUtilities.GetInputLines(); width = inputList[0].Length; height = inputList.Count; map = new GridType[height][]; for (int height_i = 0; height_i < height; height_i++) { map[height_i] = new GridType[width]; for (int width_i = 0; width_i < width; width_i++) { if (inputList[height_i][width_i] == '.') { map[height_i][width_i] = GridType.Open; } else { map[height_i][width_i] = GridType.Tree; } } } int trees_1_1 = CountTrees(1, 1); int trees_1_3 = CountTrees(1, 3); int trees_1_5 = CountTrees(1, 5); int trees_1_7 = CountTrees(1, 7); int trees_2_1 = CountTrees(2, 1); Console.WriteLine(trees_1_3); Console.WriteLine(trees_1_1 * trees_1_3 * trees_1_5 * trees_1_7 * trees_2_1); Console.ReadLine(); }
static void Main(string[] args) { string input = AoCUtilities.GetInput(); List <string> passportStrings = input.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.None).ToList(); List <Passport> passports = passportStrings.ConvertAll(str => new Passport(str)); int validPassportsCount = passports.Count(passport => passport.valid); Console.WriteLine(validPassportsCount); Console.ReadLine(); }
static void Main(string[] args) { List <string> inputList = AoCUtilities.GetInputLines(); int earliestDepartureTime = int.Parse(inputList[0]); string[] busStringParts = inputList[1].Split(','); List <Bus> buses = new List <Bus>(); foreach (string busStringPart in busStringParts) { buses.Add(new Bus(busStringPart)); } List <Bus> busesByEarliest = buses.OrderBy(bus => bus.FindFirstDepartureAfter(earliestDepartureTime)).ToList(); Bus earliestBus = busesByEarliest[0]; int earliestBusDepartureTime = earliestBus.FindFirstDepartureAfter(earliestDepartureTime); int answer = earliestBus.TimePeriod * (earliestBusDepartureTime - earliestDepartureTime); Console.WriteLine(answer); bool foundSolution = false; List <int> busTimesFound = new List <int>(); long trialTime = 1; long timeIncrement = 1; while (!foundSolution) { foundSolution = true; for (int bus_i = 0; bus_i < buses.Count; bus_i++) { Bus bus = buses[bus_i]; if (bus.TimePeriod != 0) { long requiredDepartureTime = trialTime + bus_i; if (requiredDepartureTime % bus.TimePeriod != 0) { foundSolution = false; break; } else if (!busTimesFound.Contains(bus.TimePeriod)) { busTimesFound.Add(bus.TimePeriod); timeIncrement = LCM(busTimesFound); } } } trialTime += timeIncrement; } trialTime -= timeIncrement; Console.WriteLine(trialTime); Console.ReadLine(); }
static void Main(string[] args) { List <string> inputList = AoCUtilities.GetInputLines(); List <Password> passwords = inputList.ConvertAll(str => new Password(str)); int validP1PasswordsCount = passwords.Count(password => password.validP1); int validP2PasswordsCount = passwords.Count(password => password.validP2); Console.WriteLine(validP1PasswordsCount); Console.WriteLine(validP2PasswordsCount); Console.ReadLine(); }
public void print() { #if DEBUG || OVERRIDE for (int y = 0; y < this.dim; y++) { for (int x = 0; x < this.dim; x++) { AoCUtilities.DebugWrite("{0}", (char)this.pixels[y][x].type); } AoCUtilities.DebugWriteLine(); } AoCUtilities.DebugWriteLine(); #endif }
static void Main(string[] args) { List <string> inputList = AoCUtilities.GetInputLines(); foreach (string bagRuleString in inputList) { string[] bagRuleStringParts = bagRuleString.Split(new string[] { " bags contain " }, StringSplitOptions.RemoveEmptyEntries); string bagColour = bagRuleStringParts[0]; if (bags.ContainsKey(bagColour)) { throw new ArgumentException("Rule already exists for this bag"); } bags[bagColour] = new Bag(bagColour, bagRuleStringParts[1]); } List <Bag> bagsThatWouldAllowShinyGoldBag = new List <Bag>(); List <Bag> bagsToCheckForThatWouldBeValid = new List <Bag> { bags["shiny gold"] }; while (bagsToCheckForThatWouldBeValid.Count > 0) { List <Bag> nextBagsToCheckForThatWouldBeValid = new List <Bag>(); foreach (Bag bagToCheckForThatWouldBeValid in bagsToCheckForThatWouldBeValid) { foreach (KeyValuePair <string, Bag> kv in bags) { if (kv.Value.allowedBags.ContainsKey(bagToCheckForThatWouldBeValid.colour)) { if (!bagsThatWouldAllowShinyGoldBag.Contains(kv.Value)) { bagsThatWouldAllowShinyGoldBag.Add(kv.Value); } nextBagsToCheckForThatWouldBeValid.Add(kv.Value); } } } bagsToCheckForThatWouldBeValid = nextBagsToCheckForThatWouldBeValid; } int bagsInsideShinyGoldCount = bags["shiny gold"].BagsInsideThisCount(); Console.WriteLine(bagsThatWouldAllowShinyGoldBag.Count); Console.WriteLine(bagsInsideShinyGoldCount); Console.ReadLine(); }
static void Main(string[] args) { List <string> inputList = AoCUtilities.GetInputLines(); int acc; Simulate(inputList, out acc); Console.WriteLine(acc); bool loop = true; for (int instruction_i = 0; instruction_i < inputList.Count; instruction_i++) { string instruction = inputList[instruction_i]; string opcode = instruction.Substring(0, 3); if (opcode == "nop" || opcode == "jmp") { List <string> copiedInputList = new List <string>(inputList); if (opcode == "nop") { copiedInputList[instruction_i] = instruction.Replace("nop", "jmp"); } else if (opcode == "jmp") { copiedInputList[instruction_i] = instruction.Replace("jmp", "nop"); } loop = Simulate(copiedInputList, out acc); if (!loop) { break; } } } if (loop) { throw new ArgumentException("Didn't find an instruction to switch that fixes the error"); } Console.WriteLine(acc); Console.ReadLine(); }
static void Main(string[] args) { string input = AoCUtilities.GetInput(); List <string> groupStrings = input.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); List <Group> groups = groupStrings.ConvertAll(str => new Group(str)); int summedUniqueAnswers = 0; foreach (Group group in groups) { summedUniqueAnswers += group.uniqueAnswerCount; } int summedEveryoneAnsweredYes = 0; foreach (Group group in groups) { summedEveryoneAnsweredYes += group.everyoneAnsweredYesCount; } Console.WriteLine(summedUniqueAnswers); Console.WriteLine(summedEveryoneAnsweredYes); Console.ReadLine(); }
static void Main(string[] args) { List <string> inputList = AoCUtilities.GetInputLines(); foreach (var entry1 in inputList) { int val1 = int.Parse(entry1); foreach (var entry2 in inputList) { int val2 = int.Parse(entry2); foreach (var entry3 in inputList) { int val3 = int.Parse(entry3); if (val1 + val2 + val3 == 2020) { Console.WriteLine(val1 * val2 * val3); } } } } Console.ReadLine(); }
static void Main(string[] args) { List <string> inputList = AoCUtilities.GetInputLines(); int cardPublicKey = int.Parse(inputList[0]); int doorPublicKey = int.Parse(inputList[1]); //cardPublicKey = 5764801; //doorPublicKey = 17807724; int cardLoopSize = FindLoopSize(cardPublicKey, 7); int doorLoopSize = FindLoopSize(doorPublicKey, 7); long encryptionKey1 = Transform(cardLoopSize, doorPublicKey); long encryptionKey2 = Transform(doorLoopSize, cardPublicKey); if (encryptionKey1 != encryptionKey2) { throw new ArgumentException("Invalid input"); } Console.WriteLine(encryptionKey1); Console.ReadLine(); }
// This doesn't work, was an attempt to avoid using regex public int CheckMessage(string messageSubstring, int recursion = 0) { //for (int i = 0; i < recursion; i++) // Console.Write("\t"); //Console.WriteLine($"{this.id} {messageSubstring}"); int lengthOfMatch = 0; if (this.leaf) { if (messageSubstring.Length > 0 && messageSubstring[0] == this.value) { lengthOfMatch = 1; } } else { bool matchStr; foreach (StringOfRules strOfRules in this.alternatives) { for (int i = 0; i < recursion; i++) { AoCUtilities.DebugWrite("\t"); } AoCUtilities.DebugWriteLine($"{messageSubstring} must match {strOfRules}"); lengthOfMatch = 0; matchStr = true; foreach (Rule rule in strOfRules.rules) { for (int i = 0; i < recursion; i++) { AoCUtilities.DebugWrite("\t"); } AoCUtilities.DebugWriteLine($"{rule}"); string messageSubstringSection = messageSubstring.Substring(lengthOfMatch); int lengthOfSubMatch = rule.CheckMessage(messageSubstringSection, recursion + 1); if (lengthOfSubMatch == 0) { matchStr = false; break; } else { lengthOfMatch += lengthOfSubMatch; } } if (matchStr) { for (int i = 0; i < recursion; i++) { AoCUtilities.DebugWrite("\t"); } AoCUtilities.DebugWriteLine($"True"); break; } else { lengthOfMatch = 0; for (int i = 0; i < recursion; i++) { AoCUtilities.DebugWrite("\t"); } AoCUtilities.DebugWriteLine($"False"); } } } return(lengthOfMatch); }
static void Main(string[] args) { List <string> inputList = AoCUtilities.GetInputLines(); ulong pow_2_36 = (ulong)Math.Pow(2, 36); Dictionary <ulong, ulong> memoryP1 = new Dictionary <ulong, ulong>(); ulong maxValue36bit = pow_2_36 - 1; ulong andMask = maxValue36bit; ulong orMask = 0; foreach (string line in inputList) { string[] lineParts = line.Split(new string[] { " = " }, StringSplitOptions.RemoveEmptyEntries); if (lineParts[0] == "mask") { andMask = maxValue36bit; orMask = 0; for (int i = 0; i < 36; i++) { char character = lineParts[1][i]; switch (character) { case '0': { andMask &= ~((ulong)1 << (35 - i)); } break; case '1': { orMask |= (ulong)1 << (35 - i); } break; } } } else { string addressString = lineParts[0].Substring(4, lineParts[0].Length - 5); ulong address = ulong.Parse(addressString); ulong preMaskData = ulong.Parse(lineParts[1]); ulong postMaskData = (preMaskData | orMask) & andMask; memoryP1[address] = postMaskData; } } ulong totalP1 = 0; foreach (var kv in memoryP1) { totalP1 += kv.Value; } Console.WriteLine(totalP1); /////////////////////////////////////// // Part 2 /////////////////////////////////////// /// Dictionary <ulong, ulong> memoryP2 = new Dictionary <ulong, ulong>(); string maskString = ""; foreach (string line in inputList) { string[] lineParts = line.Split(new string[] { " = " }, StringSplitOptions.RemoveEmptyEntries); if (lineParts[0] == "mask") { maskString = lineParts[1]; } else { string addressString = lineParts[0].Substring(4, lineParts[0].Length - 5); ulong address = ulong.Parse(addressString); ulong data = ulong.Parse(lineParts[1]); List <ulong> possibleAddresses = GetPotentialAddressesFrom(address, maskString); foreach (ulong possibleAddress in possibleAddresses) { memoryP2[possibleAddress] = data; } } } ulong totalP2 = 0; foreach (var kv in memoryP2) { totalP2 += kv.Value; } Console.WriteLine(totalP2); Console.ReadLine(); }
static void Main(string[] args) { string input = AoCUtilities.GetInput(); string[] playerDecks = input.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries); List <int> originalDeck1 = new List <int>(); List <int> originalDeck2 = new List <int>(); List <int> currentDeck; for (int i = 0; i < 2; i++) { if (i == 0) { currentDeck = originalDeck1; } else { currentDeck = originalDeck2; } var deckStringParts = playerDecks[i].Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); for (int j = 1; j < deckStringParts.Length; j++) { var cardString = deckStringParts[j]; currentDeck.Add(int.Parse(cardString)); } } ////////////////////////////////////// // Part 1 ////////////////////////////////////// List <int> deck1 = new List <int>(originalDeck1); List <int> deck2 = new List <int>(originalDeck2); List <string> hashes = new List <string>() { string.Join("", deck1) }; List <int> winningDeck = null; while (winningDeck == null && deck1.Count > 0 && deck2.Count > 0) { int nextDeck1 = deck1[0]; int nextDeck2 = deck2[0]; deck1.RemoveAt(0); deck2.RemoveAt(0); if (nextDeck1 > nextDeck2) { deck1.Add(nextDeck1); deck1.Add(nextDeck2); } else { deck2.Add(nextDeck2); deck2.Add(nextDeck1); } string newHash = string.Join("", deck1); if (hashes.Contains(newHash)) { winningDeck = deck1; } else { hashes.Add(newHash); } } if (winningDeck == null) { if (deck1.Count == 0) { winningDeck = deck2; } else { winningDeck = deck1; } } int score = 0; for (int i = 1; i <= winningDeck.Count; i++) { int value = winningDeck[winningDeck.Count - i]; score += value * i; } Console.WriteLine(score); ////////////////////////////////////// // Part 2 ////////////////////////////////////// deck1 = new List <int>(originalDeck1); deck2 = new List <int>(originalDeck2); bool player1Wins = RecursiveGame(deck1, deck2); if (player1Wins) { winningDeck = deck1; } else { winningDeck = deck2; } score = 0; for (int i = 1; i <= winningDeck.Count; i++) { int value = winningDeck[winningDeck.Count - i]; score += value * i; } Console.WriteLine(score); Console.ReadLine(); }
static void Main(string[] args) { List <string> inputList = AoCUtilities.GetInputLines(); List <Field> fields = new List <Field>(); int i = 0; string line = inputList[i]; while (line != "") { var parts1 = line.Split(new string[] { ": " }, StringSplitOptions.RemoveEmptyEntries); string name = parts1[0]; var field = new Field(name); var parts2 = parts1[1].Split(new string[] { " or " }, StringSplitOptions.RemoveEmptyEntries); foreach (string requirementString in parts2) { var requirementStringParts = requirementString.Split('-'); int min = int.Parse(requirementStringParts[0]); int max = int.Parse(requirementStringParts[1]); field.requirements.Add(new Requirement(min, max)); } fields.Add(field); i++; line = inputList[i]; } i += 2; string yourTicketString = inputList[i]; Ticket yourTicket = new Ticket(yourTicketString); List <Ticket> otherTickets = new List <Ticket>(); i += 3; while (i < inputList.Count) { line = inputList[i]; otherTickets.Add(new Ticket(line)); i++; } List <Ticket> validOtherTickets = new List <Ticket>(); int sumP1 = 0; foreach (Ticket otherTicket in otherTickets) { bool allValuesValid = true; foreach (int value in otherTicket.values) { bool valid = false; foreach (Field field in fields) { foreach (Requirement requirement in field.requirements) { if (value >= requirement.min && value <= requirement.max) { valid = true; break; } } if (valid) { break; } } if (!valid) { sumP1 += value; allValuesValid = false; } } otherTicket.valid = allValuesValid; if (allValuesValid) { validOtherTickets.Add(otherTicket); } } Console.WriteLine(sumP1); foreach (Field field in fields) { for (int x = 0; x < yourTicket.values.Count; x++) { field.possiblePositions.Add(x); } } foreach (Field field in fields) { for (int possiblePosition_i = 0; possiblePosition_i < field.possiblePositions.Count;) { int possiblePosition = field.possiblePositions[possiblePosition_i]; bool positionValid = true; foreach (Ticket ticket in validOtherTickets) { int trialValue = ticket.values[possiblePosition]; bool valid = false; foreach (Requirement requirement in field.requirements) { if (trialValue >= requirement.min && trialValue <= requirement.max) { valid = true; break; } } if (!valid) { positionValid = false; } } if (!positionValid) { field.possiblePositions.RemoveAt(possiblePosition_i); } else { possiblePosition_i++; } } } bool changeMade = true; while (changeMade) { changeMade = false; foreach (Field field in fields) { if (field.position == -1 && field.possiblePositions.Count == 1) { int knownPositionForThisField = field.possiblePositions[0]; field.position = knownPositionForThisField; // remove this position from other fields foreach (Field otherField in fields) { if (field != otherField) { changeMade = true; otherField.possiblePositions.Remove(knownPositionForThisField); } } } } } bool foundSolution = true; long product = 1; foreach (Field field in fields) { if (field.position == -1) { foundSolution = false; break; } if (field.name.Length >= 9 && field.name.Substring(0, 9) == "departure") { product *= yourTicket.values[field.position]; } } if (!foundSolution) { throw new Exception("No solution found"); } Console.WriteLine(product); Console.ReadLine(); }
static void Main(string[] args) { Dictionary <int, List <Turn> > numbersTurns = new Dictionary <int, List <Turn> >(); Turn lastTurn = null; List <string> inputList = AoCUtilities.GetInputLines(); string[] turnStrings = inputList[0].Split(','); int turn_i = 1; for (; turn_i <= turnStrings.Length; turn_i++) { string turnString = turnStrings[turn_i - 1]; int number = int.Parse(turnString); Turn turn = new Turn(turn_i, number); if (!numbersTurns.ContainsKey(number)) { numbersTurns[number] = new List <Turn> { turn }; } else { numbersTurns[number].Add(turn); } lastTurn = turn; } for (; turn_i <= 30000000; turn_i++) { int lastNumberSpoke = lastTurn.number; List <Turn> turnsWithThatNumber = numbersTurns[lastNumberSpoke]; int nextNumber; if (turnsWithThatNumber.Count > 1) { int lastTurnThatNumberSpoken = lastTurn.id; int secondLastTurnThatNumberSpoken = turnsWithThatNumber[turnsWithThatNumber.Count - 2].id; nextNumber = lastTurnThatNumberSpoken - secondLastTurnThatNumberSpoken; } else { nextNumber = 0; } if (turn_i == 2020) { Console.WriteLine(nextNumber); } Turn newTurn = new Turn(turn_i, nextNumber); if (!numbersTurns.ContainsKey(nextNumber)) { numbersTurns[nextNumber] = new List <Turn> { newTurn }; } else { numbersTurns[nextNumber].Add(newTurn); } lastTurn = newTurn; } numbersTurns.Clear(); Console.WriteLine(lastTurn.number); Console.ReadLine(); }
static void Main(string[] args) { List <string> inputList = AoCUtilities.GetInputLines(); }
static void Main(string[] args) { List <string> inputList = AoCUtilities.GetInputLines(); List <long> inputNumbers = inputList.ConvertAll(str => long.Parse(str)); const int preambleLength = 25; List <long> availableNumbers = new List <long>(preambleLength); for (int i = 0; i < preambleLength; i++) { availableNumbers.Add(inputNumbers[i]); } int number_i = preambleLength; for (; number_i < inputNumbers.Count; number_i++) { long targetNumber = inputNumbers[number_i]; bool foundSum = false; foreach (long availableNumber1 in availableNumbers) { foreach (long availableNumber2 in availableNumbers) { if (availableNumber1 + availableNumber2 == targetNumber) { foundSum = true; break; } } if (foundSum) { break; } } if (foundSum) { availableNumbers.RemoveAt(0); availableNumbers.Add(targetNumber); } else { break; } } long badNumber = inputNumbers[number_i]; Console.WriteLine(badNumber); List <long> contiguousRange = null; for (int startNumber_i = 0; startNumber_i < inputNumbers.Count - 1; startNumber_i++) { long sum = 0; int contiguousElementCount = 2; while (sum < badNumber) { sum = inputNumbers.Skip(startNumber_i).Take(contiguousElementCount).Sum(); contiguousElementCount++; } if (sum == badNumber) { contiguousRange = inputNumbers.Skip(startNumber_i).Take(contiguousElementCount - 1).ToList(); } } if (contiguousRange == null) { throw new ArgumentException("No contiguous range is found that sums to the value " + badNumber); } long minInContiguousRange = contiguousRange.Min(); long maxInContiguousRange = contiguousRange.Max(); long weakness = minInContiguousRange + maxInContiguousRange; Console.WriteLine(weakness); Console.ReadLine(); }
static bool RecursiveGame(List <int> deck1, List <int> deck2, int recursionLevel = 1) { List <string> hashes = new List <string>() { string.Join("", deck1) }; int round = 1; AoCUtilities.DebugWriteLine(); AoCUtilities.DebugWriteLine($"=== Game Recursion Level {recursionLevel} ==="); while (deck1.Count > 0 && deck2.Count > 0) { AoCUtilities.DebugWriteLine($"- Round {round} (Game {recursionLevel}) -"); AoCUtilities.DebugWriteLine($"Player 1's deck: {string.Join(", ", deck1)}"); AoCUtilities.DebugWriteLine($"Player 2's deck: {string.Join(", ", deck2)}"); int nextDeck1 = deck1[0]; int nextDeck2 = deck2[0]; deck1.RemoveAt(0); deck2.RemoveAt(0); AoCUtilities.DebugWriteLine($"Player 1 plays: {nextDeck1}"); AoCUtilities.DebugWriteLine($"Player 2 plays: {nextDeck2}"); if (deck1.Count >= nextDeck1 && deck2.Count >= nextDeck2) { AoCUtilities.DebugWriteLine($"Playing a sub-game to determine the winner..."); // recurse List <int> deck1Copy = new List <int>(deck1.GetRange(0, nextDeck1)); List <int> deck2Copy = new List <int>(deck2.GetRange(0, nextDeck2)); bool player1Wins = RecursiveGame(deck1Copy, deck2Copy, recursionLevel + 1); if (player1Wins) { deck1.Add(nextDeck1); deck1.Add(nextDeck2); AoCUtilities.DebugWriteLine($"Player 1 wins round {round} of game {recursionLevel}!"); } else { deck2.Add(nextDeck2); deck2.Add(nextDeck1); AoCUtilities.DebugWriteLine($"Player 2 wins round {round} of game {recursionLevel}!"); } } else { if (nextDeck1 > nextDeck2) { deck1.Add(nextDeck1); deck1.Add(nextDeck2); AoCUtilities.DebugWriteLine($"Player 1 wins round {round} of game {recursionLevel}!"); } else { deck2.Add(nextDeck2); deck2.Add(nextDeck1); AoCUtilities.DebugWriteLine($"Player 2 wins round {round} of game {recursionLevel}!"); } } string newHash = string.Join(",", deck1) + "|" + string.Join(",", deck2); if (hashes.Contains(newHash)) { AoCUtilities.DebugWriteLine($"Back to game level {recursionLevel - 1}..."); return(true); } else { hashes.Add(newHash); } round++; AoCUtilities.DebugWriteLine(); } AoCUtilities.DebugWriteLine($"Back to game level {recursionLevel-1}..."); return(deck2.Count == 0); }
static void Main(string[] args) { Dictionary <int, Rule> rules = new Dictionary <int, Rule>(); string input = AoCUtilities.GetInput(); var inputParts = input.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries); string rulesString = inputParts[0]; string[] ruleStrings = rulesString.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); string messages = inputParts[1]; string[] messageStrings = messages.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (string ruleString in ruleStrings) { var ruleStringParts = ruleString.Split(':'); int id = int.Parse(ruleStringParts[0]); Rule newRule; if (rules.ContainsKey(id)) { newRule = rules[id]; } else { newRule = new Rule(); newRule.id = id; rules[id] = newRule; } if (ruleStringParts[1][1] == '"') { newRule.leaf = true; newRule.value = ruleStringParts[1][2]; } else { var ruleStringAlternatives = ruleStringParts[1].Split('|'); foreach (string ruleStringAlternative in ruleStringAlternatives) { string[] subRuleStrings = ruleStringAlternative.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); StringOfRules stringOfRules = new StringOfRules(); foreach (string subRuleString in subRuleStrings) { int subRuleId = int.Parse(subRuleString); Rule subRule; if (rules.ContainsKey(subRuleId)) { subRule = rules[subRuleId]; } else { subRule = new Rule(); subRule.id = subRuleId; rules[subRuleId] = subRule; } stringOfRules.rules.Add(subRule); } newRule.alternatives.Add(stringOfRules); } } } string regex = rules[0].EvaluateRegex(); string regexPatternP1 = $@"^\b{regex}\b$"; AoCUtilities.DebugWriteLine("{0}", regexPatternP1.Length); AoCUtilities.DebugWriteLine(regexPatternP1); Regex regexP1 = new Regex(regexPatternP1); int messagesMatchCountP1 = 0; foreach (string messageString in messageStrings) { bool match = regexP1.IsMatch(messageString); if (match) { messagesMatchCountP1++; } } Console.WriteLine(messagesMatchCountP1); var new8strOfRules = new StringOfRules(); new8strOfRules.rules.Add(rules[42]); new8strOfRules.rules.Add(rules[8]); rules[8].alternatives.Add(new8strOfRules); var new11strOfRules = new StringOfRules(); new11strOfRules.rules.Add(rules[42]); new11strOfRules.rules.Add(rules[11]); new11strOfRules.rules.Add(rules[31]); rules[11].alternatives.Add(new11strOfRules); regex = rules[0].EvaluateRegex(); string regexPatternP2 = $@"^\b{regex}\b$"; AoCUtilities.DebugWriteLine("{0}", regexPatternP2.Length); AoCUtilities.DebugWriteLine(regexPatternP2); Regex regexP2 = new Regex(regexPatternP2); int messagesMatchCountP2 = 0; foreach (string messageString in messageStrings) { bool match = regexP2.IsMatch(messageString); if (match) { messagesMatchCountP2++; } } Console.WriteLine(messagesMatchCountP2); Console.ReadLine(); }
static void Main(string[] args) { Dictionary <int, int> joltageDifferences = new Dictionary <int, int>(); List <string> inputList = AoCUtilities.GetInputLines(); List <int> availableAdapters = inputList.ConvertAll(str => int.Parse(str)); List <int> sortedAdapters = availableAdapters.OrderBy(adapter => adapter).ToList(); joltageDifferences[3] = 1; for (int adapter_i = 0; adapter_i < sortedAdapters.Count; adapter_i++) { int adapterJoltage = sortedAdapters[adapter_i]; int joltageDifference; if (adapter_i == 0) { joltageDifference = adapterJoltage; } else { joltageDifference = adapterJoltage - sortedAdapters[adapter_i - 1]; } if (joltageDifferences.ContainsKey(joltageDifference)) { joltageDifferences[joltageDifference]++; } else { joltageDifferences[joltageDifference] = 1; } } Console.WriteLine(joltageDifferences[1] * joltageDifferences[3]); Dictionary <int, Node> adapterNodes = new Dictionary <int, Node>(); Node outletNode = new Node(0); adapterNodes[0] = outletNode; outletNode.WaysToReach = 1; int highestJoltageAdapter = sortedAdapters.Max(); Node deviceNode = new Node(highestJoltageAdapter + 3); adapterNodes[highestJoltageAdapter + 3] = deviceNode; for (int adapter_i = 0; adapter_i < sortedAdapters.Count; adapter_i++) { int adapterJoltage = sortedAdapters[adapter_i]; adapterNodes[adapterJoltage] = new Node(adapterJoltage); } outletNode.FindNodesInReach(adapterNodes); for (int adapter_i = 0; adapter_i < sortedAdapters.Count; adapter_i++) { int adapterJoltage = sortedAdapters[adapter_i]; adapterNodes[adapterJoltage].FindNodesInReach(adapterNodes); } Console.WriteLine(deviceNode.WaysToReach); Console.ReadLine(); }
static void Main(string[] args) { string input = AoCUtilities.GetInput(); ///////////////////// // Part 1 ///////////////////// List <Tile> tilesR0 = new List <Tile>(); List <Tile> tilesR90 = new List <Tile>(); List <Tile> tilesR180 = new List <Tile>(); List <Tile> tilesR270 = new List <Tile>(); List <Tile> flippedTilesR0 = new List <Tile>(); List <Tile> flippedTilesR90 = new List <Tile>(); List <Tile> flippedTilesR180 = new List <Tile>(); List <Tile> flippedTilesR270 = new List <Tile>(); var tileStrings = input.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (var tileString in tileStrings) { var tileLines = tileString.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); string idString = tileLines[0].Substring(5, tileLines[0].Length - 6); int id = int.Parse(idString); Tile newTileR0 = new Tile(id, tileLines); tilesR0.Add(newTileR0); Tile newTileR90 = newTileR0.GetR90Tile(); tilesR90.Add(newTileR90); Tile newTileR180 = newTileR90.GetR90Tile(); tilesR180.Add(newTileR180); Tile newTileR270 = newTileR180.GetR90Tile(); tilesR270.Add(newTileR270); Tile flippedNewTileR0 = newTileR0.GetFlippedTile(); flippedTilesR0.Add(flippedNewTileR0); Tile flippedNewTileR90 = flippedNewTileR0.GetR90Tile(); flippedTilesR90.Add(flippedNewTileR90); Tile flippedNewTileR180 = flippedNewTileR90.GetR90Tile(); flippedTilesR180.Add(flippedNewTileR180); Tile flippedNewTileR270 = flippedNewTileR180.GetR90Tile(); flippedTilesR270.Add(flippedNewTileR270); } Tile anchorTile = tilesR0[0]; List <Tile> usedTiles = new List <Tile> { anchorTile }; anchorTile.FindMatch(usedTiles, tilesR0, tilesR90, tilesR180, tilesR270, flippedTilesR0, flippedTilesR90, flippedTilesR180, flippedTilesR270); long product = 1; Tile tlTile = null; foreach (Tile usedTile in usedTiles) { if (usedTile.TL) { tlTile = usedTile; } if (usedTile.TL || usedTile.TR || usedTile.BL || usedTile.BR) { product *= usedTile.id; } } Console.WriteLine(product); ///////////////////// // Part 2 ///////////////////// int tilesDim = (int)Math.Sqrt(usedTiles.Count); Tile[][] tileMap = new Tile[tilesDim][]; int tile_y = 0; Tile bottomTile = tlTile; while (bottomTile != null) { tileMap[tile_y] = new Tile[tilesDim]; int tile_x = 0; Tile rightTile = bottomTile; while (rightTile != null) { tileMap[tile_y][tile_x] = rightTile; tile_x++; rightTile = rightTile.rightTile; } tile_y++; bottomTile = bottomTile.bottomTile; } int imageDim = tilesDim * 8; Image imageR0 = new Image(imageDim); int whitePixelCount = 0; for (tile_y = 0; tile_y < tilesDim; tile_y++) { for (int tile_x = 0; tile_x < tilesDim; tile_x++) { Tile tile = tileMap[tile_y][tile_x]; for (int pixel_y = 1; pixel_y <= 8; pixel_y++) { for (int pixel_x = 1; pixel_x <= 8; pixel_x++) { int globalPixel_y = (tile_y * 8) + (pixel_y - 1); int globalPixel_x = (tile_x * 8) + (pixel_x - 1); imageR0.pixels[globalPixel_y][globalPixel_x] = tile.pixels[pixel_y][pixel_x]; if (tile.pixels[pixel_y][pixel_x].type == PixelType.white) { whitePixelCount++; } } } } } Image imageR90 = imageR0.GetR90Image(); Image imageR180 = imageR90.GetR90Image(); Image imageR270 = imageR180.GetR90Image(); Image imageFlippedR0 = imageR0.GetFlippedImage(); Image imageFlippedR90 = imageFlippedR0.GetR90Image(); Image imageFlippedR180 = imageFlippedR90.GetR90Image(); Image imageFlippedR270 = imageFlippedR180.GetR90Image(); //imageR0.print(); int seaMonsters = 0; if (seaMonsters == 0) { seaMonsters = searchForSeaMonsters(imageR0); } if (seaMonsters == 0) { seaMonsters = searchForSeaMonsters(imageR90); } if (seaMonsters == 0) { seaMonsters = searchForSeaMonsters(imageR180); } if (seaMonsters == 0) { seaMonsters = searchForSeaMonsters(imageR270); } if (seaMonsters == 0) { seaMonsters = searchForSeaMonsters(imageFlippedR0); } if (seaMonsters == 0) { seaMonsters = searchForSeaMonsters(imageFlippedR90); } if (seaMonsters == 0) { seaMonsters = searchForSeaMonsters(imageFlippedR180); } if (seaMonsters == 0) { seaMonsters = searchForSeaMonsters(imageFlippedR270); } int nonSeaMonstersWhitePixels = whitePixelCount - (seaMonsters * 15); Console.WriteLine(nonSeaMonstersWhitePixels); Console.ReadLine(); }
static void Main(string[] args) { List <string> inputList = AoCUtilities.GetInputLines(); Tile referenceTile = Tile.Create(0, 0); ///////////////////////////////////// // Part 1 ///////////////////////////////////// foreach (string tileIdentifierString in inputList) { Tile currentTile = referenceTile; for (int char_i = 0; char_i < tileIdentifierString.Length; char_i++) { string direction = ""; direction += tileIdentifierString[char_i]; if (direction == "s" || direction == "n") { direction += tileIdentifierString[char_i + 1]; char_i++; } switch (direction) { case "e": currentTile = currentTile.e; break; case "w": currentTile = currentTile.w; break; case "se": currentTile = currentTile.se; break; case "sw": currentTile = currentTile.sw; break; case "ne": currentTile = currentTile.ne; break; case "nw": currentTile = currentTile.nw; break; } } if (!currentTile.white) { } currentTile.white = !currentTile.white; } int blackTileCount = 0; foreach (var temp in TilesNEE.Values) { foreach (var tile in temp.Values) { if (!tile.white) { blackTileCount++; } } } Console.WriteLine(blackTileCount); ///////////////////////////////////// // Part 2 ///////////////////////////////////// for (int day = 0; day < 100; day++) { List <Tile> tilesToGenerateBorderFor = new List <Tile>(); foreach (var temp in TilesNEE.Values) { foreach (var tile in temp.Values) { tilesToGenerateBorderFor.Add(tile); } } foreach (Tile tile in tilesToGenerateBorderFor) { Tile t; t = tile.e; t = tile.w; t = tile.ne; t = tile.sw; t = tile.nw; t = tile.se; } List <Tile> tilesToCheckForFlip = new List <Tile>(); foreach (var temp in TilesNEE.Values) { foreach (var tile in temp.Values) { tilesToCheckForFlip.Add(tile); } } List <Tile> tilesToFlip = new List <Tile>(); foreach (Tile tile in tilesToCheckForFlip) { int adjacentBlackTiles = 0; if (!tile.e.white) { adjacentBlackTiles++; } if (!tile.w.white) { adjacentBlackTiles++; } if (!tile.ne.white) { adjacentBlackTiles++; } if (!tile.sw.white) { adjacentBlackTiles++; } if (!tile.nw.white) { adjacentBlackTiles++; } if (!tile.se.white) { adjacentBlackTiles++; } if (tile.white && adjacentBlackTiles == 2) { tilesToFlip.Add(tile); } else if (!tile.white && (adjacentBlackTiles == 0 || adjacentBlackTiles > 2)) { tilesToFlip.Add(tile); } } foreach (Tile tile in tilesToFlip) { tile.white = !tile.white; } } blackTileCount = 0; foreach (var temp in TilesNEE.Values) { foreach (var tile in temp.Values) { if (!tile.white) { blackTileCount++; } } } Console.WriteLine(blackTileCount); Console.ReadLine(); }
static void Main(string[] args) { int positionX = 0; int positionY = 0; int angleFromNorth = 90; List <string> inputList = AoCUtilities.GetInputLines(); foreach (string instruction in inputList) { int numericArg = int.Parse(instruction.Substring(1)); switch (instruction[0]) { case 'N': positionY += numericArg; break; case 'S': positionY -= numericArg; break; case 'E': positionX += numericArg; break; case 'W': positionX -= numericArg; break; case 'R': angleFromNorth += numericArg; angleFromNorth = angleFromNorth % 360; if (angleFromNorth < 0) { angleFromNorth += 360; } break; case 'L': angleFromNorth -= numericArg; angleFromNorth = angleFromNorth % 360; if (angleFromNorth < 0) { angleFromNorth += 360; } break; case 'F': positionX += numericArg * (int)Math.Round(Math.Sin(angleFromNorth * (Math.PI / 180))); positionY += numericArg * (int)Math.Round(Math.Cos(angleFromNorth * (Math.PI / 180))); break; } } Console.WriteLine(Math.Abs(positionX) + Math.Abs(positionY)); ///////////////////////////////////////// // Part 2 ///////////////////////////////////////// positionX = 0; positionY = 0; int waypointOffsetX = 10; int waypointOffsetY = 1; foreach (string instruction in inputList) { int numericArg = int.Parse(instruction.Substring(1)); switch (instruction[0]) { case 'N': waypointOffsetY += numericArg; break; case 'S': waypointOffsetY -= numericArg; break; case 'E': waypointOffsetX += numericArg; break; case 'W': waypointOffsetX -= numericArg; break; case 'R': { int temp; if (numericArg == 90) { temp = waypointOffsetY; waypointOffsetY = -waypointOffsetX; waypointOffsetX = temp; } else if (numericArg == 180) { waypointOffsetY = -waypointOffsetY; waypointOffsetX = -waypointOffsetX; } else if (numericArg == 270) { temp = waypointOffsetY; waypointOffsetY = waypointOffsetX; waypointOffsetX = -temp; } } break; case 'L': { int temp; if (numericArg == 270) { temp = waypointOffsetY; waypointOffsetY = -waypointOffsetX; waypointOffsetX = temp; } else if (numericArg == 180) { waypointOffsetY = -waypointOffsetY; waypointOffsetX = -waypointOffsetX; } else if (numericArg == 90) { temp = waypointOffsetY; waypointOffsetY = waypointOffsetX; waypointOffsetX = -temp; } } break; case 'F': positionX += numericArg * waypointOffsetX; positionY += numericArg * waypointOffsetY; break; } } Console.WriteLine(Math.Abs(positionX) + Math.Abs(positionY)); Console.ReadLine(); }
static void Main(string[] args) { List <string> inputList = AoCUtilities.GetInputLines(); List <Food> foods = new List <Food>(); Dictionary <string, Allergen> allAllergens = new Dictionary <string, Allergen>(); Dictionary <string, Ingredient> allIngredients = new Dictionary <string, Ingredient>(); foreach (string foodString in inputList) { foods.Add(new Food(allAllergens, allIngredients, foodString)); } foreach (var allergenKV in allAllergens) { Allergen allergen = allergenKV.Value; List <Ingredient> intersection = null; foreach (Food foodWithThisAllergen in allergen.foodsWithThisAllergen) { if (intersection == null) { intersection = foodWithThisAllergen.ingredients; } else { intersection = intersection.Intersect(foodWithThisAllergen.ingredients).ToList(); } } allergen.possibleIngredients = intersection; } bool changeMade = true; while (changeMade) { changeMade = false; foreach (var allergenKV in allAllergens) { Allergen allergen = allergenKV.Value; if (allergen.possibleIngredients.Count == 1 && allergen.knownIngredient == null) { changeMade = true; allergen.knownIngredient = allergen.possibleIngredients[0]; allergen.knownIngredient.knownToCauseThisAllergen = allergen; foreach (var otherAllergenKV in allAllergens) { Allergen otherAllergen = otherAllergenKV.Value; if (allergen != otherAllergen) { otherAllergen.possibleIngredients.Remove(allergen.knownIngredient); } } } } } List <Ingredient> safeIngredients = allIngredients.Values.ToList(); foreach (var allergenKV in allAllergens) { Allergen allergen = allergenKV.Value; foreach (var possibleIngredient in allergen.possibleIngredients) { safeIngredients.Remove(possibleIngredient); } } int numberOfSafeIngredients = 0; foreach (Food food in foods) { foreach (Ingredient ingredient in safeIngredients) { if (food.ingredients.Contains(ingredient)) { numberOfSafeIngredients++; } } } Console.WriteLine(numberOfSafeIngredients); List <Allergen> allAllergensList = allAllergens.Values.OrderBy(allergen => allergen.name).ToList(); string canonicalDangerousIngredientList = string.Join(",", allAllergensList.ConvertAll(allergen => allergen.knownIngredient.name)); Console.WriteLine(canonicalDangerousIngredientList); Console.ReadLine(); }
static void Main(string[] args) { List <string> inputList = AoCUtilities.GetInputLines(); ////////////////////////////////////// // Part 1 ////////////////////////////////////// const int totalCupsP1 = 9; LinkedListNode[] nodesByValue = new LinkedListNode[totalCupsP1 + 1]; LinkedListNode previousNode = null; LinkedListNode firstNode = null; foreach (char c in inputList[0]) { int value = (int)char.GetNumericValue(c); LinkedListNode newNode = new LinkedListNode(value); nodesByValue[value] = newNode; if (firstNode == null) { firstNode = newNode; } if (previousNode != null) { newNode.previous = previousNode; previousNode.next = newNode; } previousNode = newNode; } firstNode.previous = previousNode; previousNode.next = firstNode; LinkedListNode currentCup = firstNode; Sim(totalCupsP1, 100, currentCup, nodesByValue); LinkedListNode oneNode = nodesByValue[1]; Print(oneNode.next, 8); ////////////////////////////////////// // Part 2 ////////////////////////////////////// const int totalCupsP2 = 1000000; nodesByValue = new LinkedListNode[totalCupsP2 + 1]; previousNode = null; firstNode = null; foreach (char c in inputList[0]) { long value = (long)char.GetNumericValue(c); LinkedListNode newNode = new LinkedListNode(value); nodesByValue[value] = newNode; if (firstNode == null) { firstNode = newNode; } if (previousNode != null) { newNode.previous = previousNode; previousNode.next = newNode; } previousNode = newNode; } for (long i = 10; i <= totalCupsP2; i++) { LinkedListNode newNode = new LinkedListNode(i); nodesByValue[i] = newNode; newNode.previous = previousNode; previousNode.next = newNode; previousNode = newNode; } firstNode.previous = previousNode; previousNode.next = firstNode; currentCup = firstNode; Sim(totalCupsP2, 10000000, currentCup, nodesByValue); oneNode = nodesByValue[1]; long product = oneNode.next.value * oneNode.next.next.value; Console.WriteLine(product); Console.ReadLine(); }
static void Main(string[] args) { List <string> inputList = AoCUtilities.GetInputLines(); /* * y * ^ * | * z x---> x * * */ // Planes : Rows : Cube /////////////////////////////////////// // Part 1 /////////////////////////////////////// for (int y = 0; y < inputList.Count; y++) { string rowString = inputList[y]; for (int x = 0; x < rowString.Length; x++) { char cubeChar = rowString[x]; Coord coord = new Coord(x, y, 0); switch (cubeChar) { case '.': setCube(x, y, 0, new Cube(x, y, 0, false)); break; case '#': setCube(x, y, 0, new Cube(x, y, 0, true)); break; } } } //print(); for (int cycle = 0; cycle < 6; cycle++) { int _minZ = minZ; int _maxZ = maxZ; int _minY = minY; int _maxY = maxY; int _minX = minX; int _maxX = maxX; for (int z = _minZ - 1; z <= _maxZ + 1; z++) { for (int y = _minY - 1; y <= _maxY + 1; y++) { for (int x = _minX - 1; x <= _maxX + 1; x++) { Cube cube = getCube(x, y, z); List <Cube> neighbourCubes = getNeighbourCubes(x, y, z); int howManyActiveNeighbours = neighbourCubes.Count(c => c.active); if (cube.active) { cube.newActive = howManyActiveNeighbours == 2 || howManyActiveNeighbours == 3; } else { cube.newActive = howManyActiveNeighbours == 3; } } } } foreach (var twoDimWorldKV in threeDimWorld) { foreach (var oneDimWorldKV in twoDimWorldKV.Value) { foreach (var cubeKV in oneDimWorldKV.Value) { cubeKV.Value.active = cubeKV.Value.newActive; } } } //Console.WriteLine($"Cycle {cycle + 1}"); //print(); } int activeCountP1 = 0; foreach (var twoDimWorldKV in threeDimWorld) { foreach (var oneDimWorldKV in twoDimWorldKV.Value) { foreach (var cubeKV in oneDimWorldKV.Value) { if (cubeKV.Value.active) { activeCountP1++; } } } } Console.WriteLine(activeCountP1); /////////////////////////////////////// // Part 2 /////////////////////////////////////// minX = int.MaxValue; maxX = int.MinValue; minY = int.MaxValue; maxY = int.MinValue; minZ = int.MaxValue; maxZ = int.MinValue; minW = int.MaxValue; maxW = int.MinValue; for (int y = 0; y < inputList.Count; y++) { string rowString = inputList[y]; for (int x = 0; x < rowString.Length; x++) { char cubeChar = rowString[x]; Coord coord = new Coord(x, y, 0); switch (cubeChar) { case '.': setCube(x, y, 0, 0, new Cube(x, y, 0, 0, false)); break; case '#': setCube(x, y, 0, 0, new Cube(x, y, 0, 0, true)); break; } } } //print4d(); for (int cycle = 0; cycle < 6; cycle++) { int _minW = minW; int _maxW = maxW; int _minZ = minZ; int _maxZ = maxZ; int _minY = minY; int _maxY = maxY; int _minX = minX; int _maxX = maxX; for (int w = _minW - 1; w <= _maxW + 1; w++) { for (int z = _minZ - 1; z <= _maxZ + 1; z++) { for (int y = _minY - 1; y <= _maxY + 1; y++) { for (int x = _minX - 1; x <= _maxX + 1; x++) { Cube cube = getCube(x, y, z, w); List <Cube> neighbourCubes = getNeighbourCubes(x, y, z, w); int howManyActiveNeighbours = neighbourCubes.Count(c => c.active); if (cube.active) { cube.newActive = howManyActiveNeighbours == 2 || howManyActiveNeighbours == 3; } else { cube.newActive = howManyActiveNeighbours == 3; } } } } } foreach (var threeDimWorldKV in fourDimWorld) { foreach (var twoDimWorldKV in threeDimWorldKV.Value) { foreach (var oneDimWorldKV in twoDimWorldKV.Value) { foreach (var cubeKV in oneDimWorldKV.Value) { cubeKV.Value.active = cubeKV.Value.newActive; } } } } //Console.WriteLine($"Cycle {cycle + 1}"); //print4d(); } int activeCountP2 = 0; foreach (var threeDimWorldKV in fourDimWorld) { foreach (var twoDimWorldKV in threeDimWorldKV.Value) { foreach (var oneDimWorldKV in twoDimWorldKV.Value) { foreach (var cubeKV in oneDimWorldKV.Value) { if (cubeKV.Value.active) { activeCountP2++; } } } } } Console.WriteLine(activeCountP2); Console.ReadLine(); }
static void Main(string[] args) { List <string> inputList = AoCUtilities.GetInputLines(); height = inputList.Count; width = inputList[0].Length; grid = new GridCell[height][]; for (int y = 0; y < height; y++) { grid[y] = new GridCell[width]; for (int x = 0; x < width; x++) { switch (inputList[y][x]) { case 'L': grid[y][x] = GridCell.AvailableChair; break; case '.': grid[y][x] = GridCell.Empty; break; default: throw new ArgumentException("Expecting only L and ."); } } } bool changeMade = true; while (changeMade) { changeMade = false; GridCell[][] copyOfGrid = new GridCell[height][]; for (int y = 0; y < height; y++) { copyOfGrid[y] = new GridCell[width]; for (int x = 0; x < width; x++) { copyOfGrid[y][x] = grid[y][x]; } } for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (grid[y][x] != GridCell.Empty) { int occupiedAdjacentSeats = 0; // left if (x > 0 && grid[y][x - 1] == GridCell.OccupiedChair) { occupiedAdjacentSeats++; } // right if (x < width - 1 && grid[y][x + 1] == GridCell.OccupiedChair) { occupiedAdjacentSeats++; } // top if (y > 0 && grid[y - 1][x] == GridCell.OccupiedChair) { occupiedAdjacentSeats++; } // bottom if (y < height - 1 && grid[y + 1][x] == GridCell.OccupiedChair) { occupiedAdjacentSeats++; } // tl if (x > 0 && y > 0 && grid[y - 1][x - 1] == GridCell.OccupiedChair) { occupiedAdjacentSeats++; } // bl if (x > 0 && y < height - 1 && grid[y + 1][x - 1] == GridCell.OccupiedChair) { occupiedAdjacentSeats++; } // tr if (x < width - 1 && y > 0 && grid[y - 1][x + 1] == GridCell.OccupiedChair) { occupiedAdjacentSeats++; } // br if (x < width - 1 && y < height - 1 && grid[y + 1][x + 1] == GridCell.OccupiedChair) { occupiedAdjacentSeats++; } if (grid[y][x] == GridCell.AvailableChair) { if (occupiedAdjacentSeats == 0) { changeMade = true; copyOfGrid[y][x] = GridCell.OccupiedChair; } } else if (grid[y][x] == GridCell.OccupiedChair) { if (occupiedAdjacentSeats >= 4) { changeMade = true; copyOfGrid[y][x] = GridCell.AvailableChair; } } } } } grid = copyOfGrid; } int occupiedSeatCount = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (grid[y][x] == GridCell.OccupiedChair) { occupiedSeatCount++; } } } Console.WriteLine(occupiedSeatCount); ////////////////////////////////////// // PART 2 ////////////////////////////////////// grid = new GridCell[height][]; for (int y = 0; y < height; y++) { grid[y] = new GridCell[width]; for (int x = 0; x < width; x++) { switch (inputList[y][x]) { case 'L': grid[y][x] = GridCell.AvailableChair; break; case '.': grid[y][x] = GridCell.Empty; break; default: throw new ArgumentException("Expecting only L and ."); } } } changeMade = true; while (changeMade) { changeMade = false; GridCell[][] copyOfGrid = new GridCell[height][]; for (int y = 0; y < height; y++) { copyOfGrid[y] = new GridCell[width]; for (int x = 0; x < width; x++) { copyOfGrid[y][x] = grid[y][x]; } } for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (grid[y][x] != GridCell.Empty) { int occupiedAdjacentSeats = 0; // left if (FindFirstChair(x, y, -1, 0) == GridCell.OccupiedChair) { occupiedAdjacentSeats++; } // right if (FindFirstChair(x, y, +1, 0) == GridCell.OccupiedChair) { occupiedAdjacentSeats++; } // top if (FindFirstChair(x, y, 0, -1) == GridCell.OccupiedChair) { occupiedAdjacentSeats++; } // bottom if (FindFirstChair(x, y, 0, +1) == GridCell.OccupiedChair) { occupiedAdjacentSeats++; } // tl if (FindFirstChair(x, y, -1, -1) == GridCell.OccupiedChair) { occupiedAdjacentSeats++; } // bl if (FindFirstChair(x, y, -1, +1) == GridCell.OccupiedChair) { occupiedAdjacentSeats++; } // tr if (FindFirstChair(x, y, +1, -1) == GridCell.OccupiedChair) { occupiedAdjacentSeats++; } // br if (FindFirstChair(x, y, +1, +1) == GridCell.OccupiedChair) { occupiedAdjacentSeats++; } if (grid[y][x] == GridCell.AvailableChair) { if (occupiedAdjacentSeats == 0) { changeMade = true; copyOfGrid[y][x] = GridCell.OccupiedChair; } } else if (grid[y][x] == GridCell.OccupiedChair) { if (occupiedAdjacentSeats >= 5) { changeMade = true; copyOfGrid[y][x] = GridCell.AvailableChair; } } } } } grid = copyOfGrid; } occupiedSeatCount = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (grid[y][x] == GridCell.OccupiedChair) { occupiedSeatCount++; } } } Console.WriteLine(occupiedSeatCount); Console.ReadLine(); }