public Node(Ingrediens ing, int amount) { Ing = ing; Amount = amount; AmountLeft = 100 - amount; IngIx = 0; }
public Node(Node prev, Ingrediens ing, int amount) { Prev = prev; Ing = ing; Amount = amount; AmountLeft = prev.AmountLeft - amount; IngIx = prev.IngIx + 1; }
/// <summary> /// Här deklareras HällSmetFrån. /// </summary> /// <param name="kastrullen"> Ett objekt av kastrullen<see cref="Kastrull"/>.</param> internal void HällSmetFrån(Kastrull kastrullen) { for (int i = kastrullen.Ingredienser.Count - 1; i >= 0; i--) { Ingrediens ingrediens = kastrullen.Ingredienser[i]; Smet.Add(ingrediens); kastrullen.Ingredienser.Remove(ingrediens); } kastrullen.Ingredienser.Clear(); }
public void Calculate() { var input = File.ReadAllLines("day15.txt"); var regex = new Regex(@"(?<name>\w+): capacity (?<capacity>-?\d+), durability (?<durability>-?\d+), flavor (?<flavor>-?\d+), texture (?<texture>-?\d+), calories (?<calories>-?\d+)"); var list = new List <Ingrediens>(); foreach (var ingredien in input) { var match = regex.Match(ingredien); var ing = new Ingrediens { Name = match.Groups["name"].Value, Capacity = int.Parse(match.Groups["capacity"].Value), Durability = int.Parse(match.Groups["durability"].Value), Flavor = int.Parse(match.Groups["flavor"].Value), Texture = int.Parse(match.Groups["texture"].Value), Calories = int.Parse(match.Groups["calories"].Value) }; list.Add(ing); } var searchList = new Stack <Node>(); for (int i = 0; i <= 100; i++) { var node = new Node(list[0], i); searchList.Push(node); } int highScore = int.MinValue; Node bestNode; while (searchList.Count > 0) { var node = searchList.Pop(); if (node.IngIx == list.Count - 2) { var lastNode = new Node(node, list[node.IngIx + 1], node.AmountLeft); if (lastNode.Calories() == 500) { int score = lastNode.Score(); if (score > highScore) { highScore = score; bestNode = lastNode; } } continue; } for (int amount = 0; amount <= node.AmountLeft; amount++) { var nextNode = new Node(node, list[node.IngIx + 1], amount); searchList.Push(nextNode); } } }