示例#1
0
        private void ApplyMask2(BitMask mask, Dictionary <long, long> memory)
        {
            foreach (var instruction in mask.ValueToMemoryPosition)
            {
                var addressesToWrite = ApplyMaskToAddress(mask.Mask, instruction.MemoryAddress);

                foreach (var address in addressesToWrite)
                {
                    var addressAsNumber = HelperMethods.ConvertBinaryFromString(address);
                    if (!memory.ContainsKey(addressAsNumber))
                    {
                        memory.Add(addressAsNumber, 0);
                    }
                    memory[addressAsNumber] = HelperMethods.ConvertBinaryFromString(instruction.Value);
                }
            }
        }
示例#2
0
        protected override void ParseInput()
        {
            masks = new List <BitMask>();
            var     lines = ReadInput();
            BitMask mask  = new BitMask();
            var     cycle = 0;

            foreach (var line in lines)
            {
                var memoryAddress = Regex.Match(line, "^mem\\[([0-9]*)\\] = ([0-9]*)");
                var maskMatch     = Regex.Match(line, "^mask = ([X01]*)");
                if (maskMatch.Success || cycle == lines.Length - 1)
                {
                    if (cycle == lines.Length - 1)
                    {
                        mask.ValueToMemoryPosition.Add(new Instruction
                        {
                            MemoryAddress = long.Parse(memoryAddress.Groups[1].Value),
                            Value         = memoryAddress.Groups[2].Value
                        });;
                    }

                    if (!string.IsNullOrEmpty(mask.Mask))
                    {
                        masks.Add(mask);
                    }
                    mask      = new BitMask();
                    mask.Mask = maskMatch.Groups[1].Value;
                }
                else if (memoryAddress.Success)
                {
                    mask.ValueToMemoryPosition.Add(new Instruction
                    {
                        MemoryAddress = long.Parse(memoryAddress.Groups[1].Value),
                        Value         = memoryAddress.Groups[2].Value
                    });;
                }
                cycle++;
            }
        }