コード例 #1
0
        private List <long> ApplyAddressMask(long address)
        {
            // apply a mask, to the floating array where each bit represents an entry and whether it is active
            // then check all bits and set the address bit at the index held in floating to whether the bit in the mask that belongs to that index is set.
            List <long> addresses = new List <long>();

            address |= force1;
            //maskValues counts upwards to get all combinations of floating numbers (Mask XX becomes 00 to 11)
            long floatCountMask = Bitwise.GetBitMask(floating.Count);

            for (int maskValues = 0; maskValues <= floatCountMask; maskValues++)
            {
                long newAddress = address;
                //check each bit in the current floating mask and set the bit in the adress that is referenced in the position this bit belongs to
                for (int i = 0; i < floating.Count; i++)
                {
                    newAddress = Bitwise.SetBit(newAddress, floating[i], Bitwise.IsBitSet(maskValues, i));
                }
                addresses.Add(newAddress);
            }
            return(addresses);
        }
コード例 #2
0
 private void RefreshMasks(string mask)
 {
     //our number is only 36 bits long. Get a 36 bit long fully set mask
     force0   = Bitwise.GetBitMask(36);
     force1   = 0;
     floating = new List <int>();
     for (int i = 0; i < mask.Length; i++)
     {//set the bits as given in the mask
         if (mask[i] == '1')
         {
             force1 = Bitwise.SetBit(force1, mask.Length - 1 - i, true);
         }
         if (mask[i] == '0')
         {
             force0 = Bitwise.SetBit(force0, mask.Length - 1 - i, false);
         }
         if (mask[i] == 'X')
         {
             floating.Add(mask.Length - 1 - i);
         }
     }
     floating.Reverse();
 }
コード例 #3
0
ファイル: Day18.cs プロジェクト: ntellos13/AdventOfCode
        private List <Node> TraceRecursive(Node start, long collectedKeys, out double newCost, int depth = 0, string pathText = "")
        {
            long dictKey = Bitwise.SetBit(collectedKeys, start.GetKeyBitPos() + 27, true);

            if (knownPaths.ContainsKey(dictKey))
            {
                newCost = knownPaths[dictKey].Item1;
                return(knownPaths[dictKey].Item2.ToList());
            }

            var reachable = GetReachableNodes(pois);
            var keys      = reachable.Where(x => x.Key != '\0' && !Bitwise.IsBitSet(collectedKeys, x.GetKeyBitPos())).ToList();

            newCost = 0;
            if (keys.Count == 0)
            {
                long mask = Bitwise.GetBitMask(this.keys.Count);
                if ((collectedKeys & mask) == mask)
                {
                    return(new List <Node>());
                }
                else
                {
                    return(null);
                }
            }

            double      selCost   = 0;
            List <Node> selection = null;
            Node        selected  = null;

            for (int i = 0; i < keys.Count; i++)
            {
                var key = keys[i];
                collectedKeys = Bitwise.SetBit(collectedKeys, key.GetKeyBitPos(), true);
                double      currCost;
                List <Node> currSel;
                var         unlocked = new List <Node>();

                //Console.SetCursorPosition(0, maxHeight * (printouts) + singleHeight);
                //Console.Write(outAssis.GetNextProgressChar() + depth.ToString() + " ");
                //Console.CursorLeft = 10;
                //Console.Write((pathText + key.Key).PadRight(20));
                Console.Write(key.Key);

                foreach (Node door in doors)
                {
                    if (door.TryUnlock(key.Key))
                    {
                        unlocked.Add(door);
                    }
                }
                var path = pathfind.GetPath(start, key, out List <BaseNodeConnection> cons, out double partCost).Select(x => (Node)x).ToList();
                if (path.Last() == start)
                {
                    continue;
                }
                var newKey = path.FirstOrDefault(x => x.Key != '\0' && x != key && x != start && !Bitwise.IsBitSet(collectedKeys, x.GetKeyBitPos()));
                if (newKey != null)
                {
                    collectedKeys = Bitwise.SetBit(collectedKeys, key.GetKeyBitPos(), false);
                    key           = newKey;
                    path          = pathfind.GetPath(start, key, out cons, out partCost).Select(x => (Node)x).ToList();
                    if (path.Last() == start)
                    {
                        continue;
                    }
                    collectedKeys = Bitwise.SetBit(collectedKeys, key.GetKeyBitPos(), true);
                }


                //PrintConnections(connections, maxHeight * printouts + singleHeight, false);
                //PrintConnections(cons, maxHeight * printouts + singleHeight, true);
                //Console.CursorLeft = key.X;
                //Console.CursorTop = key.Y + maxHeight * printouts + singleHeight;
                //Console.Write("*");

                currSel = TraceRecursive(key, collectedKeys, out currCost, depth + 1, pathText + key.Key);
                Console.CursorLeft--;
                Console.Write(" ");
                Console.CursorLeft--;


                if (currSel != null && currCost + partCost < selCost || selCost == 0)
                {
                    selection = currSel;
                    selCost   = currCost + partCost;
                    selected  = key;
                }
                collectedKeys = Bitwise.SetBit(collectedKeys, key.GetKeyBitPos(), false);
                foreach (Node door in unlocked)
                {
                    door.IsUnlocked = false;
                }
            }
            if (selection != null && selected != null)
            {
                selection.Insert(0, selected);
            }
            newCost = selCost;

            //dictKey = Bitwise.SetBit(collectedKeys, selected.GetKeyBitPos() + 27, true);
            if (!knownPaths.ContainsKey(dictKey))
            {
                knownPaths.Add(dictKey, new Tuple <double, List <Node> >(newCost, selection?.ToList()));
            }
            return(selection);
        }