4
\$\begingroup\$

This is a challenge about the the tricks and optimizations that can be used when golfing in CJam. CJam golfers may recognize many of the tricks involved. However, unfamiliar approaches and constructs may be involved, so take a look at the CJam tips as well as the CJam instruction reference if you get stuck. Solutions may be tested on TIO or with the JavaScript interpreter.

Goal: There are 10 problems, each with a CJam snippet for you to optimize. Your goal is to create something equivalent but shorter. The reference solutions total 148 bytes. Your goal is to beat that by as much as possible.

The winner will go to the submission that solves all 10 problems with the smallest total number of bytes. Tiebreaker is earlier post.

Answering: Please spoiler your entire answer, except for your total score. It is intended that you do not look at other people's answers before submitting your own.

Each submission should answer every problem and give the corresponding byte count, but feel free to use the reference implementation if you cannot improve it.

Details: All answers will take input via the stack and output to the stack. You can assume that the input will be the only thing on the stack (or, if there is no input, the stack will be empty).

Problem 1: Add one to every element in a list (link):

e# 5 bytes
{1+}%

Problem 2: Given a list, create a list of partial sums. For example, [1 2 3] becomes [1 3 6] (link):

e# 16 bytes
_,\a*ee{~\)<:+}%

Problem 3: Create the following "times table" array (it doesn't need the whitespace, it's just there to make it look better):

[[1  2  3  4  5  6  7  8  9]
 [2  4  6  8 10 12 14 16 18]
 [3  6  9 12 15 18 21 24 27]
 [4  8 12 16 20 24 28 32 36]
 [5 10 15 20 25 30 35 40 45]
 [6 12 18 24 30 36 42 48 54]
 [7 14 21 28 35 42 49 56 63]
 [8 16 24 32 40 48 56 64 72]
 [9 18 27 36 45 54 63 72 81]]

Reference (link):

e# 15 bytes
9{):T;9{)T*}%}%

Problem 4: Add one to each element in a nested array (link):

e# 22 bytes
{{_`0='[={T}{)}?}%}:T~

Problem 5: Given a list with an odd number of elements, delete the center one (link):

e# 14 bytes
__,2/_@<@@W*>+

Problem 6: Given a list of bits (1 or 0), remove all leading zeroes (link):

e# 6 bytes
_1+1#>

Problem 7: Given a list of positive numbers, return the same list, but with every element replaced by 1 (link):

e# 5 bytes
{;1}%

Problem 8: Generate the list [1 2 3 4 5 4 3 2 1] (link):

e# 12 bytes
XYZ4 5 4ZYX]

Problem 9: Generate the list [[[0 1] [1 2] [2 0]] [[0 0] [1 1] [2 2]] [[0 2] [1 0] [2 1]]] (link):

e# 41 bytes
TX][XY][YT]][[TT][XX][YY]][[TY][XT][YX]]]

Problem 10: Take two arrays on the stack and "interleave" them, so "foo" "bar" becomes "fboaor". You can assume the arrays have the same length (link):

e# 12 bytes
]_0=,,\ff=:+

Note: Most of this challenge was stolen from Pyth practice.

\$\endgroup\$
4

0

Browse other questions tagged or ask your own question.