6
\$\begingroup\$

An n-simplex is a generalization of 'triangleness' in any dimension (specifically, it is the simplest shape requiring n dimensions). Starting with 0 dimensions, the named simplexes are: point, line segment, triangle, tetrahedron, and pentachoron.

Simplexes have many useful properties, but one is that you can make an n-simplex shaped grid with sides of length l out of \$ \left( \begin{smallmatrix} n + l - 1 \\ n \end{smallmatrix} \right) \$ points. For this challenge, you will weave a string through these points starting at the top, and then output the structure of the full simplex from the top to the bottom. Before you get scared away by high dimensional geometry, this challenge only requires you to split a string into a nested list, reversing some of them.

Inputs

You will be given some integer n (the number of dimensions the simplex should have), and a string of length \$ \left( \begin{smallmatrix} n + l - 1 \\ n \end{smallmatrix} \right) \$ for some integer l (the length that each edge of the simplex will end up having). If you want to use it, you can also take the integer l as input. You may take these inputs in any order/format.

Output

Your output should be a nested list. This list is a representation of the simplex that you are outputting. It starts at the top point and has one entry for each layer of the shape. If there are multiple elements on one layer, the entry for that layer is a nested list with one entry for each layer. This recurses through the shape until all indices are hit.

The string should snake continuously around the simplex, only traveling to adjacent points. In practice this means at each level you alternate between going forwards and backwards. If you start at the top of a layer, and hit each point of that layer, you will end up at the bottom (especially obvious for the base case of the line segment, but true for all n-simplexes). When you end a layer at the bottom, you start the next layer at the adjacent point at the bottom, meaning you end that layer at the top and repeat.

Examples

It's much easier to visualize using examples, so I'll explain the format and let you look at it yourselves. The first line of each section is a sample input with a side length of four, followed by a graphical representation of the corresponding shape with the string snaking around the simplex. Each dimension past 2 gets split up, with unique separators for each dimension. After the picture, there are four sample input/ouput lines formatted as n,l,string -> output. The 6,4 case adds ASCII symbols in order from 33 to 64. The 8,6 case uses the first 1,287 Unicode CJK Unified Ideographs, see the graphical representation here as text or here as an image. I did manually type all these out so let me know if you think there's an error.

0,a
a

0,1,a -> a
0,2,a -> a
0,3,a -> a
0,4,a -> a

1,abcd
 a b c d 

1,1,a    -> [a]
1,2,ab   -> [a,b]
1,3,abc  -> [a,b,c]
1,4,abcd -> [a,b,c,d]

2,abcdefghij
    a    
   b c   
  f e d  
 g h i j 

2,1,a          -> [[a]]
2,2,abc        -> [[a],[b,c]]
2,3,abcdef     -> [[a],[b,c],[f,e,d]]
2,4,abcdefghij -> [[a],[b,c],[f,e,d],[g,h,i,j]]

3,abcdefghijklmnopqrst
    a    
 ------- 
    b    
   c d   
 ------- 
    j    
   h i   
  g f e  
 ------- 
    k    
   l m   
  p o n  
 q r s t 

3,1,a                    -> [[[a]]]
3,2,abcd                 -> [[[a]],[[b],[c,d]]]
3,3,abcdefghij           -> [[[a]],[[b],[c,d]],[[j],[h,i],[g,f,e]]]
3,4,abcdefghijklmnopqrst -> [[[a]],[[b],[c,d]],[[j],[h,i],[g,f,e]],[[k],[l,m],[p,o,n],[q,r,s,t]]]

4,abcdefghijklmnopqrstuvwxyz123456789
    a    
 ======= 
    b    
 ------- 
    c    
   d e   
 ======= 
    o    
 ------- 
    l    
   m n   
 ------- 
    k    
   i j   
  h g f  
 ======= 
    p    
 ------- 
    q    
   r s   
 ------- 
    y    
   w x   
  v u t  
 ------- 
    z    
   1 2   
  5 4 3  
 6 7 8 9 

4,1,a                                   -> [[[[a]]]]
4,2,abcde                               -> [[[[a]]],[[[b]],[[c],[d,e]]]]
4,3,abcdefghijklmno                     -> [[[[a]]],[[[b]],[[c],[d,e]]],[[[o]],[[l],[m,n]],[[k],[i,j],[h,g,f]]]]
4,4,abcdefghijklmnopqrstuvwxyz123456789 -> [[[[a]]],[[[b]],[[c],[d,e]]],[[[o]],[[l],[m,n]],[[k],[i,j],[h,g,f]]],[[[p]],[[q],[r,s]],[[y],[w,x],[v,u,t]],[[z],[1,2],[5,4,3],[6,7,8,9]]]]

5,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234
 a |  b  |   u   |    v    
   | === | ===== | ======= 
   |  c  |   q   |    w    
   | --- | ----- | ------- 
   |  d  |   r   |    x    
   | e f |  s t  |   y z   
         | ===== | ======= 
         |   p   |    J    
         | ----- | ------- 
         |   m   |    G    
         |  n o  |   H I   
         | ----- | ------- 
         |   l   |    F    
         |  j k  |   D E   
         | i h g |  C B A  
                 | ======= 
                 |    K    
                 | ------- 
                 |    L    
                 |   M N   
                 | ------- 
                 |    T    
                 |   R S   
                 |  Q P O  
                 | ------- 
                 |    U    
                 |   V W   
                 |  Z Y X  
                 | 1 2 3 4 

5,1,a                                                        -> [[[[[a]]]]]
5,2,abcdef                                                   -> [[[[[a]]]],[[[[b]]],[[[c]],[[d],[e,f]]]]]
5,3,abcdefghijklmnopqrstu                                    -> [[[[[a]]]],[[[[b]]],[[[c]],[[d],[e,f]]]],[[[[u]]],[[[q]],[[r],[s,t]]],[[[p]],[[m],[n,o]],[[l],[j,k],[i,h,g]]]]]
5,4,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234 -> [[[[[a]]]],[[[[b]]],[[[c]],[[d],[e,f]]]],[[[[u]]],[[[q]],[[r],[s,t]]],[[[p]],[[m],[n,o]],[[l],[j,k],[i,h,g]]]],[[[[v]]],[[[w]],[[x],[y,z]]],[[[J]],[[G],[H,I]],[[F],[D,E],[C,B,A]]],[[[K]],[[L],[M,N]],[[T],[R,S],[Q,P,O]],[[U],[V,W],[Z,Y,X],[1,2,3,4]]]]]

6,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./0123456789:;<=>?@
 a ‖ b |  c  ‖ B |  w  |   v   ‖ C |  D  |   W   |    X    
   ‖   | === ‖   | === | ===== ‖   | === | ===== | ======= 
   ‖   |  d  ‖   |  x  |   r   ‖   |  E  |   S   |    Y    
   ‖   | --- ‖   | --- | ----- ‖   | --- | ----- | ------- 
   ‖   |  e  ‖   |  y  |   s   ‖   |  F  |   T   |    Z    
   ‖   | f g ‖   | z A |  t u  ‖   | G H |  U V  |   ! "   
             ‖         | ===== ‖         | ===== | ======= 
             ‖         |   q   ‖         |   R   |    ,    
             ‖         | ----- ‖         | ----- | ------- 
             ‖         |   n   ‖         |   O   |    )    
             ‖         |  o p  ‖         |  P Q  |   * +   
             ‖         | ----- ‖         | ----- | ------- 
             ‖         |   m   ‖         |   N   |    (    
             ‖         |  k l  ‖         |  L M  |   & '   
             ‖         | j i h ‖         | K J I |  % $ #  
                               ‖                 | ======= 
                               ‖                 |    -    
                               ‖                 | ------- 
                               ‖                 |    .    
                               ‖                 |   / 0   
                               ‖                 | ------- 
                               ‖                 |    6    
                               ‖                 |   4 5   
                               ‖                 |  3 2 1  
                               ‖                 | ------- 
                               ‖                 |    7    
                               ‖                 |   8 9   
                               ‖                 |  < ; :  
                               ‖                 | = > ? @ 

6,1,a                                                                                    -> [[[[[[a]]]]]]
6,2,abcdefg                                                                              -> [[[[[[a]]]]],[[[[[b]]]],[[[[c]]],[[[d]],[[e],[f,g]]]]]]
6,3,abcdefghijklmnopqrstuvwxyzAB                                                         -> [[[[[[a]]]]],[[[[[b]]]],[[[[c]]],[[[d]],[[e],[f,g]]]]],[[[[[B]]]],[[[[w]]],[[[x]],[[y],[z,A]]]],[[[[v]]],[[[r]],[[s],[t,u]]],[[[q]],[[n],[o,p]],[[m],[k,l],[j,i,h]]]]]]
6,4,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./0123456789:;<=>?@ -> [[[[[[a]]]]],[[[[[b]]]],[[[[c]]],[[[d]],[[e],[f,g]]]]],[[[[[B]]]],[[[[w]]],[[[x]],[[y],[z,A]]]],[[[[v]]],[[[r]],[[s],[t,u]]],[[[q]],[[n],[o,p]],[[m],[k,l],[j,i,h]]]]],[[[[[C]]]],[[[[D]]],[[[E]],[[F],[G,H]]]],[[[[W]]],[[[S]],[[T],[U,V]]],[[[R]],[[O],[P,Q]],[[N],[L,M],[K,J,I]]]],[[[[X]]],[[[Y]],[[Z],[!,"]]],[[[,]],[[)],[*,+]],[[(],[&,'],[%,$,#]]],[[[-]],[[.],[/,0]],[[6],[4,5],[3,2,1]],[[7],[8,9],[<,;,:],[=,>,?,@]]]]]]

Copy/paste-able Examples

Inputs:

0 1 a
0 2 a
0 3 a
0 4 a
1 1 a
1 2 ab
1 3 abc
1 4 abcd
2 1 a
2 2 abc
2 3 abcdef
2 4 abcdefghij
3 1 a
3 2 abcd
3 3 abcdefghij
3 4 abcdefghijklmnopqrst
4 1 a
4 2 abcde
4 3 abcdefghijklmno
4 4 abcdefghijklmnopqrstuvwxyz123456789
5 1 a
5 2 abcdef
5 3 abcdefghijklmnopqrstu
5 4 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234
6 1 a
6 2 abcdefg
6 3 abcdefghijklmnopqrstuvwxyzAB
6 4 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./0123456789:;<=>?@
6 4 w;mp<bF$ZX1kH%M4IrLl&QqnWx7(9E>JKs)YeCP:8S*RGgUO=0v6!+-Tdu'3a5",#./?thfji2cANyB@VozD
8 6 丁丂七丄丅丆万丈三上下丌不与丏丐丑丒专且丕世丗丘丙业丛东丝丞丟丠両丢丣两严並丧丨丩个丫丬中丮丯丰丱串丳临丵丶丷丸丹为主丼丽举丿乀乁乂乃乄久乆乇么义乊之乌乍乎乏乐乑乒乓乔乕乖乗乘乙乚乛乜九乞也习乡乢乣乤乥书乧乨乩乪乫乬乭乮乯买乱乲乳乴乵乶乷乸乹乺乻乼乽乾乿亀亁亂亃亄亅了亇予争亊事二亍于亏亐云互亓五井亖亗亘亙亚些亜亝亞亟亠亡亢亣交亥亦产亨亩亪享京亭亮亯亰亱亲亳亴亵亶亷亸亹人亻亼亽亾亿什仁仂仃仄仅仆仇仈仉今介仌仍从仏仐仑仒仓仔仕他仗付仙仚仛仜仝仞仟仠仡仢代令以仦仧仨仩仪仫们仭仮仯仰仱仲仳仴仵件价仸仹仺任仼份仾仿伀企伂伃伄伅伆伇伈伉伊伋伌伍伎伏伐休伒伓伔伕伖众优伙会伛伜伝伞伟传伡伢伣伤伥伦伧伨伩伪伫伬伭伮伯估伱伲伳伴伵伶伷伸伹伺伻似伽伾伿佀佁佂佃佄佅但佇佈佉佊佋佌位低住佐佑佒体佔何佖佗佘余佚佛作佝佞佟你佡佢佣佤佥佦佧佨佩佪佫佬佭佮佯佰佱佲佳佴併佶佷佸佹佺佻佼佽佾使侀侁侂侃侄侅來侇侈侉侊例侌侍侎侏侐侑侒侓侔侕侖侗侘侙侚供侜依侞侟侠価侢侣侤侥侦侧侨侩侪侫侬侭侮侯侰侱侲侳侴侵侶侷侸侹侺侻侼侽侾便俀俁係促俄俅俆俇俈俉俊俋俌俍俎俏俐俑俒俓俔俕俖俗俘俙俚俛俜保俞俟俠信俢俣俤俥俦俧俨俩俪俫俬俭修俯俰俱俲俳俴俵俶俷俸俹俺俻俼俽俾俿倀倁倂倃倄倅倆倇倈倉倊個倌倍倎倏倐們倒倓倔倕倖倗倘候倚倛倜倝倞借倠倡倢倣値倥倦倧倨倩倪倫倬倭倮倯倰倱倲倳倴倵倶倷倸倹债倻值倽倾倿偀偁偂偃偄偅偆假偈偉偊偋偌偍偎偏偐偑偒偓偔偕偖偗偘偙做偛停偝偞偟偠偡偢偣偤健偦偧偨偩偪偫偬偭偮偯偰偱偲偳側偵偶偷偸偹偺偻偼偽偾偿傀傁傂傃傄傅傆傇傈傉傊傋傌傍傎傏傐傑傒傓傔傕傖傗傘備傚傛傜傝傞傟傠傡傢傣傤傥傦傧储傩傪傫催傭傮傯傰傱傲傳傴債傶傷傸傹傺傻傼傽傾傿僀僁僂僃僄僅僆僇僈僉僊僋僌働僎像僐僑僒僓僔僕僖僗僘僙僚僛僜僝僞僟僠僡僢僣僤僥僦僧僨僩僪僫僬僭僮僯僰僱僲僳僴僵僶僷僸價僺僻僼僽僾僿儀儁儂儃億儅儆儇儈儉儊儋儌儍儎儏儐儑儒儓儔儕儖儗儘儙儚儛儜儝儞償儠儡儢儣儤儥儦儧儨儩優儫儬儭儮儯儰儱儲儳儴儵儶儷儸儹儺儻儼儽儾儿兀允兂元兄充兆兇先光兊克兌免兎兏児兑兒兓兔兕兖兗兘兙党兛兜兝兞兟兠兡兢兣兤入兦內全兩兪八公六兮兯兰共兲关兴兵其具典兹兺养兼兽兾兿冀冁冂冃冄内円冇冈冉冊冋册再冎冏冐冑冒冓冔冕冖冗冘写冚军农冝冞冟冠冡冢冣冤冥冦冧冨冩冪冫冬冭冮冯冰冱冲决冴况冶冷冸冹冺冻冼冽冾冿净凁凂凃凄凅准凇凈凉凊凋凌凍凎减凐凑凒凓凔凕凖凗凘凙凚凛凜凝凞凟几凡凢凣凤凥処凧凨凩凪凫凬凭凮凯凰凱凲凳凴凵凶凷凸凹出击凼函凾凿刀刁刂刃刄刅分切刈刉刊刋刌刍刎刏刐刑划刓刔刕刖列刘则刚创刜初刞刟删刡刢刣判別刦刧刨利刪别刬刭刮刯到刱刲刳刴刵制刷券刹刺刻刼刽刾刿剀剁剂剃剄剅剆則剈剉削剋剌前剎剏剐剑剒剓剔剕剖剗剘剙剚剛剜剝剞剟剠剡剢剣剤剥剦剧剨剩剪剫剬剭剮副剰剱割剳剴創剶剷剸剹剺剻剼剽剾剿劀劁劂劃劄劅劆劇劈劉劊劋劌劍劎劏劐劑劒劓劔劕劖劗劘劙劚力劜劝办功加务劢劣劤劥劦劧动助努劫劬劭劮劯劰励劲劳労劵劶劷劸効劺劻劼劽劾势勀勁勂勃勄勅勆勇勈勉勊勋勌勍勎勏勐勑勒勓勔動勖勗勘務勚勛勜勝勞募勠勡勢勣勤勥勦勧勨勩勪勫勬勭勮勯勰勱勲勳勴勵勶勷勸勹勺勻勼勽勾勿匀匁匂匃匄包匆匇

Outputs:

a
a
a
a
[a]
[a,b]
[a,b,c]
[a,b,c,d]
[[a]]
[[a],[b,c]]
[[a],[b,c],[f,e,d]]
[[a],[b,c],[f,e,d],[g,h,i,j]]
[[[a]]]
[[[a]],[[b],[c,d]]]
[[[a]],[[b],[c,d]],[[j],[h,i],[g,f,e]]]
[[[a]],[[b],[c,d]],[[j],[h,i],[g,f,e]],[[k],[l,m],[p,o,n],[q,r,s,t]]]
[[[[a]]]]
[[[[a]]],[[[b]],[[c],[d,e]]]]
[[[[a]]],[[[b]],[[c],[d,e]]],[[[o]],[[l],[m,n]],[[k],[i,j],[h,g,f]]]]
[[[[a]]],[[[b]],[[c],[d,e]]],[[[o]],[[l],[m,n]],[[k],[i,j],[h,g,f]]],[[[p]],[[q],[r,s]],[[y],[w,x],[v,u,t]],[[z],[1,2],[5,4,3],[6,7,8,9]]]]
[[[[[a]]]]]
[[[[[a]]]],[[[[b]]],[[[c]],[[d],[e,f]]]]]
[[[[[a]]]],[[[[b]]],[[[c]],[[d],[e,f]]]],[[[[u]]],[[[q]],[[r],[s,t]]],[[[p]],[[m],[n,o]],[[l],[j,k],[i,h,g]]]]]
[[[[[a]]]],[[[[b]]],[[[c]],[[d],[e,f]]]],[[[[u]]],[[[q]],[[r],[s,t]]],[[[p]],[[m],[n,o]],[[l],[j,k],[i,h,g]]]],[[[[v]]],[[[w]],[[x],[y,z]]],[[[J]],[[G],[H,I]],[[F],[D,E],[C,B,A]]],[[[K]],[[L],[M,N]],[[T],[R,S],[Q,P,O]],[[U],[V,W],[Z,Y,X],[1,2,3,4]]]]]
[[[[[[a]]]]]]
[[[[[[a]]]]],[[[[[b]]]],[[[[c]]],[[[d]],[[e],[f,g]]]]]]
[[[[[[a]]]]],[[[[[b]]]],[[[[c]]],[[[d]],[[e],[f,g]]]]],[[[[[B]]]],[[[[w]]],[[[x]],[[y],[z,A]]]],[[[[v]]],[[[r]],[[s],[t,u]]],[[[q]],[[n],[o,p]],[[m],[k,l],[j,i,h]]]]]]
[[[[[[a]]]]],[[[[[b]]]],[[[[c]]],[[[d]],[[e],[f,g]]]]],[[[[[B]]]],[[[[w]]],[[[x]],[[y],[z,A]]]],[[[[v]]],[[[r]],[[s],[t,u]]],[[[q]],[[n],[o,p]],[[m],[k,l],[j,i,h]]]]],[[[[[C]]]],[[[[D]]],[[[E]],[[F],[G,H]]]],[[[[W]]],[[[S]],[[T],[U,V]]],[[[R]],[[O],[P,Q]],[[N],[L,M],[K,J,I]]]],[[[[X]]],[[[Y]],[[Z],[!,"]]],[[[,]],[[)],[*,+]],[[(],[&,'],[%,$,#]]],[[[-]],[[.],[/,0]],[[6],[4,5],[3,2,1]],[[7],[8,9],[<,;,:],[=,>,?,@]]]]]]
[[[[[[w]]]]],[[[[[;]]]],[[[[m]]],[[[p]],[[<],[b,F]]]]],[[[[[(]]]],[[[[q]]],[[[n]],[[W],[x,7]]]],[[[[Q]]],[[[r]],[[L],[l,&]]],[[[I]],[[%],[M,4]],[[H],[1,k],[X,Z,$]]]]],[[[[[9]]]],[[[[E]]],[[[>]],[[J],[K,s]]]],[[[[=]]],[[[G]],[[g],[U,O]]],[[[R]],[[8],[S,*]],[[:],[C,P],[e,Y,)]]]],[[[[0]]],[[[v]],[[6],[!,+]]],[[[,]],[[a],[5,"]],[[3],[u,'],[d,T,-]]],[[[#]],[[.],[/,?]],[[2],[j,i],[f,h,t]],[[c],[A,N],[@,B,y],[V,o,z,D]]]]]]
[[[[[[[[丁]]]]]]],[[[[[[[丂]]]]]],[[[[[[七]]]]],[[[[[丄]]]],[[[[丅]]],[[[丆]],[[万],[丈,三]]]]]]],[[[[[[[中]]]]]],[[[[[[並]]]]],[[[[[丧]]]],[[[[丨]]],[[[丩]],[[个],[丫,丬]]]]]],[[[[[[严]]]]],[[[[[丟]]]],[[[[丠]]],[[[両]],[[丢],[丣,两]]]]],[[[[[丞]]]],[[[[丙]]],[[[业]],[[丛],[东,丝]]]],[[[[丘]]],[[[且]],[[丕],[世,丗]]],[[[专]],[[丐],[丑,丒]],[[丏],[不,与],[丌,下,上]]]]]]],[[[[[[[丮]]]]]],[[[[[[丯]]]]],[[[[[丰]]]],[[[[丱]]],[[[串]],[[丳],[临,丵]]]]]],[[[[[[乑]]]]],[[[[[之]]]],[[[[乌]]],[[[乍]],[[乎],[乏,乐]]]]],[[[[[乊]]]],[[[[久]]],[[[乆]],[[乇],[么,义]]]],[[[[乄]]],[[[乀]],[[乁],[乂,乃]]],[[[丿]],[[丼],[丽,举]],[[主],[丹,为],[丸,丷,丶]]]]]],[[[[[[乒]]]]],[[[[[乓]]]],[[[[乔]]],[[[乕]],[[乖],[乗,乘]]]]],[[[[[乭]]]],[[[[乨]]],[[[乩]],[[乪],[乫,乬]]]],[[[[乧]]],[[[乣]],[[乤],[乥,书]]],[[[乢]],[[也],[习,乡]],[[乞],[乜,九],[乛,乚,乙]]]]],[[[[[乮]]]],[[[[乯]]],[[[买]],[[乱],[乲,乳]]]],[[[[亂]]],[[[乾]],[[乿],[亀,亁]]],[[[乽]],[[乺],[乻,乼]],[[乹],[乷,乸],[乶,乵,乴]]]],[[[[亃]]],[[[亄]],[[亅],[了,亇]]],[[[云]],[[于],[亏,亐]],[[亍],[事,二],[亊,争,予]]],[[[互]],[[亓],[五,井]],[[些],[亙,亚],[亘,亗,亖]],[[亜],[亝,亞],[亡,亠,亟],[亢,亣,交,亥]]]]]]],[[[[[[[俯]]]]]],[[[[[[俨]]]]],[[[[[俩]]]],[[[[俪]]],[[[俫]],[[俬],[俭,修]]]]]],[[[[[[俧]]]]],[[[[[信]]]],[[[[俢]]],[[[俣]],[[俤],[俥,俦]]]]],[[[[[俠]]]],[[[[俛]]],[[[俜]],[[保],[俞,俟]]]],[[[[俚]]],[[[俖]],[[俗],[俘,俙]]],[[[俕]],[[俒],[俓,俔]],[[俑],[俏,俐],[俎,俍,俌]]]]]],[[[[[[佸]]]]],[[[[[佹]]]],[[[[佺]]],[[[佻]],[[佼],[佽,佾]]]]],[[[[[侓]]]],[[[[侎]]],[[[侏]],[[侐],[侑,侒]]]],[[[[侍]]],[[[侉]],[[侊],[例,侌]]],[[[侈]],[[侅],[來,侇]],[[侄],[侂,侃],[侁,侀,使]]]]],[[[[[侔]]]],[[[[侕]]],[[[侖]],[[侗],[侘,侙]]]],[[[[侨]]],[[[侤]],[[侥],[侦,侧]]],[[[侣]],[[侠],[価,侢]],[[侟],[依,侞],[侜,供,侚]]]],[[[[侩]]],[[[侪]],[[侫],[侬,侭]]],[[[侷]],[[侴],[侵,侶]],[[侳],[侱,侲],[侰,侯,侮]]],[[[侸]],[[侹],[侺,侻]],[[俁],[便,俀],[侾,侽,侼]],[[係],[促,俄],[俇,俆,俅],[俈,俉,俊,俋]]]]]],[[[[[[佷]]]]],[[[[[佱]]]],[[[[佲]]],[[[佳]],[[佴],[併,佶]]]]],[[[[[佰]]]],[[[[佫]]],[[[佬]],[[佭],[佮,佯]]]],[[[[佪]]],[[[佦]],[[佧],[佨,佩]]],[[[佥]],[[佢],[佣,佤]],[[佡],[佟,你],[佞,佝,作]]]]],[[[[[伤]]]],[[[[伥]]],[[[伦]],[[伧],[伨,伩]]]],[[[[伸]]],[[[伴]],[[伵],[伶,伷]]],[[[伳]],[[估],[伱,伲]],[[伯],[伭,伮],[伬,伫,伪]]]],[[[[伹]]],[[[伺]],[[伻],[似,伽]]],[[[佇]],[[佄],[佅,但]],[[佃],[佁,佂],[佀,伿,伾]]],[[[佈]],[[佉],[佊,佋]],[[佑],[住,佐],[低,位,佌]],[[佒],[体,佔],[佗,佖,何],[佘,余,佚,佛]]]]],[[[[[伣]]]],[[[[伞]]],[[[伟]],[[传],[伡,伢]]]],[[[[伝]]],[[[伙]],[[会],[伛,伜]]],[[[优]],[[伕],[伖,众]],[[伔],[伒,伓],[休,伐,伏]]]],[[[[们]]],[[[仭]],[[仮],[仯,仰]]],[[[仺]],[[价],[仸,仹]],[[件],[仴,仵],[仳,仲,仱]]],[[[任]],[[仼],[份,仾]],[[伄],[伂,伃],[企,伀,仿]],[[伅],[伆,伇],[伊,伉,伈],[伋,伌,伍,伎]]]],[[[[仫]]],[[[仧]],[[仨],[仩,仪]]],[[[仦]],[[代],[令,以]],[[仢],[仠,仡],[仟,仞,仝]]],[[[仉]],[[今],[介,仌]],[[仒],[仐,仑],[仏,从,仍]],[[仓],[仔,仕],[付,仗,他],[仙,仚,仛,仜]]],[[[仈]],[[仅],[仆,仇]],[[仄],[仂,仃],[仁,什,亿]],[[亵],[亶,亷],[人,亹,亸],[亻,亼,亽,亾]],[[亴],[亲,亳],[亱,亰,亯],[享,京,亭,亮],[亪,亩,亨,产,亦]]]]]]],[[[[[[[俰]]]]]],[[[[[[俱]]]]],[[[[[俲]]]],[[[[俳]]],[[[俴]],[[俵],[俶,俷]]]]]],[[[[[[倓]]]]],[[[[[倍]]]],[[[[倎]]],[[[倏]],[[倐],[們,倒]]]]],[[[[[倌]]]],[[[[倇]]],[[[倈]],[[倉],[倊,個]]]],[[[[倆]]],[[[倂]],[[倃],[倄,倅]]],[[[倁]],[[俾],[俿,倀]],[[俽],[俻,俼],[俺,俹,俸]]]]]],[[[[[[倔]]]]],[[[[[倕]]]],[[[[倖]]],[[[倗]],[[倘],[候,倚]]]]],[[[[[倯]]]],[[[[倪]]],[[[倫]],[[倬],[倭,倮]]]],[[[[倩]]],[[[倥]],[[倦],[倧,倨]]],[[[値]],[[倡],[倢,倣]],[[倠],[倞,借],[倝,倜,倛]]]]],[[[[[倰]]]],[[[[倱]]],[[[倲]],[[倳],[倴,倵]]]],[[[[偄]]],[[[偀]],[[偁],[偂,偃]]],[[[倿]],[[值],[倽,倾]],[[倻],[倹,债],[倸,倷,倶]]]],[[[[偅]]],[[[偆]],[[假],[偈,偉]]],[[[偓]],[[偐],[偑,偒]],[[偏],[偍,偎],[偌,偋,偊]]],[[[偔]],[[偕],[偖,偗]],[[偝],[偛,停],[做,偙,偘]],[[偞],[偟,偠],[偣,偢,偡],[偤,健,偦,偧]]]]]],[[[[[[儹]]]]],[[[[[儳]]]],[[[[儴]]],[[[儵]],[[儶],[儷,儸]]]]],[[[[[儲]]]],[[[[儭]]],[[[儮]],[[儯],[儰,儱]]]],[[[[儬]]],[[[儨]],[[儩],[優,儫]]],[[[儧]],[[儤],[儥,儦]],[[儣],[儡,儢],[儠,償,儞]]]]],[[[[[僦]]]],[[[[僧]]],[[[僨]],[[僩],[僪,僫]]]],[[[[僺]]],[[[僶]],[[僷],[僸,價]]],[[[僵]],[[僲],[僳,僴]],[[僱],[僯,僰],[僮,僭,僬]]]],[[[[僻]]],[[[僼]],[[僽],[僾,僿]]],[[[儉]],[[儆],[儇,儈]],[[儅],[儃,億],[儂,儁,儀]]],[[[儊]],[[儋],[儌,儍]],[[儓],[儑,儒],[儐,儏,儎]],[[儔],[儕,儖],[儙,儘,儗],[儚,儛,儜,儝]]]]],[[[[[僥]]]],[[[[僠]]],[[[僡]],[[僢],[僣,僤]]]],[[[[僟]]],[[[僛]],[[僜],[僝,僞]]],[[[僚]],[[僗],[僘,僙]],[[僖],[僔,僕],[僓,僒,僑]]]],[[[[傮]]],[[[傯]],[[傰],[傱,傲]]],[[[傼]],[[傹],[傺,傻]],[[傸],[傶,傷],[債,傴,傳]]],[[[傽]],[[傾],[傿,僀]],[[僆],[僄,僅],[僃,僂,僁]],[[僇],[僈,僉],[僌,僋,僊],[働,僎,像,僐]]]],[[[[傭]]],[[[傩]],[[傪],[傫,催]]],[[[储]],[[傥],[傦,傧]],[[傤],[傢,傣],[傡,傠,傟]]],[[[傋]],[[傌],[傍,傎]],[[傔],[傒,傓],[傑,傐,傏]],[[傕],[傖,傗],[傚,備,傘],[傛,傜,傝,傞]]],[[[傊]],[[傇],[傈,傉]],[[傆],[傄,傅],[傃,傂,傁]],[[偷],[偸,偹],[偼,偻,偺],[偽,偾,偿,傀]],[[偶],[側,偵],[偳,偲,偱],[偭,偮,偯,偰],[偬,偫,偪,偩,偨]]]]]],[[[[[[儺]]]]],[[[[[儻]]]],[[[[儼]]],[[[儽]],[[儾],[儿,兀]]]]],[[[[[兕]]]],[[[[児]]],[[[兑]],[[兒],[兓,兔]]]],[[[[兏]]],[[[克]],[[兌],[免,兎]]],[[[兊]],[[兇],[先,光]],[[兆],[兄,充],[元,兂,允]]]]],[[[[[兖]]]],[[[[兗]]],[[[兘]],[[兙],[党,兛]]]],[[[[兪]]],[[[兦]],[[內],[全,兩]]],[[[入]],[[兢],[兣,兤]],[[兡],[兟,兠],[兞,兝,兜]]]],[[[[八]]],[[[公]],[[六],[兮,兯]]],[[[兹]],[[其],[具,典]],[[兵],[关,兴],[兲,共,兰]]],[[[兺]],[[养],[兼,兽]],[[冃],[冁,冂],[冀,兿,兾]],[[冄],[内,円],[冉,冈,冇],[冊,冋,册,再]]]]],[[[[[刋]]]],[[[[分]]],[[[切]],[[刈],[刉,刊]]]],[[[[刅]]],[[[刁]],[[刂],[刃,刄]]],[[[刀]],[[函],[凾,凿]],[[凼],[出,击],[凹,凸,凷]]]],[[[[凔]]],[[[凕]],[[凖],[凗,凘]]],[[[凢]],[[凟],[几,凡]],[[凞],[凜,凝],[凛,凚,凙]]],[[[凣]],[[凤],[凥,処]],[[凬],[凪,凫],[凩,凨,凧]],[[凭],[凮,凯],[凲,凱,凰],[凳,凴,凵,凶]]]],[[[[凓]]],[[[减]],[[凐],[凑,凒]]],[[[凎]],[[凋],[凌,凍]],[[凊],[凈,凉],[凇,准,凅]]],[[[冱]],[[冲],[决,冴]],[[冺],[冸,冹],[冷,冶,况]],[[冻],[冼,冽],[净,冿,冾],[凁,凂,凃,凄]]],[[[冰]],[[冭],[冮,冯]],[[冬],[冪,冫],[冩,冨,冧]],[[冝],[冞,冟],[冢,冡,冠],[冣,冤,冥,冦]],[[农],[冚,军],[写,冘,冗],[冓,冔,冕,冖],[冒,冑,冐,冏,冎]]]]],[[[[[刌]]]],[[[[刍]]],[[[刎]],[[刏],[刐,刑]]]],[[[[删]]],[[[刜]],[[初],[刞,刟]]],[[[创]],[[刘],[则,刚]],[[列],[刕,刖],[刔,刓,划]]]],[[[[刡]]],[[[刢]],[[刣],[判,別]]],[[[刯]],[[刬],[刭,刮]],[[别],[利,刪],[刨,刧,刦]]],[[[到]],[[刱],[刲,刳]],[[刹],[刷,券],[制,刵,刴]],[[刺],[刻,刼],[刿,刾,刽],[剀,剁,剂,剃]]]],[[[[劉]]],[[[劅]],[[劆],[劇,劈]]],[[[劄]],[[劁],[劂,劃]],[[劀],[剾,剿],[剽,剼,剻]]],[[[剧]],[[剨],[剩,剪]],[[剰],[剮,副],[剭,剬,剫]],[[剱],[割,剳],[剶,創,剴],[剷,剸,剹,剺]]],[[[剦]],[[剣],[剤,剥]],[[剢],[剠,剡],[剟,剞,剝]],[[剓],[剔,剕],[剘,剗,剖],[剙,剚,剛,剜]],[[剒],[剐,剑],[剏,剎,前],[剉,削,剋,剌],[剈,則,剆,剅,剄]]]],[[[[劊]]],[[[劋]],[[劌],[劍,劎]]],[[[劘]],[[劕],[劖,劗]],[[劔],[劒,劓],[劑,劐,劏]]],[[[劙]],[[劚],[力,劜]],[[劢],[加,务],[功,办,劝]],[[劣],[劤,劥],[动,劧,劦],[助,努,劫,劬]]],[[[勏]],[[勌],[勍,勎]],[[勋],[勉,勊],[勈,勇,勆]],[[劼],[劽,劾],[勁,勀,势],[勂,勃,勄,勅]],[[劻],[効,劺],[劸,劷,劶],[劲,劳,労,劵],[励,劰,劯,劮,劭]]],[[[勐]],[[勑],[勒,勓]],[[務],[勗,勘],[勖,動,勔]],[[勚],[勛,勜],[募,勞,勝],[勠,勡,勢,勣]],[[勲],[勰,勱],[勯,勮,勭],[勩,勪,勫,勬],[勨,勧,勦,勥,勤]],[[勳],[勴,勵],[勸,勷,勶],[勹,勺,勻,勼],[匁,匀,勿,勾,勽],[匂,匃,匄,包,匆,匇]]]]]]]]

Validity

You should get the same outputs as I have here. You should also not hardcode the cases, so your formulae should work for larger values; see also the forbidden standard loopholes. It is guaranteed that the string will perfectly fill the shape (no need to trim/pad it). This is , so the shortest valid answer in bytes in each language wins.

\$\endgroup\$
5
  • \$\begingroup\$ sandbox, see also \$\endgroup\$
    – guest4308
    Commented Aug 30 at 19:01
  • 1
    \$\begingroup\$ My solution returns the same output for all but the last three test cases, but returns a different output for the final test case. (Haven't bothered to apply the other two test cases, didn't want to wrangle with special characters.) Looking into it, there were 5 places where something differed, one of which I could verify as "doesn't look right" in the manually written test cases. \$\endgroup\$ Commented Aug 31 at 19:07
  • 1
    \$\begingroup\$ @shapewarriort I found all 5 differences and compared with the source string; it looks like all of them were my errors. (ie, your output is correct) \$\endgroup\$
    – guest4308
    Commented Aug 31 at 19:40
  • 1
    \$\begingroup\$ two of them were from a slip-up with going from the diagram to the nested list; I initially forgot to wrap level-2 sections in brackets so I went back through and added them in afterwards, apparently my regex messed up twice. the rest are diagram-level errors: the first (the big one) was caught before the image was made, and it seems the script I made to correct the output string didn't fix it properly. the last two errors, at and , were never caught in the first place, and will be the only spots where the image is wrong. \$\endgroup\$
    – guest4308
    Commented Aug 31 at 19:53
  • 1
    \$\begingroup\$ Decided to check the other two final test cases as well; both seem to be fine, so there should be no more errors in the current test suite unless I messed up somewhere. \$\endgroup\$ Commented Aug 31 at 19:54

2 Answers 2

6
\$\begingroup\$

Python, 85 84 83 bytes

f=lambda s,n,l,r=1:n and[f(s,n-1,i+1,i%2*2-1)for i in range(l)[::r]][::r]or next(s)

Takes the string (s) as an iterator of characters -- to take a str, replace n and with n*[s:=iter(s)]and for +16 +12 bytes (-4 bytes by att).
Assumes l is nonzero -- to support the case where l is zero, change <a> and<b>or <c> to <b>if <a> else <c> for +2 bytes.

Attempt This Online! (does not have the final 3 test cases)

Ungolfed:

def snake(src, dimensions, length, reverse=False):
    if dimensions == 0:
        return next(src)
    maybe_rev = lambda seq: seq[::-1] if reverse else seq
    return maybe_rev([snake(src, dimensions - 1, length=i+1, reverse=i%2==0)
        for i in maybe_rev(range(length))])

Not really much to say -- just uses recursion to build the specified simplex, with characters coming from a stateful iterator. In the golfed code, instead of a boolean reverse parameter, we take r: 1 for forwards, -1 for backwards. With that change, maybe_rev simply becomes lambda seq: seq[::r], and we use powers of -1 to express alternating directions.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ -(-1)**ii%2or-1, str input n*[s:=iter(s)] \$\endgroup\$
    – att
    Commented Sep 1 at 3:30
  • \$\begingroup\$ @att Thanks for the golfs! I decided to find a different 7-byte way of expressing -(-1)**i that didn't raise a SyntaxWarning, but it's thanks to you that I took another look at that part. (Another alternative is i-(i^1), which would be 5 bytes if only the operator precedence were a little different...) \$\endgroup\$ Commented Sep 1 at 16:31
  • 1
    \$\begingroup\$ ~i%-2|1 also ties \$\endgroup\$
    – xnor
    Commented Sep 2 at 2:13
2
\$\begingroup\$

Charcoal, 83 bytes

≔⪪⮌S¹θ≔EN¹ηFNUMη…η⊕λ≔⟦⟦⟧X⊟η¹⟧η⊞υ⟦⟦⟧η⟧FυFLι«≔§ικζ¿№ζ¹F⁺²κUMζ⎇λ§⮌ζν∧μ⊟θ⊞υ⎇﹪κ²ζ⮌ζ»⭆¹⊟η

Try it online! Link is to verbose version of code. Takes input in reverse order from the test cases. Explanation: Too many edge cases costing me a whole 10 bytes.

≔⪪⮌S¹θ

Input the string and convert it into a form that can be iterated over using Pop.

≔EN¹η

Make a 1-dimensional simplex of the input length.

FNUMη…η⊕λ

Expand the simplex by the input number of dimensions. (At this point I could consider 0 dimensions to be an edge case but fortunately it doesn't cost any bytes here.)

≔⟦⟦⟧X⊟η¹⟧η

Extract the last layer of the expanded simplex, which results in the desired number of dimensions. Deep clone it, as the expansion works by duplicating nested lists. Finally, wrap that in a list, to handle the edge case of 0 dimensions.

⊞υ⟦⟦⟧η⟧Fυ

Start traversing the wrapped simplex, but wrap it again, to handle the edge case of 1 dimension.

FLι«≔§ικζ

Loop over each slice of the current simplex.

¿№ζ¹F⁺²κUMζ⎇λ§⮌ζν∧μ⊟θ

If this is an edge then fill it from the input string, reversing it in place if it's an even edge (actually reversing it in place a number of times given by one more than its index; the 2 accounts for the fill operation, which it's golfier to inline as an extra loop pass).

⊞υ⎇﹪κ²ζ⮌ζ

Otherwise add this slice to the list of simplexes to process, reversing it if it's an even slice.

»⭆¹⊟η

Pretty-print the final simplex.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.