Sometimes, you may see something that seems rather easy to be achieved, you have a vision in your mind regarding it and it still takes 5 times more time and patience to get it. Today, after taking a look at what seemed to be rather easy task with printing on the console an egg, I was pretty sure that I can come up with a working solution within 15 minutes.
The task was similar to the one with the boats, its logic and methodology was exactly the same, but still I needed about 1.5 hours of what seemed to be an easily digestible task. Quite funny, eh?
The question is why? First, I had problems with the fact that the vertical lines of the egg were not defined in size. I created some easy formula to define them, but it did not work with more than 3 of the types. Then, it seemed that it is not that easy to reverse the first part of the egg, and after some 30 minutes of trying to do it, I simply decided to build it up again from scratch. Thus, the code written by me became 144 lines, with about 60% of it a redundant one.
Considering the fact, that I have already spent some hours writing the code, I have decided to publish it anyhow, without further edition. Yes, it can be upgraded and improved, but for the sake of the fact that it gives 100% on the tests, it is quite OK. Brace yourself, here it comes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Egg { static void Main() { int N = int.Parse(Console.ReadLine()); int height = 2 * N; int Eggwidth = 3 * N - 1; int drawingAreaWidth = 3 * N + 1; int counterHeight = 0; //Top Console.Write(new string('.', 1)); Console.Write(new string('.', N)); Console.Write(new string('*', Eggwidth - N * 2)); Console.Write(new string('.', N)); Console.Write(new string('.', 1)); counterHeight++; Console.WriteLine(); //print slope top side: int loopSize = N - 2; int mask = N; if (loopSize >= 1) { for (int i = 0; i < loopSize; i++) { if (mask < 1) { break; } Console.Write(new string('.', 1)); Console.Write(new string('.', mask - 2)); Console.Write(new string('*', 1)); Console.Write(new string('.', Eggwidth - mask * 2 + 2)); Console.Write(new string('*', 1)); Console.Write(new string('.', mask - 2)); Console.Write(new string('.', 1)); mask -= 2; counterHeight++; Console.WriteLine(); } } //print top sides: int maskWidth = (N * 2 - (counterHeight * 2)-2)/2; if (maskWidth > 0) { for (int i = 0; i < maskWidth; i++) { Console.Write(new string('.', 1)); Console.Write(new string('*', 1)); Console.Write(new string('.', Eggwidth - 2)); Console.Write(new string('*', 1)); Console.Write(new string('.', 1)); counterHeight++; Console.WriteLine(); } } //Printing the middle: Console.Write(new string('.', 1)); Console.Write(new string('*', 1)); for (int i = 0; i < Eggwidth - 2; i++) { if (i % 2 == 0) { Console.Write('@'); } else { Console.Write('.'); } } Console.Write(new string('*', 1)); Console.Write(new string('.', 1)); counterHeight++; Console.WriteLine(); //Second line of the middle: Console.Write(new string('.', 1)); Console.Write(new string('*', 1)); for (int i = 0; i < Eggwidth - 2; i++) { if (i % 2 == 1) { Console.Write('@'); } else { Console.Write('.'); } } Console.Write(new string('*', 1)); Console.Write(new string('.', 1)); counterHeight++; Console.WriteLine(); //print bottom: //print bottom sides: for (int i = 0; i < maskWidth; i++) { Console.Write(new string('.', 1)); Console.Write(new string('*', 1)); Console.Write(new string('.', Eggwidth - 2)); Console.Write(new string('*', 1)); Console.Write(new string('.', 1)); counterHeight++; Console.WriteLine(); } int loopsLeft = height - counterHeight - 1; int z = 0; for (int i = 0; i < loopsLeft; i++) { Console.Write(new string('.', 1)); Console.Write(new string('.', z)); Console.Write(new string('*', 1)); Console.Write(new string('.', Eggwidth-2*z-2)); Console.Write(new string('*', 1)); Console.Write(new string('.', z)); Console.Write(new string('.', 1)); z += 2; Console.WriteLine(); } Console.Write(new string('.', 1)); Console.Write(new string('.', N)); Console.Write(new string('*', Eggwidth - N * 2)); Console.Write(new string('.', N)); Console.Write(new string('.', 1)); counterHeight++; Console.WriteLine(); } } |
So, that’s all 🙂