Exemple #1
0
        protected override object SolvePart1()
        {
            var ipReg        = int.Parse(Input[0].Replace("#ip ", ""));
            var instructions = Input.Skip(1)
                               .Select(a => a.Split(' '))
                               .Select(d => new ChronalInstructionEngine.ChronalInstruction(new[] { Translation[d[0]], int.Parse(d[1]), int.Parse(d[2]), int.Parse(d[3]) }))
                               .ToArray();


            var engine = new ChronalInstructionEngine(ipReg);

            var instructionPointer = 0;
            var registers          = new[] { 0, 0, 0, 0, 0, 0 };

            while (instructionPointer >= 0 && instructionPointer <= instructions.Length)
            {
                (instructionPointer, registers) = engine.Execute(registers, instructions[instructionPointer], instructionPointer);
            }

            return(registers[0]);
        }
        protected override object SolvePart2()
        {
            var data    = File.ReadAllText(@"2018\AdventOfCode201816.txt").Split("\n\n\n");
            var samples = data[0]
                          .Split("\n\n", StringSplitOptions.RemoveEmptyEntries)
                          .Select(
                s =>
            {
                var set = Regex.Matches(s, @"\d+")
                          .Cast <Match>()
                          .Select(m => int.Parse(m.Value))
                          .ToArray();

                var br  = set.Take(4).ToArray();
                var ins = set.Skip(4).Take(4).ToArray();
                var ar  = set.Skip(8).ToArray();

                return(new ChronalInstructionEngine.ChronalInstructionSample(br, ar, new ChronalInstructionEngine.ChronalInstruction(ins)));
            })
                          .ToArray();

            var code = data[1]
                       .Split("\n", StringSplitOptions.RemoveEmptyEntries)
                       .Select(
                s =>
            {
                return(new ChronalInstructionEngine.ChronalInstruction(Regex.Matches(s, @"\d+")
                                                                       .Cast <Match>()
                                                                       .Select(m => int.Parse(m.Value))
                                                                       .ToArray()));
            })
                       .ToArray();

            var opCodes = Enumerable.Range(0, 16).ToDictionary(i => i, i => new List <int>());

            foreach (var s in samples)
            {
                opCodes[s.OpCode].AddRange(s.PossibleInstructions().Where(v => !opCodes[s.OpCode].Contains(v)));
            }

            var realOpCodes = opCodes.OrderBy(o => o.Value.Count).ToList();

            for (var i = 0; i < realOpCodes.Count; i++)
            {
                for (var j = 0; j < realOpCodes.Count; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    realOpCodes[j].Value.Remove(realOpCodes[i].Value.Single());
                    realOpCodes = realOpCodes.OrderBy(o => o.Value.Count).ToList();
                }
            }

            var translation = realOpCodes.OrderBy(r => r.Key).Select(d => d.Value.Single()).ToArray();
            var engine      = new ChronalInstructionEngine(translation);
            var registers   = code.Aggregate(new[] { 0, 0, 0, 0 }, (current, c) => engine.Execute(current, c).Register);

            return(registers[0]);
        }