Exemplo n.º 1
0
        public ulong ExecutePart2(string[] commands)
        {
            Dictionary <ulong, ulong> memory = new Dictionary <ulong, ulong>();
            string mask = "";

            foreach (var input in commands)
            {
                if (input.StartsWith("mask"))
                {
                    var parts = input.Split(" = ");
                    mask = parts[1];
                }
                if (input.StartsWith("mem"))
                {
                    int   startIndex             = input.IndexOf('[') + 1;
                    int   lenght                 = input.IndexOf(']') - startIndex;
                    var   initialMemoryDirection = ulong.Parse(input.Substring(startIndex, lenght));
                    var   memoryDirectionMasked  = Day14Extensions.ApplyMask2(initialMemoryDirection, mask);
                    ulong value = ulong.Parse(input.Split(" = ")[1]);

                    foreach (var memoryDirection in memoryDirectionMasked)
                    {
                        if (memory.ContainsKey(memoryDirection))
                        {
                            memory[memoryDirection] = value;
                        }
                        else
                        {
                            memory.Add(memoryDirection, value);
                        }
                    }
                }
            }

            ulong result = 0;

            foreach (var item in memory)
            {
                result = result + item.Value;
            }
            return(result);
        }
Exemplo n.º 2
0
        public ulong ExecutePart1()
        {
            Dictionary <int, ulong> memory = new Dictionary <int, ulong>();
            string mask = "";

            foreach (var input in _input)
            {
                if (input.StartsWith("mask"))
                {
                    var parts = input.Split(" = ");
                    mask = parts[1];
                }
                if (input.StartsWith("mem"))
                {
                    int   startIndex      = input.IndexOf('[') + 1;
                    int   lenght          = input.IndexOf(']') - startIndex;
                    int   memoryDirection = int.Parse(input.Substring(startIndex, lenght));
                    ulong originalValue   = ulong.Parse(input.Split(" = ")[1]);
                    ulong value           = Day14Extensions.ApplyMask(originalValue, mask);
                    if (memory.ContainsKey(memoryDirection))
                    {
                        memory[memoryDirection] = value;
                    }
                    else
                    {
                        memory.Add(memoryDirection, value);
                    }
                }
            }

            ulong result = 0;

            foreach (var item in memory)
            {
                result = result + item.Value;
            }
            return(result);
        }