private bool ValidatePassport(string passport, int version) { var requiredFields = new HashSet <string>() { "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid" }; var fields = StringParsers.SplitDelimitedStringIntoStringList(passport, new char[0]); foreach (var field in fields) { var fieldAndValue = field.Split(':'); var fieldName = fieldAndValue[0]; var value = fieldAndValue[1]; if (ValidateField(fieldName, value, version)) { if (requiredFields.Contains(fieldName)) { requiredFields.Remove(fieldName); } } } return(requiredFields.Count() == 0); }
public string SolveFirstStar(StreamReader reader) { var estimate = int.Parse(reader.ReadLine()); var nextLine = reader.ReadLine(); var buses = StringParsers.SplitDelimitedStringIntoStringList(nextLine, ",".ToCharArray()); long shortestWait = Int64.MaxValue; int closestBus = 0; foreach (var bus in buses) { if (bus == "x") { continue; } var busInterval = int.Parse(bus); // Effectively computes -estimate % busInterval, picks the smallest positive unique solution var waitTime = (-estimate % busInterval + busInterval) % busInterval; if (waitTime < shortestWait) { shortestWait = waitTime; closestBus = busInterval; } } return((shortestWait * closestBus).ToString()); }
public string SolveSecondStar(StreamReader reader) { // Ignore first line of file reader.ReadLine(); var nextLine = reader.ReadLine(); var buses = StringParsers.SplitDelimitedStringIntoStringList(nextLine, ",".ToCharArray()); var busIntervals = new List <long>(); var busDepartures = new List <int>(); for (int i = 0; i < buses.Count(); i++) { var busInterval = buses[i]; if (busInterval != "x") { busIntervals.Add(uint.Parse(busInterval)); busDepartures.Add(i); } } // We have a list of bus intervals that looks like this: // busIntervals = [ n0, n1, n2, ... ] // We also have a list of required departure times for each bus (relative to the departure of bus n0): // busDepartures = [ 0, 1, 5, ... ] // This can be interpreted as n1 leaves at time t=0, n2 leaves at time t=1, n3 leaves at time t=5... // We can generalize this to say: // busIntervals[i] leaves busDepartures[i] minutes after bus n0, for 0 <= i < busIntervals.Count(); // For a given n0 departure time t, we can compute the wait time from t to the departure of bus[i] as follows: // actualDeparture[0] = 0 // actualDeparture[i] = -t mod busIntervals[i] // We are looking for an n0 departure time t such that -t = busDepartures[i] (mod busIntervals[i]) // This problem is a system of congruence equations of the form x = a[i] (mod n[i]) for 0 <= i <= busIntervals.Count() var solution = SolveSystemOfCongruenceEquations(busIntervals, busDepartures); // The solution we get back is the single unique solution that exists in the interval (-N, 0) // where N is defined below. The way we defined the problem, the solution to this system is equivalent to -t, // so to find that actual value of t we need to multiply by -1 return((-solution).ToString()); }
private void BuildBagList(List <string> listOfDefinitions) { BagList = new Dictionary <string, Bag>(); foreach (var definition in listOfDefinitions) { var bagAndInnerBags = definition.Split("bags contain").Select(x => x.Trim()).ToList(); var currentBagColor = bagAndInnerBags[0]; var innerBags = StringParsers.SplitDelimitedStringIntoStringList(bagAndInnerBags[1], ",".ToCharArray()); var currentBag = GetOrCreateBag(currentBagColor); if (bagAndInnerBags[1] != "no other bags.") { foreach (var innerBagDescription in innerBags) { var innerBagTuple = CreateBagTuple(innerBagDescription); currentBag.InnerBags.Add(innerBagTuple); var innerBag = GetOrCreateBag(innerBagTuple.Item2); innerBag.ParentColors.Add(currentBagColor); } } } }