/// <inheritdoc/> public bool Equals(StringBag other) { return(other != null && BagColor.Equals(other.BagColor) && ContainedBag.Equals(other.ContainedBag) && ContainedQuantity == other.ContainedQuantity); }
private void ParseInput(String inputLine) { BagColor = StringOps.SubStringPre(inputLine, "contain").Trim(); BagColor = BagColor.Replace("bags", "").Trim(); BagColor = BagColor.Replace("bag", "").Trim(); string working = StringOps.SubStringPost(inputLine, "contain").Trim(); working = working.Replace(".", ""); working = working.Replace("bags", ""); working = working.Replace("bag", ""); String[] bags = working.Split(','); foreach (string bag in bags) { if (!bag.Trim().Equals("no other")) { string qty = StringOps.SubStringPre(bag.Trim(), " "); int iqty = Convert.ToInt32(qty); string bagColour = StringOps.SubStringPost(bag.Trim(), " ").Trim(); containBags.Add(bagColour, iqty); } } }
private BagColor TransitionColor(BagColor color) { if (color == BagColor.Normal) { return(BagColor.Red); } if (color == BagColor.Red) { return(BagColor.Blue); } return(BagColor.Normal); }
private Sprite ColorToSprite(BagColor color) { switch (color) { case BagColor.Blue: return(BagSprites[0]); case BagColor.Red: return(BagSprites[1]); default: return(BagSprites[2]); } }
private Material ColorToMaterial(BagColor color) { switch (color) { case BagColor.Blue: return(BagMats[0]); case BagColor.Red: return(BagMats[1]); default: return(BagMats[2]); } }
public async ValueTask ExecuteAsync(IConsole console) { var sampleGraph = LoadGraph("input/07-sample"); var sampleGraph2 = LoadGraph("input/07-sample2"); var inputGraph = LoadGraph("input/07-input"); var shinyGold = new BagColor("shiny gold"); console.Output.WriteLine(AllColorsFor(sampleGraph, shinyGold).ToList().Count); console.Output.WriteLine(AllColorsFor(inputGraph, shinyGold).ToList().Count); console.Output.WriteLine(CountInsideBags(sampleGraph, shinyGold)); console.Output.WriteLine(CountInsideBags(sampleGraph2, shinyGold)); console.Output.WriteLine(CountInsideBags(inputGraph, shinyGold)); }
private static int FindNumberOfClicks(BagColor currentColor, BagColor wantedColor) { switch (currentColor) { case BagColor.Normal: return(wantedColor == BagColor.Red ? 1 : (wantedColor == BagColor.Blue ? 2 : 0)); case BagColor.Red: return(wantedColor == BagColor.Blue ? 1 : (wantedColor == BagColor.Normal ? 2 : 0)); case BagColor.Blue: return(wantedColor == BagColor.Normal ? 1 : (wantedColor == BagColor.Red ? 2 : 0)); default: throw new ArgumentException("Unknown color " + currentColor); } }
public static Rule Parse(string line) { var words = line.Split(new[] { ' ', ',', '.' }, StringSplitOptions.RemoveEmptyEntries); var outer = new BagColor($"{words[0]} {words[1]}"); if (words[4] is "no") { return(new Rule(outer, NoOtherBags)); } var inner = new Dictionary <BagColor, int>(); for (int i = 4; i < words.Length; i += 4) { inner.Add(new BagColor($"{words[i + 1]} {words[i + 2]}"), int.Parse(words[i])); } return(new Rule(outer, inner)); }
private static BagColor SwapColor(BagColor color) { return(color == BagColor.Blue ? BagColor.Red : BagColor.Blue); }
static IEnumerable <BagColor> ColorsFor(AdjacencyGraph <BagColor, BagEdge> graph, BagColor color) => graph .Edges .Where(e => e.Target == color) .Select(e => e.Source);
static IEnumerable <BagColor> _(AdjacencyGraph <BagColor, BagEdge> graph, BagColor color) { var parentColors = ColorsFor(graph, color).ToList(); return(parentColors.Concat(parentColors.SelectMany(pc => _(graph, pc)))); }
static IEnumerable <BagColor> AllColorsFor(AdjacencyGraph <BagColor, BagEdge> graph, BagColor color) { return(_(graph, color).Distinct());
static int CountInsideBags(AdjacencyGraph <BagColor, BagEdge> graph, BagColor toCount) => graph .OutEdges(toCount) .Select(e => e.Quantity + e.Quantity * CountInsideBags(graph, e.Target)) .Sum();
public record Rule(BagColor Outer, Dictionary <BagColor, int> Inner) {
public static IEnumerable <BagColor> GetRelatedColors(BagColor targetColor, Rule[] rules) => rules.Where(rule => rule.Inner.ContainsKey(targetColor)).Select(rule => rule.Outer);