public static void Task2() { int ipIdx = 0; int instrPt = 0; int[] registers = new int[6]; List <OpCode> instructions = new List <OpCode>(); Dictionary <int, int> r0s = new Dictionary <int, int>(); using (StreamReader reader = new StreamReader(inputPath)) { string line; while ((line = reader.ReadLine()) != null) { if (line[0] == '#') { ipIdx = int.Parse(line[4].ToString()); } else { string[] instr = line.Split(' '); int[] nums = new int[] { int.Parse(instr[1]), int.Parse(instr[2]), int.Parse(instr[3]) }; instructions.Add(new OpCode(OpCode.GetCode(instr[0]), nums)); } } } registers = new int[6]; instrPt = 0; HashSet <int> r2s = new HashSet <int>(); while (true) { registers[ipIdx] = instrPt; if (instrPt == 28) { if (r2s.Contains(registers[2])) { break; } r2s.Add(registers[2]); } instructions[instrPt].Exec(registers); instrPt = registers[ipIdx]; instrPt++; } Console.WriteLine(r2s.Last()); }
public static void Task2() { int ipIdx = 0; int instrPt = 0; int[] registers = new int[6]; registers[0] = 1; List <OpCode> instructions = new List <OpCode>(); List <int> register4 = new List <int>(); const int PATTERN_TRESHOLD = 200; using (StreamReader reader = new StreamReader(inputPath)) { string line; while ((line = reader.ReadLine()) != null) { if (line[0] == '#') { ipIdx = int.Parse(line[4].ToString()); } else { string[] instr = line.Split(' '); int[] nums = new int[] { int.Parse(instr[1]), int.Parse(instr[2]), int.Parse(instr[3]) }; instructions.Add(new OpCode(OpCode.GetCode(instr[0]), nums)); } } } for (int i = 0; i < PATTERN_TRESHOLD; i++) { registers[ipIdx] = instrPt; instructions[instrPt].Exec(registers); register4.Add(registers[4]); instrPt = registers[ipIdx]; instrPt++; } var r4s = register4.GroupBy(x => x).Select(group => new { Number = group.Key, Count = group.Count() }); var r4 = r4s.Aggregate((l, r) => l.Count > r.Count ? l : r).Number; var primes = FindFactors(r4); int r0 = CalculateRegister0(primes); Console.WriteLine(r0); }
public static void Task1() { int ipIdx = 0; int instrPt = 0; int[] registers = new int[6]; List <OpCode> instructions = new List <OpCode>(); using (StreamReader reader = new StreamReader(inputPath)) { string line; while ((line = reader.ReadLine()) != null) { if (line[0] == '#') { ipIdx = int.Parse(line[4].ToString()); } else { string[] instr = line.Split(' '); int[] nums = new int[] { int.Parse(instr[1]), int.Parse(instr[2]), int.Parse(instr[3]) }; instructions.Add(new OpCode(OpCode.GetCode(instr[0]), nums)); } } } while (instrPt < instructions.Count) { registers[ipIdx] = instrPt; instructions[instrPt].Exec(registers); instrPt = registers[ipIdx]; instrPt++; } Console.WriteLine(registers[0]); }