| Bytes | Lang | Time | Link |
|---|---|---|---|
| 018 | Japt v2.0a0 | 180822T133934Z | Shaggy |
| 017 | 05AB1E | 180822T113759Z | Kevin Cr |
| 016 | Vyxal 2 j | 241031T231548Z | arutonee |
| 062 | Perl 6 | 180823T230855Z | Jo King |
| 150 | Java 11 | 180823T122832Z | Kevin Cr |
| 017 | Jelly | 180822T151451Z | Jonathan |
| 020 | Jelly | 180822T113513Z | user2027 |
| 081 | JavaScript Node.js | 180822T130830Z | Shieru A |
| 023 | Husk | 180822T115151Z | ბიმო |
| 037 | Retina 0.8.2 | 180822T123405Z | Neil |
| 095 | Python 2 | 180822T113750Z | TFeld |
Japt v2.0a0, 20 19 18 bytes
r/^ +/mÈÎpÓMg2+XÊz
r/^ +/mÈÎpÓMg2+XÊz :Implicit input of multi-line string
r :Replace
/^ +/m : RegEx
È : Pass matches through the following function as X
Î : First character
p : Repeat
Ó : Bitwise NOT of the negation of
Mg : Nth Fibonacci number, where n=
2+ : Add 2 to
XÊ : Length of X
z : Floor divided by 2
05AB1E, 22 21 17 bytes
|εgyðÛ©g-;ÝÅfO®ú,
The alternative formula \$Fib(\frac{n}{2}+2)-1\$ instead of \$\sum_{i=1}^{i=\frac{n}{2}}{Fib(i)}\$ (credit to @JonathanAllan's Jelly answer for this formula) is now 1 byte longer due to a bug in 05AB1E (requiring a ï before the Åf), instead of equal length:
|εgyðÛ©g-;ÌïÅf<®ú,
Explanation:
| # Take a list of all newline separated input-lines
ε # Loop over each line:
# (implicitly push the current line)
g # Pop and push its length
y # Push the current line again
ðÛ # Trim all its leading spaces
© # Store the result in variable `®` (without popping)
g # Pop and push its length as well
- # Subtract both lengths from one another
; # Halve it, because they are multiples of 2 indented spaces
Ý # Create a list in the range [0,n]
Åf # Get the i'th Fibonacci number for each
O # Sum them together
® # Push the current line with trimmed leading spaces
ú # Pad it with the Fibonacci-sum amount of leading spaces
, # Pop and print this with trailing newline
Vyxal 2 j, 16 bytes
↵ƛ:øL:£ḟ½ÞFẎ∑I¥+
↵ split on newlines
ƛ for each line...
:øL remove leading spaces
:£ push to the register
ḟ first index of original w/ trimmed substring
½ halve
ÞFẎ∑ sum of first n fibonacci numbers
I n spaces
¥+ add register
with flag j, which prints the stack joined with newlines
Java 11, 150 bytes
s->{for(var l:s.split("\n"))System.out.println(" ".repeat(f(l.length()-(l=l.stripLeading()).length())-1)+l);};int f(int n){return n<1?1:f(n-2)+f(n-4);}
Try it online (NOTE: Both String.repeat(int) and String.stripLeading() are emulated as repeat(String,int) and stripLeadingE(String) respectively, because Java 11 isn't on TIO yet.)
Explanation:
s->{ // Method with String parameter and no return-type
for(var l:s.split("\n"))
// Loop over the lines of the input:
System.out.println(// Print with trailing new-line:
" ".repeat( // Repeat a space this many times:
f( // Call the separated (recursive) method `f` with the parameter:
l.length()// The length of the line,
-(l=l.stripLeading()).length()
// minus the length of the line without leading spaces
)-1 // minus 1
)+l);}; // And append the line without leading spaces
int f(int n){ // Separated method with integer as both parameter and return-type
return n<1? // If the input is 0 or lower:
1 // Return 1
: // Else:
f(n-2)+f(n-4);}
// Return a recursive call to `n-2` plus a recursive call to `n-4`
Jelly, 17 bytes
Ỵµwt©⁶$HÆḞ€⁶ẋ;®)Y
A full program which prints the result.
How?
The indentations required for a line of the output is \$Fib(\frac{s}{2}+2)-1\$ where \$s\$ is the number of spaces in the line of the input, and:
$$Fib(\frac{s}{2}+2)-1=\sum_{i=1}^{i=\frac{s}{2}}{Fib(i)}$$
(\$i\$ could also start at \$0\$ since \$Fib(0)=0\$)
Ỵµwt©⁶$HÆḞ€⁶ẋ;®)Y - Main Link: list of characters
Ỵ - split at newlines
µ ) - for each line:
$ - last 2 links as a monad:
⁶ - literal space character
t - trim from both sides
© - (copy to register)
w - first index of that substring in the line
H - halve (this is (s+1)/2)
ÆḞ€ - Fibonacci of €ach (using an implicit range [1,2,...,s/2]
- where the (s+1)/2 is floored to s/2)
⁶ - literal space character
ẋ - repeat (vectorises) (e.g. [1,1,2,3,5] -> [[' '],[' '],[' ',' '],[' ',' ',' '],[' ',' ',' ',' ',' ']]
® - recall from register
; - concatenate
Y - join with newline characters
- implicit print
- ...smashes, so e.g. [[' '],[' '],[' ',' '],['x']] is just " x"
Jelly, 20 bytes
Ỵµ=⁶i0‘H‘ÆḞ’⁶ẋ;t⁶$)Y
Explanation
Let \$x\$ be the 1-indexing index of the first non-whitespace character (\$1,3,5,7,\dots\$).
Then the new indent is
$$F_{(x+1)/2+1}-1$$
where \$F\$ is the Fibonacci function.
JavaScript (Node.js), 87 81 bytes
x=>x.replace(/^ */gm,s=>" ".repeat((g=(n,a=b=1)=>n?g(n-2,b,b+=a):b-1)(s.length)))
For small indent levels (up to 15), 61 bytes using approximation:
x=>x.replace(/^ */gm,s=>" ".repeat(1.272**s.length*1.171-.5))
Use 1.27202 ** s.length * 1.17082 - .5 for indents up to 21 levels
Husk, 23 bytes
mȯF+M(R' !∫Θİf→½L)↕=' ¶
Explanation
m(F+M(R' !∫Θİf→½L)↕=' )¶ -- full program
¶ -- lines
m( ) -- map
↕=' -- | split on first non-space
M( ) -- | with the spaces
L -- | | length
½ -- | | halve
→ -- | | increment
İf -- | | Fibonacci numbers: [1,1,2,3,5..]
Θ -- | | prepend 0: [0,1,1,2,3,5..]
∫ -- | | cumulative sums: [0,1,2,4,7,12..]
! -- | | 1-based index
R' -- | | replicate that many spaces
F+ -- | join
Retina 0.8.2, 37 bytes
m`^
+`( + )( +)
$2$1$1
*
Try it online! Warning: Lots of white space. Explanation:
m`^
Prefix each line with space tab space tab. These keep track of the previous and current Fibonacci number.
+`( + )( +)
$2$1$1
For each two spaces of the original indentation, compute the next Fibonacci number by summing the previous two.
*
Subtract 1 from the result and delete the previous Fibonacci number.
Python 2, 111 109 100 95 bytes
g=lambda n:n<1or g(n-2)+g(n-4)
def f(I):
for l in I:L=l.lstrip();print~-g(len(l)-len(L))*' '+L
Saved:
- -5 bytes, thanks to ovs.