সিজোম, 139 বাইট
ভাল মনে হচ্ছে এমন কিছু আসতে বেশ কয়েক ঘন্টা সময় লেগেছিল। এটি মনে হচ্ছে যে সময়টিকে আক্রমণাত্মকভাবে অপ্টিমাইজ করতে সময় লাগে সিজেএম কোডটি কোডের আকারের ক্ষেত্রে ও (এন) এর চেয়ে বড় কিছু ...
আপনি এটি অনলাইনে চেষ্টা করে দেখতে পারেন , তবে যে কোনও ইনপুটটির জন্য সবচেয়ে ভাল পাথ কমপক্ষে operations টি অপারেশন বা তাই, আপনার সম্ভবত এটি একটি দ্রুত দোভাষী দিয়ে অফলাইনে চেষ্টা করা উচিত ।
Squished:
q_'$-_'^-:T;'^#\'^-'$#W{)2$5Y$5b+{:D[L"_T<W%_N#)_@>N+N#X-Ue>+-"_"W%-U"--2'<t2'>t'++'(')]=~0e>T,e<D3/1$T<N\+W%N#X?:X;}/2$-}g5b{" ^v<>"=}%]W=
সম্প্রসারিত এবং মন্তব্য করা হয়েছে:
q "Read the input";
_'$- "Remove the end marker";
_'^-:T; "Remove the start marker and save the text";
'^# "With only the end marker removed, locate the start marker";
\'^-'$# "With only the start marker removed, locate the end marker";
W "Initialize the path number to -1";
{ "Do...";
) "Increment the path number";
2$ "Initialize the cursor position to that of the start marker";
5Y$5b+ "Convert the path number to base 5, then add a leading 5
(the leading 5 will act to initialize the column memory)";
{:D "For each digit in the path digit string:";
[ "Begin cases:";
L "0: Do nothing";
"_T<W%_N#)_@>N+N#X-Ue>+-"
"REFS: [ 1 ][ 2 ][ 3 ]45
1: [1] Calculate the distance to the end of the previous
line (0 if no such line)
[2] Calculate the length of the previous line (0 if
no such line)
[3] Calculate the distance to move backwards in the
previous line as the maximum of the length of the
previous line minus the column memory and 0
[4] Calculate the total distance to move as the sum
of [1] and [3]
[5] Subtract [4] from the cursor position";
_"W%-U"- "2: Start with a base of the logic of case 1, but with a
few operations adjusted.";
-2'<t2'>t " [1] Calculate the distance to the *start* of the
*next* line (0 if no such line)
[2] Calculate the length of the *next* line (0 if no
such line)
[3] Calculate the distance to move *forwards* in the
*next* line as the *minimum* of the length of the
*next line* and *the column memory*
[4] Calculate the total distance to move as the sum
of [1] and [3]";
'++ " [5] *Add* [4] *to* the cursor position";
'( "3: Decrement the cursor position";
') "4: Increment the cursor position";
]=~ "Execute the case corresponding to the path digit mod 5";
0e>T,e< "Clamp the cursor position to [0, text length]";
D3/ "Check if the path digit is not 0, 1, or 2...";
1$T<N\+W%N# "Calculate the current column";
X?:X; "If the above check succeeded, update the column memory";
}/ "End for each";
2$- "Subtract the end marker position from the cursor position";
}g "... While the above subtraction is nonzero";
5b "Convert the path number to base 5";
{" ^v<>"=}% "Map each digit in the path string to its operation symbol";
]W= "Clean up";
সামগ্রিকভাবে, এটি একটি দুর্দান্ত সোজা সমাধান। এটি কোনও পাথ কাজ না করা অবধি 0 দিয়ে শুরু করে প্রতিটি পুনরাবৃত্তিকে বৃদ্ধি করে এমন একটি পাথ সংখ্যার বেস -5 উপস্থাপনার অঙ্কগুলি "সম্পাদন করে"। অঙ্কগুলি 1
- 4
উপরে, নীচে, বাম এবং ডানদিকে ক্রিয়াকলাপের মানচিত্র এবং 0
কিছুই করে না। সবেমাত্র একটি পথ ব্যবহার করে প্রথম পুনরাবৃত্তিটি 0
ডিজেনরেট কেসটিকে ক্যাচ করে। এর মধ্যে থাকা সমস্ত অন্যান্য পাথগুলি 0
কখনই নির্বাচিত হয় না কারণ এগুলি কেবল নো-অপ্সের সাথে ইতিমধ্যে পরীক্ষিত পাথগুলির সংস্করণ।
রাজ্যটি যতটা সম্ভব উপায়ের হিসাবে সংক্ষিপ্ত আকারে মডেল করা হয়েছে: শুরু এবং শেষ চিহ্নিতকারীগুলির সাহায্যে পাঠ্য অপসারণ করা হয়েছে, পাঠ্যে কার্সারের অবস্থান এবং "কলামের স্মৃতি" " নিউলাইনগুলি বেশিরভাগ অন্যান্য চরিত্রের মতোই আচরণ করা হয়, সুতরাং একটি সারির ধারণা নেই, এবং কার্সারের অবস্থানটি কেবল একটি সূচক। এটি বাম এবং ডান মৃতকে সরানো সহজ করে তোলে, যা কেবল হ্রাস এবং বৃদ্ধি হিসাবে প্রয়োগ করা হয় (পাঠ্যের আকারে ক্ল্যাম্পিং সহ)। উপরে এবং নীচে সরানো কিছুটা কৌশলযুক্ত তবে এখনও পরিচালনাযোগ্য।
কোড পুনরায় ব্যবহার হ'ল একটি অত্যন্ত গুরুত্বপূর্ণ অপ্টিমাইজেশন কৌশল। এর উদাহরণগুলির মধ্যে রয়েছে:
- এমনভাবে চলার জন্য কোডটি লেখার জন্য যেমন রান কোডে নিজের কোডটি লেখার চেয়ে কম যাওয়ার জন্য কোড উত্পন্ন করা ছোট। এটি কিছু সরানো এবং সরিয়ে / প্রতিস্থাপনের জন্য কোডটি অনুলিপি করে করা হয়।
- "কলাম মেমরি" আপডেট করা অপারেশনটির যুক্তিতে কোডিং করার পরিবর্তে শর্ত অনুসারে 3 দ্বারা বিভাজক পাথ ডিজিটের ভিত্তিতে করা হয়। এটি
5
পাথ স্ট্রিংয়ের শুরুতে একটি ডামি অপারেশন যুক্ত করে কলাম মেমোরিটির সূচনা করার অনুমতি দেয় , যা 0
বিজ্ঞপ্তি অ্যারে সূচিকরণের কারণে কোনও অপ-লজিক ব্যবহার করতে ঘটে এবং সেখানে কেবল 5 সংজ্ঞায়িত ক্রিয়াকলাপ হয়।
সামগ্রিকভাবে, কীভাবে এটি প্রকাশিত হয়েছিল তা নিয়ে আমি খুব খুশি। এটি অবশ্যই আমি তারিখের একটি কোড গল্ফ উত্তর রেখেছি সবচেয়ে কাজ (একটি টুইট মধ্যে ফিট করে এমন কিছু জন্য ??)। যদিও রান সময়টি বেশ বেহাল। সিজেএম শুরু করার মতো দ্রুততম ভাষা নয় এবং এই অ্যালগরিদমের ও (মি * 5 এন ) এর মতো কোনও জটিলতা রয়েছে , যেখানে এম ইনপুটটির আকার এবং এন আউটপুটটির আকার। ভাল জিনিস গতি গণনা করা হয় না!