public static void Problem() { Console.WriteLine("Input file:"); string fileName = Console.ReadLine(); while (!File.Exists(fileName)) { Console.WriteLine("file not found - try again:"); fileName = Console.ReadLine(); } int x = 0; int y = 0; var colors = new Dictionary <int, Dictionary <int, long> >(); int direction = 0; Computer c = new Computer(fileName); c.Run(); while (c.Processor.InstructionPointer >= 0) { //read output queue, move as instructed while (c.Processor.OutputQueue.Count > 0) { long colorToPaint = c.Processor.OutputQueue.Dequeue(); if (!colors.ContainsKey(x)) { colors[x] = new Dictionary <int, long>(); } colors[x][y] = colorToPaint; direction = Turn(direction, c.Processor.OutputQueue.Dequeue()); switch (direction) { case 0: y--; break; case 90: x++; break; case 180: y++; break; case 270: x--; break; } } if (!colors.ContainsKey(x) || !colors[x].ContainsKey(y)) { c.Processor.Input(0); } else { c.Processor.Input(colors[x][y]); } c.Run(); } //read any remaining queue once processor is complete while (c.Processor.OutputQueue.Count > 0) { long colorToPaint = c.Processor.OutputQueue.Dequeue(); if (!colors.ContainsKey(x)) { colors[x] = new Dictionary <int, long>(); } colors[x][y] = colorToPaint; direction = Turn(direction, c.Processor.OutputQueue.Dequeue()); switch (direction) { case 0: y--; break; case 90: x++; break; case 180: y++; break; case 270: x--; break; } } //count painted cells int painted = 0; foreach (int keyX in colors.Keys) { foreach (int keyY in colors[keyX].Keys) { painted++; } } Console.WriteLine(painted); }
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}"); }
public static void Problem2() { Console.WriteLine("Input file:"); string fileName = Console.ReadLine(); while (!File.Exists(fileName)) { Console.WriteLine("file not found - try again:"); fileName = Console.ReadLine(); } int x = 0; int y = 0; var colors = new Dictionary <int, Dictionary <int, long> >(); int direction = 0; Computer c = new Computer(fileName); //tell computer first input is painted; c.Processor.Input(1); c.Run(); while (c.Processor.InstructionPointer >= 0) { //read output queue, move as instructed while (c.Processor.OutputQueue.Count > 0) { long colorToPaint = c.Processor.OutputQueue.Dequeue(); if (!colors.ContainsKey(x)) { colors[x] = new Dictionary <int, long>(); } colors[x][y] = colorToPaint; direction = Turn(direction, c.Processor.OutputQueue.Dequeue()); switch (direction) { case 0: y--; break; case 90: x++; break; case 180: y++; break; case 270: x--; break; } } if (!colors.ContainsKey(x) || !colors[x].ContainsKey(y)) { c.Processor.Input(0); } else { c.Processor.Input(colors[x][y]); } c.Run(); } //read any remaining queue once processor is complete while (c.Processor.OutputQueue.Count > 0) { long colorToPaint = c.Processor.OutputQueue.Dequeue(); if (!colors.ContainsKey(x)) { colors[x] = new Dictionary <int, long>(); } colors[x][y] = colorToPaint; direction = Turn(direction, c.Processor.OutputQueue.Dequeue()); switch (direction) { case 0: y--; break; case 90: x++; break; case 180: y++; break; case 270: x--; break; } } int minX = 0; int maxX = 0; int minY = 0; int maxY = 0; foreach (int xKey in colors.Keys) { minX = Math.Min(xKey, minX); maxX = Math.Max(xKey, maxX); foreach (int yKey in colors[xKey].Keys) { minY = Math.Min(yKey, minY); maxY = Math.Max(yKey, maxY); } } for (int y1 = minY; y1 <= maxY; y1++) { StringBuilder s = new StringBuilder(); for (int x1 = minX; x1 <= maxX; x1++) { if (!colors.ContainsKey(x1) || !colors[x1].ContainsKey(y1)) { s.Append(" "); } else { s.Append(colors[x1][y1] == 0 ? " " : "#"); } } Console.WriteLine(s.ToString()); } }