private (IList <KeyValuePair <string, string> > Replacements, string Medicine) ParseInput(IPuzzleInput input) { List <KeyValuePair <string, string> > replacements = new(); var medicine = string.Empty; foreach (var line in input.ReadLines()) { var(key, value, _) = line.Split(" => "); key = key?.Trim() ?? string.Empty; value = value?.Trim() ?? string.Empty; if (string.IsNullOrWhiteSpace(key)) { // this is a blank line continue; } if (string.IsNullOrWhiteSpace(value)) { // this is the medicine molecule medicine = key; continue; } // this is a replacement replacements.Add(new KeyValuePair <string, string>(key, value)); } return(replacements, medicine); }
private IDictionary <string, Guest> ParseGuests(IPuzzleInput input) { var guests = new Dictionary <string, Guest>(); foreach (var line in input.ReadLines()) { var(name, _, direction, points, _, _, _, _, _, _, neighbor, rest) = line.Trim(' ', '.').Split(' '); if (!guests.ContainsKey(name)) { guests.Add(name, new Guest(new Dictionary <string, int>())); } var modifier = direction switch { "lose" => - 1, _ => 1 }; if (!guests[name].Neighbors.ContainsKey(neighbor)) { var number = int.Parse(points) * modifier; guests[name].Neighbors.Add(neighbor, number); } } return(guests); } }
private IList <int> ParseContainers(IPuzzleInput input) { List <int> containers = new(); foreach (var line in input.ReadLines()) { containers.Add(int.Parse(line)); } return(containers); }
private IList <Reindeer> ParseReindeer(IPuzzleInput input) { var list = new List <Reindeer>(); foreach (var line in input.ReadLines()) { var(name, _, _, speed, _, _, flySeconds, _, _, _, _, _, _, restSeconds, _) = line.Split(' '); list.Add(new Reindeer(name, int.Parse(speed), int.Parse(flySeconds), int.Parse(restSeconds))); } return(list); }
public _24(IPuzzleInput input) { var packages = new List <ulong>(); foreach (var line in input.ReadLines()) { if (ushort.TryParse(line, out var package)) { packages.Add(package); } } _packages = packages.OrderByDescending(p => p).ToArray(); }
private Character ParseEnemy(IPuzzleInput input) { Character enemy = new(); var type = enemy.GetType(); foreach (var line in input.ReadLines()) { var(key, value, _) = line.Split(":"); var property = type.GetProperty(key.Replace(" ", "")); if (property != null) { property.SetValue(enemy, int.Parse(value)); } } return(enemy); }
private IList <Ingredient> ParseIngredients(IPuzzleInput input) { var list = new List <Ingredient>(); var separators = new[] { ' ', ',', ':' }; foreach (var line in input.ReadLines()) { var(name, _, capacity, _, durability, _, flavor, _, texture, _, calories, _) = line.Split(separators, StringSplitOptions.RemoveEmptyEntries); list.Add(new Ingredient( name, int.Parse(capacity), int.Parse(durability), int.Parse(flavor), int.Parse(texture), int.Parse(calories) )); } return(list); }
private static IDictionary <string, Instruction> ParseInstructions(IPuzzleInput input) { var result = new Dictionary <string, Instruction>(); foreach (var line in input.ReadLines()) { var(inputs, wire, _) = line.Split(" -> "); var tokens = inputs.Split(" "); string leftOperand = null; string operatorStr = null; string rightOperand = null; switch (tokens.Length) { case 1: (leftOperand, _) = tokens; break; case 2: (operatorStr, rightOperand, _) = tokens; break; case 3: (leftOperand, operatorStr, rightOperand, _) = tokens; break; } Operator? @operator = null; if (!string.IsNullOrWhiteSpace(operatorStr) && Enum.TryParse <Operator>(operatorStr, true, out var operatorEnum)) { @operator = operatorEnum; } result.Add(wire, new Instruction(leftOperand, @operator, rightOperand)); } return(result); }