| Bytes | Lang | Time | Link |
|---|---|---|---|
| 011 | BQN | 250410T115250Z | aqui8 |
| 016 | GolfScript | 250329T074411Z | Twilight |
| 037 | APLNARS | 250328T115158Z | Rosario |
| 052 | Retina | 250327T193720Z | Unrelate |
| 008 | Pyth | 250327T175420Z | CursorCo |
| 081 | Retina 0.8.2 | 250326T102511Z | Neil |
| 008 | 05AB1E | 250324T143739Z | Kevin Cr |
| 033 | R | 250326T082809Z | Glory2Uk |
| 038 | Ruby | 250325T123606Z | G B |
| 054 | Google Sheets | 250325T063306Z | z.. |
| 011 | APLDyalog Unicode | 250324T164743Z | att |
| 013 | Charcoal | 250325T011520Z | Neil |
| 013 | Uiua | 250324T153531Z | Joao-3 |
| 006 | Jelly | 250324T181625Z | Unrelate |
| 017 | x8664 machine code | 250324T174704Z | m90 |
| 026 | Haskell | 250324T135231Z | colossus |
| 030 | JavaScript Node.js | 250324T124117Z | Mukundan |
APL(NARS), 37 chars
{{2≥⍵:⍵-1⋄1+∇¯1+a[⍵]+a[⍵]=1}¨⍳≢a←⍳⍨⍵}
The input has to be one array of integers (not a scalar, a vector).
I have not much familiarity with this subject. It all start from
one function like d below
r←d w;l;i;k;a
i←1⋄l←≢w⋄p←0⋄r←⍬⋄a←⍳⍨w
→0×⍳i>l⋄→3×⍳i≤2⋄→3×⍳i=k←a[i]⋄p←r[k+k=1]
r,←p⋄(i p)+←1⋄→2
//14+23+40+17=94
this function d it seems fits the data test... It would work that
increment one counter p that represent deep that start from 0,
but if the number has index >2 and in the array of input
is already calculated the deep, than assign to p the deep
already calculated and continue as above. For root the deep is 0,
for all the node has parent root the deep is 1. The recursive function
is just the traslation of that d function from iterative
to recursive, i think will be more slow because it has to recaulculate
values. The complexity of the function d is the same of the function {⍳⍨⍵}
i think is len(input)^2 ({⍳⍨⍵} would return the first index where appear
the value in the input array), for the recursive function i don't
know complexity.
Test:
f←{{2≥⍵:⍵-1⋄1+∇¯1+a[⍵]+a[⍵]=1}¨⍳≢a←⍳⍨⍵}
f 0
┌1──┐
│ ¯1│
└~──┘
f ,0
┌1─┐
│ 0│
└~─┘
f 0 0
┌2───┐
│ 0 1│
└~───┘
f 0 0 1 2 3 4 1 6
┌8───────────────┐
│ 0 1 2 3 4 5 2 3│
└~───────────────┘
f 0 0 1 2 3 3 0 6 6 6 9
┌11────────────────────┐
│ 0 1 2 3 4 4 1 2 2 2 3│
└~─────────────────────┘
f 0 0 1 1 3 0 0 6 0 8 8 10 10 8
┌14──────────────────────────┐
│ 0 1 2 2 3 1 1 2 1 2 2 3 3 2│
└~───────────────────────────┘
f 0 0 1 1 3 0 0 6 0 8 8 10 10 3
┌14──────────────────────────┐
│ 0 1 2 2 3 1 1 2 1 2 2 3 3 3│
└~───────────────────────────┘
It is possible to golf the d function for have
r←d w;l;i;a
i←1⋄l←≢w⋄r←,0⋄a←⍳⍨w
→0×⍳l<i+←1⋄r,←1+r[¯1+a[i]+a[i]=1]⋄→2
//12+20+37=69
Retina, 52 bytes
vrL`.*\d\b
%(/^\d+ /+~`.*?(\d+)(#.*)?$
$1`\d+¶$$&#
#
Input is a space-separated list; output is newline-separated. (Test harness patches this to be less annoying.) Root-inclusive. Fundamentally 0.8.2-incompatible due to relying on eval stages for dynamic limits.
vrL`.*\d\b
Get each prefix of the input's elements on its own line. (The \d\b is to only get prefixes of the input's elements, and not all prefixes of its string representation!)
%(
introduces a group stage mapped over lines:
/^\d+ /+~`.*?(\d+)(#.*)?$
$1`\d+¶$$&#
The first stage loops while there's a space after the first run of digits /^\d+ /+, and executes an eval stage ~ which evaluates the result of replacing the entire string capturing the first run of digits .*?(\d+) followed by a pound sign or the end of the string (#.*)?$ with: another replace stage (implicit) which matches runs of digits \d+ and replaces only the run at the matched index $1` with the match $$& followed by a pound sign #.
#
The second stage in the group simply counts every pound sign on the line.
Pyth, 8 bytes
ml.u@+ZQ
Input is 0-indexed with root node omitted.
Explanation
ml.u@+ZQNd)Q # implicitly add Nd)Q
# implicitly assign Q = eval(input())
m Q # map over Q
l # length
.u d) # repeatedly call lambda N on d until the result repeats, returning a list of all intermediate results
@+ZQN # ([0]+Q)[N]
Retina 0.8.2, 81 bytes
+1`((_*)\d+ )(\d)
$1_$2$3
\d+
$*
+`_1
+`(_)+(?<=(#*)\B(?<-1> \w+)+)
#$2
^|#+
$.&
Try it online! Link includes test cases. Explanation:
+1`((_*)\d+ )(\d)
$1_$2$3
Generate (in unary using _s) the index of each node.
\d+
$*
Convert the parent indices to unary (using 1s).
+`_1
Subtract each parent index from its index.
+`(_)+(?<=(#*)\B(?<-1> \w+)+)
#$2
Replace the offsets with their depth (in unary using #s), repeating until all nodes have been processed. (Each iteration replaces the next layer of nodes.)
^|#+
$.&
Convert the depths to decimal.
05AB1E, 9 8 bytes
ÎvDyè>=ª
Port of @Mukundan314's Python answer, but outputs each value on a separated newline instead.
Root note is omitted.
Try it online or verify all test cases.
Could have been 7 bytes if we're allowed to input with omitted root note, but output with root note, by removing the =:
Try it online or verify all test cases.
Explanation:
Î # Push 0 and the input-list
v # Pop the list and loop over each value `y`:
D # Duplicate the current list,
# or the 0 in the first iteration
yè # Pop and push its y'th (0-based modular) value
> # Increase that by 1
= # Output it with trailing newline (without popping)
ª # Pop and add it to the list
# (convert the initial 0 to list of digits [0] before adding to it)
R, 33 bytes
\(x)Reduce(\(a,b)c(a,a[b+1]+1),x)
The golfiest way to work with vectors is to avoid counters. Therefore the best options are Reduce, for or Map, the last two were 38 and 48 bytes long, respectively.
Related: Mukundan314's answer, z..'s answer
Google Sheets, 54 bytes
Port of Mukundan314's answer
Expects input in A:A.
=REDUCE(0,TOCOL(A:A,1),LAMBDA(x,y,{x;INDEX(x,y+1)+1}))
APL(Dyalog Unicode), 11 bytes SBCS
(⊢⍪1+⌷∘,)⌿⊖
1-indexed.
( )⌿⊖ for each (non-root) node
⊢⍪ append
1+⌷ 1+ its parent's depth
∘, (fix initial case)
APL(Dyalog Unicode), 13 bytes SBCS
⊂(⊢⌊1+⌷)⍣≡⍳⍤≢
Also 1-indexed.
⍳⍤≢ starting from 1 2...n,
( )⍣≡ fixed point of:
⊢⌊ min with
⊂ 1+⌷ 1+ parents' depths
Charcoal, 13 bytes
FA⊞υ∧Lυ⊕§υιIυ
Try it online! Link is to verbose version of code. I/O includes root (input value is arbitrary). Explanation:
FA
Loop over the parent vector.
⊞υ∧Lυ⊕§υι
If this is the root, then push zero, otherwise push one more than the parent's depth.
Iυ
Output the depth vector.
Uiua, 15 13 bytes
∧(˜⊂+1⊸⊡)⊃↘↙1
Thanks to @Tbw for -2 bytes!
Takes in a 0-indexed array, with the root node included.
For every node, the program looks up the corresponding depth in the (partial) result array, then adds 1.
Jelly, 6 bytes
JịƬ>1S
Takes input 1-indexed with the root (test harness converts for convenience).
J Starting with the 1-index of each node,
ị 1-index each into the parent vector
Ƭ repeatedly until nothing changes (i.e. they're all 1).
>1S How many values are greater than 1 in each column?
The initial J is needed to distinguish the root from its immediate children, so that it takes 0 steps to reach itself but its children need 1. Omitting the root complicates things, as out-of-bounds indexing wraps around--the root is needed as a "sink" that keeps things 1 once they've been 1 once, because the loop keeps running until the entire vector hits a cycle, and the cycle needs to be convenient even if looping on individual elements (as in JịƬ€¹Ẉ’) because Ƭ is Jelly's only looping quick that reuses the right argument instead of Doing The Fibonacci Thing™.
x86-64 machine code, 17 bytes
57 59 83 0F FF AD 8B 04 81 FF C0 AB FF CA 75 F5 C3
Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes in RDI an address at which to place the depth vector, as an array of 32-bit integers; the address of the parent vector, as an array of 32-bit integers, in RSI; and the number of nodes in EDX.
In assembly:
f: push rdi; pop rcx # Copy the depth-vector address into RCX.
or DWORD PTR [rdi], -1 # Set the first value to -1 (will become 0).
r: lodsd # Load a parent-vector value into EAX, advancing RSI.
mov eax, [rcx + 4*rax] # Load that index's depth into EAX.
inc eax # Add 1 to the depth in EAX.
stosd # Store EAX into the depth vector, advancing RDI.
dec edx # Subtract 1 from EDX, counting down from the length.
jnz r # Jump back to repeat if it's nonzero.
ret # Return.
Haskell, 26 bytes
f p=0:[1+f p!!i|i<-tail p]
The depth of each node (except the root) will be 1 plus the depth of its parent. Since the parent always comes before its children in the input I can use self reference to get the parent's depth with f p!!i. Then I just need to ensure my base case (the root) is handled specially by always prepending 0 and only iterating over the tail.
JavaScript (Node.js), 30 bytes
x=>x.map((i,j)=>x[~j]=x[-i]+1)
Python, 46 bytes
lambda x,r=[0]:[(r:=r+[r[i]+1])[-1]for i in x]
root node is omitted in both input and output of both answers