コード例 #1
0
        public static long Puzzle1()
        {
            var memory = new Dictionary <long, long>();

            var maskOr  = default(long);
            var maskAnd = default(long);

            foreach (var line in Input.LoadLines("Puzzles.Input.input14.txt"))
            {
                var match = MaskDecoder.Match(line);
                if (match.Success)
                {
                    var mask = match.Groups[1].Value;
                    maskOr  = Convert.ToInt64(mask.Replace('X', '0'), 2);
                    maskAnd = ~Convert.ToInt64(mask.Replace('1', 'X').Replace('0', '1').Replace('X', '0'), 2);
                }
                else
                {
                    var result  = AssignmentDecoder.Match(line);
                    var address = long.Parse(result.Groups[1].Value);
                    var value   = long.Parse(result.Groups[2].Value);
                    memory[address] = value & maskAnd | maskOr;
                }
            }

            return(memory.Values.Sum());
        }
コード例 #2
0
        public static long Puzzle2()
        {
            var memory = new Dictionary <long, long>();

            var mask = string.Empty;

            foreach (var line in Input.LoadLines("Puzzles.Input.input14.txt"))
            {
                var match = MaskDecoder.Match(line);
                if (match.Success)
                {
                    mask = match.Groups[1].Value;
                }
                else
                {
                    var result  = AssignmentDecoder.Match(line);
                    var address = long.Parse(result.Groups[1].Value);
                    var value   = long.Parse(result.Groups[2].Value);

                    foreach (var singleAddress in PermuteAddress(
                                 address | Convert.ToInt64(mask.Replace('X', '0'), 2),
                                 mask
                                 .Reverse()
                                 .Select((c, i) => (c, i))
                                 .Where(t => t.c == 'X')
                                 .Select(t => 0x1L << t.i)
                                 .ToArray()))
                    {
                        memory[singleAddress] = value;
                    }
                }
            }

            return(memory.Values.Sum());
        }
コード例 #3
0
        public static void Main(string[] args)
        {
            var maskDecoders = new MaskDecoder[] { new NetworkMaskDecoder(), new HostMaskDecoder() };
            var maskAdress   = new MaskAdress("255.0.0.0");

            foreach (var maskDecoder in maskDecoders)
            {
                maskDecoder.Interpret(maskAdress);
            }

            Console.WriteLine($"Decoded Network Address: {maskAdress.Translation.ElementAt(0)}");
            Console.WriteLine($"Decoded Host Address: {maskAdress.Translation.ElementAt(1)}");

            Console.ReadKey();
        }
コード例 #4
0
 public W32Decoder(MaskDecoder subDecoders)
 {
     this.subDecoders = subDecoders;
 }