Hello ScriptingHelpers!
This is not a "question" -- it is a challenge. There are no prizes.
Code Golf
Code golf is a "game" where you try to make a program as small as possible. Note: this usually means throwing things like speed and good-practice out the window.
The smallest program "wins". For this code golf competition, you are required to use Lua. Post your solution (or a link to your solution in Hastebin to avoid spoilers) and give a brief explanation of how you did it. If you can, include a "pretty" version of your answer that is more readable.
Scoring
Your answer will be a single function called f which takes in input and returns the correct output.
Your score is the number of bytes your solution uses (which is characters, including spaces, newlines, etc).
Thus a solution that looked like this:
f=function()return 5 end
would have a score of 24.
The Task
Given input that loos like {1, {{3}}, {5, 6}, {{{{8}}}}, 1}, you need to return the deepest "depth" of the nested brackets. For example, in {5, {9}}, the answer is 2 since 9 is inside a list that is inside a list.
The elements of the lists are always positive integers, and the brackets are always { and }.
You can assume the answer is always at least 1. i.e., your program does not need to work for f(5).
You may not assume that the elements have things in them. e.g. f{{{}}} is still 3 even though no numbers are inside.
Task A
The first version of your function takes a string as input.
Here is sample input/output:
f "{1}" --> 1
f "{1, 2, {{3}}, {}}" --> 3
f "{{1, 2, 3}}" --> 2
f "{3, {3, {3}, 3}, 3}" --> 3
f "{{{{1}, 2}, {3, {4}}}}" --> 4
f "{1, {{3}}, {5, 6}, {{{{8}}}}, 1}" --> 5
f "{1, {{2, 3, {{4}, 5}, 6, {7, 8}}, 9, {10, {{{11}}}}, 12, 13}, 14}" --> 6
f "{{{{{{{3}}}}}}}" --> 7
Task B
The second version of your input actually takes lists of lists as input.
f {1} --> 1
f {1, 2, {{3}}, {}} --> 3
f {{1, 2, 3}} --> 2
f {3, {3, {3}, 3}, 3} --> 3
f {{{{1}, 2}, {3, {4}}}} --> 4
f {1, {{3}}, {5, 6}, {{{{8}}}}, 1} --> 5
f {1, {{2, 3, {{4}, 5}, 6, {7, 8}}, 9, {10, {{{11}}}}, 12, 13}, 14} --> 6
f {{{{{{{3}}}}}}} --> 7
Leaderboard - A (Strings)
| Place | User | Score |
|---|
- | XAXA | 99
- | BlueTaslem | 110
Leaderboard - B (Lists)
| Place | User | Score |
|---|
- | NullSenseStudio | 95
- | BlueTaslem | 106

3 comments
You can drop one byte by substituting the
pairsiterator withnext.Even shorter yet: replace
type(o)=="table"withpcall(next,o)!Quite creative.