Given guaranteed strictly positive integers \$w\$ and \$n\$, output
- An equilateral triangle array with side length \$w\$, filled with two distinct, consistent values. I'll call these
0and1but they do not have to be equal to0and1. - The number of
1s inside this array must be equal to \$n\$. - The output triangle must be symmetrical, meaning that it is the same when flipped horizontally or diagonally (your choice).
- An equilateral triangle array can be an array with \$w\$ elements where for \1ドル\$ to \$w\$ (inclusive), there is a sub-array with that number of elements inside of \$w\$ (for example, but it may be outputted via ascii-art, see below).
(\$n\$ values are guaranteed to fit in the triangle)
Examples
w=3, n=1
1
0 0
0 0 0
w=3, n=2
0
1 1
0 0 0
w=3, n=3
1
1 1
0 0 0
w=3, n=4
0
1 1
1 0 1
w=3, n=5
0
1 1
1 1 1
w=3, n=6
1
1 1
1 1 1
Valid Output Format List
In this case the distinct values are 1 and 0.
Possible output triangles (all considered equivalent) with 1s at their corners and center and a width of 4 are:
1
0 0
0 1 0
1 0 0 1
1
00
010
1001
[[1],[0,0],[0,1,0],[1,0,0,1]]
1 0 0 1
0 1 0
0 0
1
[[1],[0,0],[0,1,0],[1,0,0,1]]
1
0
00
11
00
0
1
1
0
0 0
1 1
0 0
0
1
here is a test case validator in Jq, takes {"w": w, "n": n, "a": result you wish to check} (in the JSON array format like [[1],[0,0],[0,1,0],[1,0,0,1]])
-
\$\begingroup\$ Sandboxed \$\endgroup\$Wezl– Wezl2021年05月21日 15:33:26 +00:00Commented May 21, 2021 at 15:33
-
\$\begingroup\$ For w=3, n=1; Why output is 1;0,0;0,0,0 instead of 0;0,0;0,1,0? \$\endgroup\$tsh– tsh2021年05月22日 04:16:49 +00:00Commented May 22, 2021 at 4:16
-
\$\begingroup\$ @tsh Both are valid, I guess the former was just easier to type :P \$\endgroup\$Wezl– Wezl2021年05月22日 21:12:34 +00:00Commented May 22, 2021 at 21:12
11 Answers 11
Jelly, 15 bytes
RØ.ṗŒpFS=ɗƇUƑƇḢ
-1 byte thanks to caird coinheringaahing
RØ.ṗŒpFS=ɗƇUƑƇḢ Main Link; accepts w as the left argument and n as the right argument
R Range; [1, 2, ..., w]
Ø.ṗ [0, 1] ~ cartesian power ~ ^
Œp Cartesian Product of ^'s items - this gets all valid triangles of the right size
---ɗƇ Keep elements where
S the sum of
F the triangle flattened
= equals (right argument)
Ƈ Keep elements where
Ƒ the element is the same when
U each sub-list is reversed
Ḣ Get the first such triangle
-
-
1\$\begingroup\$ @cairdcoinheringaahing oh yeah it's not a 0,2 chain so I don't need to specify right argument. i'm dumb lol \$\endgroup\$2021年05月21日 15:52:28 +00:00Commented May 21, 2021 at 15:52
JavaScript (ES6), 54 bytes
A much shorter approach. I think this is equivalent to @ovs' answer.
Expects (n)(w). Returns a list of lists, from bottom to top.
n=>g=w=>w?[Array(w).fill(n<w?0:(n-=w,1)),...g(w-1)]:[]
JavaScript (ES6), (削除) 88 (削除ここまで) 85 bytes
This one inserts \1ドル\$'s as soon as it can.
Expects (n)(w). Returns a list of lists, from bottom to top.
n=>g=w=>w?[(h=q=>k--?k--?[q=n>1?--n/n--:0,...h(),q]:[n&&n/n--]:[])(k=w),...g(w-1)]:[]
Japt, 20 bytes
ò1 Ô®>V?Zî0:(VμZ,Zî1
we start from the biggest row, if
nis not smaller than its length we fill the row with 1swe can always put the rest of 1s on the next lines.
filling entire rows gives a specular triangle.
- input: U=w , V=n ò1 Ô - range[1..w] reversed ® - map to Z=>>V? - if Z>n Zî0 * string of Z 0s :( - else VμZ, * n-=Z and.. Zî1 * string of Z 1s
Python 3.8, 47 bytes
Returns a nested list of booleans.
f=lambda w,n:w*[1]and[[x:=n>=w]*w]+f(w-1,n-w*x)
-
\$\begingroup\$ I have limited python experience... and would have assumed that only the right hand side of the
andis ever returned. How doesw*[1]come into play? \$\endgroup\$Jonah– Jonah2021年05月22日 06:20:45 +00:00Commented May 22, 2021 at 6:20 -
1\$\begingroup\$ @Jonah As long as
wis positivew*[1]is some non-empty list, which is always truthy, and the right hand side is returned. But as soon aswreaches zero this evaluates to the empty list (falsy) and serves as the base case of the recursion. (I could've used any other value instead of the1) \$\endgroup\$ovs– ovs2021年05月22日 08:00:21 +00:00Commented May 22, 2021 at 8:00 -
\$\begingroup\$ Thanks. Empty list = falsy is the piece I was missing... coming from ruby. \$\endgroup\$Jonah– Jonah2021年05月22日 08:07:00 +00:00Commented May 22, 2021 at 8:07
05AB1E, (削除) 20 (削除ここまで) 12 bytes
Based on the observation that all outputs can be created by having each row as all 1's or all 0's. Check out AZTECCO's answer, which got there an hour earlier.
×ばつ
1Ý # push the range [0..1] == [0, 1]
Iã # raise this to the first input (w) cartesian power
# this yields all w-tuples of 1's and 0's
.Δ } # find the first that satisfies:
ƶ # each value multiplied by its 1-based index
O # take the sum of that
Q # is equal to the second input (n)
ā # push the range [1..len(value)] == [1..w]
×ばつ # repeat each digit in the select tuple that number of times
Alternative 12 byters (Both find a subset of [1..w] that sums to n and build the triangle from that):
×ばつ
LDæIÅœÃнs×ばつ
-
1\$\begingroup\$ Ehm.. Do you refer to my observation? \$\endgroup\$AZTECCO– AZTECCO2021年05月22日 00:16:45 +00:00Commented May 22, 2021 at 0:16
-
\$\begingroup\$ @AZTECCO I didn't read through your answer before, but it seems like you got there first (+1) \$\endgroup\$ovs– ovs2021年05月22日 07:56:19 +00:00Commented May 22, 2021 at 7:56
-
\$\begingroup\$ that's fine then. Thanks for giving me credits anyway! \$\endgroup\$AZTECCO– AZTECCO2021年05月22日 15:29:52 +00:00Commented May 22, 2021 at 15:29
Charcoal, (削除) 48 (削除ここまで) (削除) 40 (削除ここまで) 28 bytes
NηF⮌...·1N«G↘↖ιI¬‹ηι↓¿¬‹ηι≧−ιη
Try it online! Link is to verbose version of code. Takes arguments in the order n, w. Explanation: Port of @AZTECCO's answer.
Nη
Input n.
F⮌...·1N«
Count from w down to 1.
G↘↖ιI¬‹ηι↓
Output a line of 1s or 0s depending on whether n is big enough.
¿¬‹ηι≧−ιη
Reduce n accordingly.
Previous 40 byte answer:
NθFN⊞υ1Eθ⭆E⌊⟦⊕ι−θι⟧‹+ιλ⊖θ∧›Noυ1λ⊟E⊕λ⊟υ‖O↘
Try it online! Link is to verbose version of code. Explanation:
Nθ
Input w.
FN⊞υ1
Input n and make a list of that many 1s.
Eθ
Start drawing a triangle of height w.
⭆E⌊⟦⊕ι−θι⟧
Draw the upper left half of the triangle.
‹+ιλ⊖θ
Calculate whether this square lies on the diagonal or not.
∧›Noυ1λ⊟E⊕λ⊟υ
If n is large enough to need both this square and its reflection, then reduce n accordingly and output a 1, otherwise output a 0. This always works because the very last square is on the diagonal.
‖O↘
Reflect to complete the triangle.
Jelly, 13 bytes
ŒPS=\ƇḢċ9xɗⱮ8
A dyadic link taking w as its left argument and n as it’s right. Returns a list of lists of 0s and 1s. Based on the observation by @AZTECCO and @ovs that we can compose this solely of rows of 0s and 1s so be sure to upvote their answers too!
Vyxal r, (削除) 18 (削除ここまで) 16 bytes
1⁄8ɾṘƛ1⁄4:n≤:Sn*,[-1⁄8
1⁄8 # (Implicit input) Push to global array
ɾṘ # (Implicit input) range(1,n+1) reversed
ƛ # Foreach [n]
1⁄4: # Push from global array and duplicate [n,r,r]
n≤ # Push n and check if r >= n [n,r,b]
:Sn*, # Duplicate, coerce to string (0 or 1), multi by n and output.
[ # If ToS is truthy (r >= n) [n,r]
- # Subtract [r-n] (r flag reverses arguments)
1⁄8 # Push to global array.
Due to a bug with redefining the register inside a lambda, I'm forced to use the global array. It works well though, and I don't think it'd be any more golfable if the register worked properly.
Based on the insight that everything can be made of rows of 1s and 0s.
Explore related questions
See similar questions with these tags.