public static void Problems() { Console.WriteLine("Input file:"); string fileName = Console.ReadLine(); while (!File.Exists(fileName)) { Console.WriteLine("file not found - try again:"); fileName = Console.ReadLine(); } Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var comp = new Computer(fileName); string[] instructions = new string[] { "NOT A J\n", "NOT B T\n", "OR J T\n", "NOT C J\n", "OR J T\n", "OR T J\n", "AND D J\n", //at this point, will jump if D is present unless ABCD "WALK\n" }; foreach (string s in instructions) { foreach (char c in s) { comp.Input(c); } } comp.Run(); StringBuilder sb = new StringBuilder(); while (comp.Output.Count > 0) { long next = comp.Output.Dequeue(); if (next > 128) { sb.Append(next.ToString()); sb.Append(Environment.NewLine); } else { sb.Append((char)next); } } Console.Write(sb); Console.WriteLine(""); Console.WriteLine("Part 2:"); instructions = new string[] { "NOT A J\n", "NOT B T\n", "OR J T\n", "NOT C J\n", "OR J T\n", "OR T J\n", "AND D J\n", //at this point, will jump if D is present unless ABCD "AND E T\n", "OR H T\n", "AND T J\n", //same as step 1 execpt don't jump if !(E|H) -- don't jump if can neither jump nor walk after this jump "RUN\n" }; comp = new Computer(fileName); foreach (string s in instructions) { foreach (char c in s) { comp.Input(c); } } comp.Run(); while (comp.Output.Count > 0) { long next = comp.Output.Dequeue(); if (next > 128) { sb.Append(next.ToString()); sb.Append(Environment.NewLine); } else { sb.Append((char)next); } } Console.Write(sb); Console.WriteLine(""); stopwatch.Stop(); Console.WriteLine($"Time to complete (ms): {stopwatch.ElapsedMilliseconds}"); }
public static void Problems() { Console.WriteLine("Input file:"); string fileName = Console.ReadLine(); while (!File.Exists(fileName)) { Console.WriteLine("file not found - try again:"); fileName = Console.ReadLine(); } int output = 0; for (int x = 0; x < 50; x++) { for (int y = 0; y < 50; y++) { Computer c = new Computer(fileName); c.Input(x); c.Input(y); c.Run(); if (c.Output.Dequeue() == 1) { output++; } } } Console.WriteLine(output); //part 2 bool foundAnswer = false; int x1 = 0; for (int y = 0; y < 10000 && !foundAnswer; y++) { bool found1 = false; for (int x = x1; x < 10000 && !found1; x++) { Computer c = new Computer(fileName); c.Input(x); c.Input(y); c.Run(); long val = c.Output.Dequeue(); if (val == 1) { found1 = true; x1 = x; //go down 99 rows, then over until the first 1 is found - mark that as tempX for now int tempX = x; int tempY = y + 99; long val2 = 0; while (val2 == 0 && tempX < 10000) { Computer c2 = new Computer(fileName); c2.Input(tempX); c2.Input(tempY); c2.Run(); val2 = c2.Output.Dequeue(); if (val2 == 0) { tempX++; } } //check if tempX+99 at original y is a 1 Computer c3 = new Computer(fileName); c3.Input(tempX + 99); c3.Input(y); c3.Run(); long val3 = c3.Output.Dequeue(); if (val3 == 1) { foundAnswer = true; Console.WriteLine(tempX * 10000 + y); } } } //for loop x } //for loop y } //Problems()
public static void Problems() { Console.WriteLine("Input file:"); string fileName = Console.ReadLine(); while (!File.Exists(fileName)) { Console.WriteLine("file not found - try again:"); fileName = Console.ReadLine(); } List <Computer> computers = new List <Computer>(); for (int i = 0; i < 50; i++) { var computer = new Computer(fileName); computer.Input(i); computer.Run(); computers.Add(computer); } bool part1Complete = false; bool part2Complete = false; long natX = 0; long natY = 0; HashSet <int> waiting = new HashSet <int>(); long natYDelivered = Int64.MinValue; while (!part1Complete || !part2Complete) { for (int i = 0; i < 50; i++) { if (i == 0 && waiting.Count == 50) { computers[i].Input(natX); computers[i].Input(natY); if (natY == natYDelivered) { part2Complete = true; Console.WriteLine($"problem 2: {natYDelivered}"); } else { natYDelivered = natY; } waiting.Remove(0); } //check for messages on the output queue while (computers[i].Output.Count > 0) { int destination = Convert.ToInt32(computers[i].Output.Dequeue()); long x = computers[i].Output.Dequeue(); long y = computers[i].Output.Dequeue(); if (destination == 255) { natX = x; natY = y; if (!part1Complete) { Console.WriteLine($"part 1: {y}"); part1Complete = true; } } else { //deliver messages to the appropriate computer computers[destination].Input(x); computers[destination].Input(y); if (waiting.Contains(destination)) { waiting.Remove(destination); } } } //if this computer's input queue is empty, queue -1 per Advent of Code day 23 instructions if (!computers[i].Processor.HasInput) { waiting.Add(i); computers[i].Input(-1); } else { if (waiting.Contains(i)) { waiting.Remove(i); } } computers[i].Run(); } } }
public static void Problems() { bool visualize = false; Console.WriteLine("Input file:"); string fileName = Console.ReadLine(); while (!File.Exists(fileName)) { Console.WriteLine("file not found - try again:"); fileName = Console.ReadLine(); } Computer comp = new Computer(fileName); comp.Input(76); comp.Run(); List <string> lines = new List <string>(); HashSet <int> sc = new HashSet <int>(); StringBuilder s = new StringBuilder(); while (comp.Output.Count > 0) { long next = comp.Output.Dequeue(); if (next == 10) { if (s.Length > 0) { lines.Add(s.ToString()); } s = new StringBuilder(); } else { s.Append((char)next); } } if (s.Length > 0) { lines.Add(s.ToString()); } long AP = 0; for (int r = 1; r < lines.Count - 1; r++) { for (int c = 1; c < lines[r].Length - 1; c++) { //check this and left/right/top/bottom for #^v<> if (IsScaffold(lines[r][c]) && IsScaffold(lines[r - 1][c]) && IsScaffold(lines[r + 1][c]) && IsScaffold(lines[r][c - 1]) && IsScaffold(lines[r][c + 1])) { AP += r * c; } } } /*Visualization*/ if (visualize) { foreach (string line in lines) { Console.WriteLine(line); } } //part 2... //(find robot) int x = 0; int y = 0; for (int r = 0; r < lines.Count; r++) { for (int c = 0; c < lines[r].Length; c++) { if (IsScaffold(lines[r][c])) { sc.Add(r * 100 + c); } if (IsRobot(lines[r][c])) { x = c; y = r; } } } //find full walk path string //first attempt - don't turn until necessary // (visualization of provided input says this is possible with only one way to turn at each endpoint) StringBuilder path = new StringBuilder(); HashSet <int> found = new HashSet <int>(); found.Add(x * 100 + y); int forward = 0; char direction = lines[y][x]; while (found.Count < sc.Count) { switch (direction) { case '^': if (y != 0 && IsScaffold(lines[y - 1][x])) { forward++; y--; found.Add(x * 100 + y); } else if (x != 0 && IsScaffold(lines[y][x - 1])) { path.Append(forward.ToString()); forward = 0; path.Append("L"); direction = '<'; } else if (x != (lines[y].Length - 1) && IsScaffold(lines[y][x + 1])) { path.Append(forward.ToString()); forward = 0; path.Append("R"); direction = '>'; } else { throw new Exception("no where to go"); } break; case '>': if (x != (lines[y].Length - 1) && IsScaffold(lines[y][x + 1])) { forward++; x++; found.Add(x * 100 + y); } else if (y != 0 && IsScaffold(lines[y - 1][x])) { path.Append(forward.ToString()); forward = 0; path.Append("L"); direction = '^'; } else if (y != (lines.Count - 1) && IsScaffold(lines[y + 1][x])) { path.Append(forward.ToString()); forward = 0; path.Append("R"); direction = 'v'; } else { throw new Exception("no where to go"); } break; case '<': if (x != 0 && IsScaffold(lines[y][x - 1])) { forward++; x--; found.Add(x * 100 + y); } else if (y != 0 && IsScaffold(lines[y - 1][x])) { path.Append(forward.ToString()); forward = 0; path.Append("R"); direction = '^'; } else if (y != (lines.Count - 1) && IsScaffold(lines[y + 1][x])) { path.Append(forward.ToString()); forward = 0; path.Append("L"); direction = 'v'; } else { throw new Exception("no where to go"); } break; case 'v': if (y != (lines.Count - 1) && IsScaffold(lines[y + 1][x])) { forward++; y++; found.Add(x * 100 + y); } else if (x != 0 && IsScaffold(lines[y][x - 1])) { path.Append(forward.ToString()); forward = 0; path.Append("R"); direction = '<'; } else if (x != (lines[y].Length - 1) && IsScaffold(lines[y][x + 1])) { path.Append(forward.ToString()); forward = 0; path.Append("L"); direction = '>'; } else { throw new Exception("no where to go"); } break; } } path.Append(forward); //break into combinations of 3 substrings not greater than 20 characters (including commas between letters and numbers ) //done visually - may rewrite with algorithm later to work with other possible input paths... /* * L4L4L6R10L6 L4L4L6R10L6 L12L6R10L6 R8R10L6 R8R10L6 L4L4L6R10L6 R8R10L6 L12L6R10L6 R8R10L6 L12L6R10L6 * AAAAAAAAAAA AAAAAAAAAAA BBBBBBBBBB CCCCCCC CCCCCCC AAAAAAAAAAA CCCCCCC BBBBBBBBBB CCCCCCC BBBBBBBBBB * */ String MainInstructions = "A,A,B,C,C,A,C,B,C,B"; String A = "L,4,L,4,L,6,R,10,L,6"; String B = "L,12,L,6,R,10,L,6"; String C = "R,8,R,10,L,6"; Computer c2 = new Computer(fileName); c2.Ram[0] = 2; foreach (var ins in MainInstructions) { c2.Input(ins); } c2.Input(10); foreach (var ins in A) { c2.Input(ins); } c2.Input(10); foreach (var ins in B) { c2.Input(ins); } c2.Input(10); foreach (var ins in C) { c2.Input(ins); } c2.Input(10); c2.Input(visualize ? 'y' : 'n'); c2.Input(10); c2.Run(); //Console.WriteLine(); StringBuilder output = new StringBuilder(); while (c2.Output.Count > 1) { long val = c2.Output.Dequeue(); if (visualize) { if (val == 10) { Console.WriteLine(output.ToString()); if (output.Length == 0) { //for visualization of map System.Threading.Thread.Sleep(45); Console.Clear(); } output = new StringBuilder(); } else { output.Append((char)val); } } } //if (output.Length > 0) Console.WriteLine(output.ToString()); long result = c2.Output.Dequeue(); //print expected directions if (visualize) { Console.WriteLine(path.ToString().TrimStart('0')); } //result with provided input was L4L4L6R10L6L4L4L6R10L6L12L6R10L6R8R10L6R8R10L6L4L4L6R10L6R8R10L6L12L6R10L6R8R10L6L12L6R10L6 Console.WriteLine($"Part 1: {AP}"); Console.WriteLine($"Part 2: {result}"); }