Ejemplo n.º 1
0
        public override object Puzzle2(AOCInput input)
        {
            input.SetExampleInput(@"mask = 000000000000000000000000000000X1001X
mem[42] = 100
mask = 00000000000000000000000000000000X0XX
mem[26] = 1");

            string[] lines = input.GetInputLines();

            Dictionary <int, ulong> memory = new Dictionary <int, ulong>();

            ulong        mask  = 0;
            List <ulong> masks = new List <ulong>();

            foreach (string line in lines)
            {
                string[] split = line.Split(new string[] { "mem[", "]", "=", " " }, System.StringSplitOptions.RemoveEmptyEntries);

                if (split[0].Equals("mask"))
                {
                    mask = 0;

                    for (int i = 0; i < split[1].Length; ++i)
                    {
                        mask = mask << 1;

                        if (split[1][i] == '1')
                        {
                            mask++;
                        }
                    }

                    masks.Add(mask);

                    for (int i = 0; i < split[1].Length; ++i)
                    {
                        if (split[1][i] == 'X')
                        {
                            int maskCount = masks.Count;
                            for (int j = 0; j < maskCount; ++j)
                            {
                                ulong newMask = ((ulong)1 << split[1].Length - 1 - i);
                                newMask = newMask | masks[j];
                                masks.Add(newMask);
                            }
                        }
                    }
                }
                else
                {
                    int   address = int.Parse(split[0]);
                    ulong value   = ulong.Parse(split[1]);

                    for (int i = 0; i < masks.Count; ++i)
                    {
                        if (!memory.ContainsKey(address))
                        {
                            memory.Add(address, 0);
                        }

                        memory[address + i] = value | masks[i];
                    }
                }
            }

            ulong total = 0;

            for (int i = 0; i < memory.Count; ++i)
            {
                total += memory.ElementAt(i).Value;
            }

            return(total);
        }