public static void Execute(string input) { Console.Clear(); long[] code = Array.ConvertAll(input.Split(','), long.Parse); RunReturnValue part1 = RunComputer(code, 0); RunReturnValue part2 = RunComputer(code, 1); StringBuilder b = new StringBuilder(); for (int y = part2.Max.Y; y >= part2.Min.Y; y--) { for (int x = part2.Min.X; x <= part2.Max.X; x++) { var k = new IntPoint(x, y); b.Append(part2.Map.ContainsKey(k) ? (part2.Map[k] == 1 ? "█" : " ") : " "); } b.Append("\n"); } double cycles = part1.Cycles + part2.Cycles; double seconds = part1.Seconds + part2.Seconds; double cyps = cycles / seconds; Console.Clear(); Console.WriteLine($"Part 1: {part1.Map.Count}\nImage:"); Console.WriteLine(b); Console.WriteLine($"Execution time: {seconds:N}, Cycles: {cycles:N0}, Avg cy/s: {cyps:N}"); }
private static RunReturnValue RunComputer(long[] code, int defaultValue) { IntcodeComputer robot = new IntcodeComputer(code, true).Run(); Dictionary <IntPoint, int> map = new Dictionary <IntPoint, int>(); IntPoint l = new IntPoint(0, 0); IntPoint min = new IntPoint(int.MaxValue, int.MaxValue); IntPoint max = new IntPoint(int.MinValue, int.MinValue); Direction d = Direction.Up; while (robot.GetState != IntcodeComputer.State.Halted) { int currentSquare = map.ContainsKey(l) ? map[l] : defaultValue; int newSquare = (int)robot.QueueInput(currentSquare).Run().OutputQueue.Dequeue(); int turn = (int)robot.OutputQueue.Dequeue(); map[l] = newSquare; d = MakeTurn(d, turn); l += DirectionToVector(d); if (l.X > max.X || l.Y > max.Y) { max = new IntPoint(Math.Max(l.X, max.X), Math.Max(l.Y, max.Y)); } if (l.X < min.X || l.Y < max.Y) { min = new IntPoint(Math.Min(l.X, min.X), Math.Min(l.Y, min.Y)); } //DrawCenter(map, l, d, map.Count, $"After move"); //Thread.Sleep(10); } RunReturnValue ret = new RunReturnValue(); ret.Map = map; ret.Min = min; ret.Max = max; ret.Cycles = robot.Cycles; ret.Seconds = robot.ExecutionTime.TotalSeconds; return(ret); }