Example #1
0
        private static (int opCode, List <Day16Opcode> possibleOpcodes) GetPossibleOpcodes(long[] input, long[] output, long[] instruction)
        {
            List <Day16Opcode> matchingCodes = new List <Day16Opcode>();

            foreach (var item in allOpcodes)
            {
                var machine = new Day16Machine(input);
                machine.ProcessInstruction(item, instruction);
                if (machine.ReadonlyRegister.SequenceEqual(output))
                {
                    matchingCodes.Add(item);
                }
            }

            return((int)instruction[0], matchingCodes);
        }
Example #2
0
        public static void Run()
        {
            List <(int opCode, List <Day16Opcode> possibleOpcodes)> learningResutls = new List <(int opCode, List <Day16Opcode> possibleOpcodes)>();
            var lines = System.IO.File.ReadAllLines(@"Day16\Learning.txt");

            for (int i = 0; i < lines.Length;)
            {
                var line = lines[i++];
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                var input = new string(line.SkipWhile(x => x != '[').Skip(1).TakeWhile(x => x != ']').ToArray()).Split(',').Select(x => long.Parse(x.Trim())).ToArray();
                line = lines[i++];
                var instruction = line.Split(' ').Select(x => long.Parse(x)).ToArray();
                line = lines[i++];
                var output = new string(line.SkipWhile(x => x != '[').Skip(1).TakeWhile(x => x != ']').ToArray()).Split(',').Select(x => long.Parse(x.Trim())).ToArray();

                learningResutls.Add(GetPossibleOpcodes(input, output, instruction));
            }

            var allRemainingCodes = allOpcodes.ToList();

            Dictionary <int, Day16Opcode> numberToOpcodeMapping = new Dictionary <int, Day16Opcode>();

            while (allRemainingCodes.Any())
            {
                for (int opcodeNumber = 0; opcodeNumber < 16; opcodeNumber++)
                {
                    if (numberToOpcodeMapping.ContainsKey(opcodeNumber))
                    {
                        continue;
                    }

                    var remainnigCodes = allRemainingCodes.ToList();

                    foreach (var learningResult in learningResutls)
                    {
                        if (learningResult.opCode != opcodeNumber)
                        {
                            continue;
                        }

                        remainnigCodes = remainnigCodes.Intersect(learningResult.possibleOpcodes).ToList();

                        if (remainnigCodes.Count == 1)
                        {
                            break;
                        }
                    }
                    if (remainnigCodes.Count == 1)
                    {
                        numberToOpcodeMapping[opcodeNumber] = remainnigCodes[0];
                    }
                }

                allRemainingCodes.RemoveAll(x => numberToOpcodeMapping.ContainsValue(x));
            }

            var machine = new Day16Machine(new long[] { 0, 0, 0, 0 });

            foreach (var item in System.IO.File.ReadAllLines(@"Day16\Input.txt"))
            {
                var data = item.Split(' ').Select(x => long.Parse(x)).ToArray();
                machine.ProcessInstruction(numberToOpcodeMapping[(int)data[0]], data);
            }

            Console.WriteLine(string.Join(",", machine.ReadonlyRegister.Select(x => x.ToString())));
        }