示例#1
0
        private static bool part1Check(int[] input, int[] instruction, int[] output)
        {
            int i = 0;

            i += AsmEmu.addi(input, instruction).SequenceEqual(output) ? 1 : 0;
            i += AsmEmu.addr(input, instruction).SequenceEqual(output) ? 1 : 0;
            i += AsmEmu.bani(input, instruction).SequenceEqual(output) ? 1 : 0;
            i += AsmEmu.banr(input, instruction).SequenceEqual(output) ? 1 : 0;
            i += AsmEmu.bori(input, instruction).SequenceEqual(output) ? 1 : 0;
            i += AsmEmu.borr(input, instruction).SequenceEqual(output) ? 1 : 0;
            i += AsmEmu.eqir(input, instruction).SequenceEqual(output) ? 1 : 0;
            i += AsmEmu.eqri(input, instruction).SequenceEqual(output) ? 1 : 0;
            i += AsmEmu.eqrr(input, instruction).SequenceEqual(output) ? 1 : 0;
            i += AsmEmu.gtir(input, instruction).SequenceEqual(output) ? 1 : 0;
            i += AsmEmu.gtri(input, instruction).SequenceEqual(output) ? 1 : 0;
            i += AsmEmu.gtrr(input, instruction).SequenceEqual(output) ? 1 : 0;
            i += AsmEmu.muli(input, instruction).SequenceEqual(output) ? 1 : 0;
            i += AsmEmu.mulr(input, instruction).SequenceEqual(output) ? 1 : 0;
            i += AsmEmu.seti(input, instruction).SequenceEqual(output) ? 1 : 0;
            i += AsmEmu.setr(input, instruction).SequenceEqual(output) ? 1 : 0;
            if (i >= 3)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#2
0
 private static void FigureOutCodes(int[] input, int[] instruction, int[] output)
 {
     if (!AsmEmu.addi(input, instruction).SequenceEqual(output))
     {
         opCodeDict[instruction[0]].Remove("addi");
     }
     if (!AsmEmu.addr(input, instruction).SequenceEqual(output))
     {
         opCodeDict[instruction[0]].Remove("addr");
     }
     if (!AsmEmu.bani(input, instruction).SequenceEqual(output))
     {
         opCodeDict[instruction[0]].Remove("bani");
     }
     if (!AsmEmu.banr(input, instruction).SequenceEqual(output))
     {
         opCodeDict[instruction[0]].Remove("banr");
     }
     if (!AsmEmu.bori(input, instruction).SequenceEqual(output))
     {
         opCodeDict[instruction[0]].Remove("bori");
     }
     if (!AsmEmu.borr(input, instruction).SequenceEqual(output))
     {
         opCodeDict[instruction[0]].Remove("borr");
     }
     if (!AsmEmu.eqir(input, instruction).SequenceEqual(output))
     {
         opCodeDict[instruction[0]].Remove("eqir");
     }
     if (!AsmEmu.eqri(input, instruction).SequenceEqual(output))
     {
         opCodeDict[instruction[0]].Remove("eqri");
     }
     if (!AsmEmu.eqrr(input, instruction).SequenceEqual(output))
     {
         opCodeDict[instruction[0]].Remove("eqrr");
     }
     if (!AsmEmu.gtir(input, instruction).SequenceEqual(output))
     {
         opCodeDict[instruction[0]].Remove("gtir");
     }
     if (!AsmEmu.gtri(input, instruction).SequenceEqual(output))
     {
         opCodeDict[instruction[0]].Remove("gtri");
     }
     if (!AsmEmu.gtrr(input, instruction).SequenceEqual(output))
     {
         opCodeDict[instruction[0]].Remove("gtrr");
     }
     if (!AsmEmu.muli(input, instruction).SequenceEqual(output))
     {
         opCodeDict[instruction[0]].Remove("muli");
     }
     if (!AsmEmu.mulr(input, instruction).SequenceEqual(output))
     {
         opCodeDict[instruction[0]].Remove("mulr");
     }
     if (!AsmEmu.seti(input, instruction).SequenceEqual(output))
     {
         opCodeDict[instruction[0]].Remove("seti");
     }
     if (!AsmEmu.setr(input, instruction).SequenceEqual(output))
     {
         opCodeDict[instruction[0]].Remove("setr");
     }
 }
示例#3
0
        public static void Run()
        {
            List <string> linesInputFirst  = File.ReadAllLines(Util.ReadFromInputFolder("16_A")).Where(arg => !string.IsNullOrWhiteSpace(arg)).ToList();
            List <string> linesInputSecond = File.ReadAllLines(Util.ReadFromInputFolder("16_B")).ToList();

            int countBehavior = 0;

            string[] opBase = new string[]
            { "addi", "addr", "bani", "banr",
              "bori", "borr", "eqir", "eqri",
              "eqrr", "gtir", "gtri", "gtrr",
              "muli", "mulr", "seti", "setr" };
            for (int i = 0; i < 16; i++)
            {
                opCodeDict.Add(i, new List <string>(opBase));
            }

            //part 1
            while (linesInputFirst.Count > 0)
            {
                string line1 = linesInputFirst.First();
                string line2 = linesInputFirst.Skip(1).First();
                string line3 = linesInputFirst.Skip(2).First();
                linesInputFirst.RemoveRange(0, 3);

                string[] lin1array = line1.Between("[", "]").Split(',');
                string[] lin3array = line3.Between("[", "]").Split(',');

                int[] input          = Array.ConvertAll(lin1array, s => Convert.ToInt32(s.Trim()));
                int[] instruction    = Array.ConvertAll(line2.Split(' '), s => Convert.ToInt32(s.Trim()));
                int[] expectedOutput = Array.ConvertAll(lin3array, s => Convert.ToInt32(s.Trim()));

                FigureOutCodes(input, instruction, expectedOutput);
                if (part1Check(input, instruction, expectedOutput))
                {
                    countBehavior++;
                }
            }

            //part 2 figure the registers
            while (opCodeDict.Any(r => r.Value.Count > 1))
            {
                var orderedCandidates = opCodeDict.OrderBy(r => r.Value.Count);
                foreach (var pair in orderedCandidates)
                {
                    if (pair.Value.Count == 1)
                    {
                        opCodeDict.Where(r => r.Key != pair.Key).AsParallel().ForAll(r => r.Value.RemoveAll(w => w == pair.Value.First()));
                    }
                }
            }

            //part 2 run program
            int[] baseRegister = new int[4] {
                0, 0, 0, 0
            };
            foreach (string line in linesInputSecond)
            {
                int[]  instruction = Array.ConvertAll(line.Split(' '), s => Convert.ToInt32(s.Trim()));
                string opCode      = opCodeDict[instruction[0]].First();
                if (opCode == "addi")
                {
                    baseRegister = AsmEmu.addi(baseRegister, instruction);
                }
                else if (opCode == "addr")
                {
                    baseRegister = AsmEmu.addr(baseRegister, instruction);
                }
                else if (opCode == "bani")
                {
                    baseRegister = AsmEmu.bani(baseRegister, instruction);
                }
                else if (opCode == "banr")
                {
                    baseRegister = AsmEmu.banr(baseRegister, instruction);
                }
                else if (opCode == "bori")
                {
                    baseRegister = AsmEmu.bori(baseRegister, instruction);
                }
                else if (opCode == "borr")
                {
                    baseRegister = AsmEmu.borr(baseRegister, instruction);
                }
                else if (opCode == "eqir")
                {
                    baseRegister = AsmEmu.eqir(baseRegister, instruction);
                }
                else if (opCode == "eqri")
                {
                    baseRegister = AsmEmu.eqri(baseRegister, instruction);
                }
                else if (opCode == "eqrr")
                {
                    baseRegister = AsmEmu.eqrr(baseRegister, instruction);
                }
                else if (opCode == "gtir")
                {
                    baseRegister = AsmEmu.gtir(baseRegister, instruction);
                }
                else if (opCode == "gtri")
                {
                    baseRegister = AsmEmu.gtri(baseRegister, instruction);
                }
                else if (opCode == "gtrr")
                {
                    baseRegister = AsmEmu.gtrr(baseRegister, instruction);
                }
                else if (opCode == "muli")
                {
                    baseRegister = AsmEmu.muli(baseRegister, instruction);
                }
                else if (opCode == "mulr")
                {
                    baseRegister = AsmEmu.mulr(baseRegister, instruction);
                }
                else if (opCode == "seti")
                {
                    baseRegister = AsmEmu.seti(baseRegister, instruction);
                }
                else if (opCode == "setr")
                {
                    baseRegister = AsmEmu.setr(baseRegister, instruction);
                }
                else
                {
                    Debug.Assert(false);
                }
            }

            Console.WriteLine("PART 1: " + countBehavior);
            Console.WriteLine("PART 2: " + baseRegister[0] + " " + baseRegister[1] + " " + baseRegister[2] + " " + baseRegister[3]);
        }