26
\$\begingroup\$

Make a program that takes a length and list of intervals and outputs a ruler of that length with longer ticks for each interval using the line drawing characters ┌ ┬ ┐ │ ╵

  • The first row of the output should begin with the tick for 0 with and end with a tick for the length with , with a being used for every character in between. There will be a total of length + 1 line drawing characters in this first row.
  • A tick should be lengthened vertically by half-character increments using and based on the input intervals.
  • Intervals are listed from smallest to largest, relative to the interval before it. To elaborate:
    • The first interval tells how many base ticks (the first row - one character per tick) are in the second-smallest interval (the smallest interval being 1). For example, [3] will lengthen every third tick by a half-character.
    • The second and subsequent intervals are in terms of the next smallest interval. For example [3, 5] will lengthen every 15th base tick by a full character and [3, 5, 2] will lengthen every 30th base tick by a character and a half.
    • A sub-interval of 1 is valid and effectively means that the last interval lines are lengthened by a full character instead of a half-character.
  • The example test cases should help to clarify how this works.

Examples/Test Cases

3, []:

┌┬┬┐

9, [3]:

┌┬┬┬┬┬┬┬┬┐
╵  ╵  ╵  ╵

30, [5, 2]:

┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐
│    ╵    │    ╵    │    ╵    │

32, [4, 2, 2, 2]:

┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐
│   ╵   │   ╵   │   ╵   │   ╵   │
│               ╵               │

48, [5, 3, 2]

┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐
│    ╵    ╵    │    ╵    ╵    │    ╵    ╵    │
╵                             ╵

24, [7, 3]

┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐
│      ╵      ╵      │

17, [3, 2, 1]

┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐
│  ╵  │  ╵  │  ╵
╵     ╵     ╵

1, [23, 19, 13, 11, 7, 5, 3, 2, 1]

┌┐
│
│
│
│
╵

Other Rules/Notes

  • Input and output can use any convenient format
  • The ruler doesn't have to end on a major tick
  • The interval list may be empty
  • The zeroth tick is always within all intervals.
  • You may assume the ruler length and intervals will always be a positive integers less than 120
  • Trailing whitespace is fine, but leading whitespace is not.
  • Any fixed-single-wide space is allowed as a spacing character if you, for some reason, want to use something other than ASCII spaces.

Happy Golfing!

\$\endgroup\$
5
  • \$\begingroup\$ For output, can I print the first row, then return a list of columns? \$\endgroup\$
    – Gymhgy
    Commented Feb 18, 2019 at 2:44
  • \$\begingroup\$ @EmbodimentofIgnorance, I'm gonna say no to that. Output should be consistent. \$\endgroup\$
    – Beefster
    Commented Feb 18, 2019 at 3:13
  • \$\begingroup\$ Can we take the box-drawing characters from a single-byte encoding (provided one exists containing the ones required)? \$\endgroup\$
    – Οurous
    Commented Feb 19, 2019 at 0:03
  • \$\begingroup\$ "any convenient format" - can we accept the interval list in reverse order? \$\endgroup\$
    – ngn
    Commented Feb 20, 2019 at 23:12
  • \$\begingroup\$ @ngn: I don't see why not. If that somehow helps you, go for it. \$\endgroup\$
    – Beefster
    Commented Feb 20, 2019 at 23:13

15 Answers 15

5
\$\begingroup\$

JavaScript (Node.js), 123 bytes

l=>g=([p,q,...t],h='┌'.padEnd(l,'┬')+`┐
`)=>p?h+g(t,h.replace(/\S/g,c=>'╵│ '[c>'╴'||++i%p?2:i/p%q<1|0],i=-1)):h

Try it online!

Use this function as f(20)([5, 2]).


Thanks Arnauld, saves 4 bytes.

\$\endgroup\$
0
3
\$\begingroup\$

Perl 6, 130 122 102 92 bytes

-10 bytes thanks to nwellnhof!

{'┌'~'┬'x$^a-1~'┐',|map {[~] <<' '╵ │>>[:1[$_ X%%@_]for 0..$a]},batch [\*] @^b: 2}

Try it online!

Ah yes, much shorter than my previous method. This is an anonymous code block that returns a list of lines.

Explanation:

{                                                   }   # Anonymous code block
 '┌'~'┬'x$^a-1~'┐',     # Return the first line
 |[\*] @^b          # Get the cumulative product of the input list
              .batch(2) # And split it into pairs
  .map:{                                      }  # Map each pair to
                                    for 0..$a    # For each interval
                        :1[$_ X%%@_]    # Whether it is divisible by none of the pair, one of the pair, or both
            <<' '╵ │>>[                     ]      # Map to a list of characters
        [~]        # And join
\$\endgroup\$
0
3
\$\begingroup\$

Dyalog APL, 66 64 58 52 bytes

{'┌┐'@0⍵@0⍉('┬│',⎕UCS 9589)/⍤1⍨1,⍉0 2⊤⊥¨⍨0=(⍵+1)⍴⍳⍺}

Try it online!

¯2 ¯8 ¯14 bytes thanks to ngn!

\$\endgroup\$
8
  • \$\begingroup\$ ∊'┌'(1↓⍵⍴'┬')'┐' -> '┌┬┐'/⍨2⍵2-1 \$\endgroup\$
    – ngn
    Commented Feb 20, 2019 at 22:25
  • \$\begingroup\$ @ngn thanks! These are the kinds of golfs that are pretty understandable but i never know to anticipate or know uses of \$\endgroup\$
    – dzaima
    Commented Feb 20, 2019 at 22:35
  • \$\begingroup\$ finally, i managed to shorten the rightmost part a little... +⌿0=(×\⍺)∘.|⍳1+⍵ -> ⊥¨⍨0=(⍵+1)⍴⍳⌽⍺. accepting in reverse order has been explicitly allowed now, so you can also remove the \$\endgroup\$
    – ngn
    Commented Feb 20, 2019 at 23:34
  • \$\begingroup\$ ('┌┬┐'/⍨2⍵2-1) -> '┌┬┐'[2,⍨×⍳⍵] \$\endgroup\$
    – ngn
    Commented Feb 22, 2019 at 14:19
  • \$\begingroup\$ or even better: ('┌┬┐'/⍨2⍵2-1)⍪⍉ -> '┌┐'@0⍵@0⍉'┬', \$\endgroup\$
    – ngn
    Commented Feb 22, 2019 at 14:26
2
\$\begingroup\$

Python 3, 173 172 bytes

def f(w,n):
 print('┌'+'┬'*~-w+'┐');R=r=range(w+1)
 for i,j in zip(*[iter(n+[0])]*2):a=r[::i];r=j*[0]and a[::j];print(''.join(' ╵│'[(v in a)+(v in r)]for v in R))

Try it online!

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 51 bytes

ÝεyIηPÖO2‰•5·W4•2äç×SI¯Qiεõ}}•áΣ=Yô•3äçy¹QyĀ+èš}ζJ»

Not too happy with the I¯Qiεõ}} as work-around for empty input-lists.. And can definitely be golfed at some other parts as well..

NOTE: Uses compressed integers converted to the required characters, because using the required characters directly means I'll have to count the entire program in UTF-8, increasing it by too much for all 05AB1E's builtin characters as well.

Try it online or verify all test cases.

Explanation:

Ý             # Create a list in the range [0, first (implicit) input-integer]
 ε            # Map each value `y` to:
   Iη         #  Get the prefixes of the second input-list
     P        #  Get the product of each prefix
  y   Ö       #  Check for each if its evenly dividing the value `y`
       O      #  Take the sum of that
        2‰    #  And then the divmod 2
  •5·W4•      #  Push compressed integer 94749589
        2ä    #  Split into two equal-sized parts: [9474,9589]
          ç   #  Convert each to a character: ["│","╵"]
           ×  #  Repeat each based on the divmod 2 result
            S #  And convert it to a flattened list of characters
  I¯Qi   }    #  If the second input-list was empty:
      εõ}     #   Map each list to an empty string
              #   (for some reason `€õ` doesn't work here..)
  •áΣ=Yô•     #  Push compressed integer 948495169488
         3ä   #  Split into three equal-sized parts: [9484,9516,9488]
           ç  #  Convert each to a character: ["┌","┬","┐"]
  y¹Q         #  Check if the value `y` is equal to the first input-integer
              #  (1 if truthy; 0 if falsey)
     yĀ       #  Check if the value `y` is NOT 0 (1 if truthy; 0 if falsey)
       +      #  Add both checks together
        è     #  Use it to index into the list ["┌","┬","┐"]
         š    #  And prepend the result in front of the other characters
 }ζ           # After the map: zip/transpose; swapping rows and columns (with space filler)
   J          # Join every inner list together to a single string
    »         # Join the lines with newline delimiter (and output implicitly)

See this 05AB1E tip of mine (section How to compress large integers?) to understand why •5·W4• is 94749589 and •áΣ=Yô• is 948495169488.

\$\endgroup\$
3
  • \$\begingroup\$ ×S can be и \$\endgroup\$ Commented Feb 18, 2019 at 21:54
  • \$\begingroup\$ @MagicOctopusUrn I first thought so too at first, but it doesn't unfortunately (just try it with one of the other test cases with a non-empty list). sиS does work, but unfortunately it's a byte longer instead of shorter. It's because the integers are on the stack first, and the strings after. With × it doesn't matter whether it's int,string or string,int, but with и it expects string,int. \$\endgroup\$ Commented Feb 19, 2019 at 13:06
  • \$\begingroup\$ Oh I see, man this one was confusing hah. Nice job on it, going to be honest I spent 10 minutes just trying to figure out what was going on, missed that tidbit about и! That'll be good to know in the future, I hadn't seen it used before one of your other answers. \$\endgroup\$ Commented Feb 19, 2019 at 14:24
2
\$\begingroup\$

Charcoal, 50 bytes

≔EηΠ…η⊕κη⪫┐┌×┬⊖θ↙↓EE⊕θΣEη¬﹪ιλ⁺×│⊘ι×╵﹪ι²‖

Try it online! Link is to verbose version of code. Box-drawing characters have a 3 byte representation in Charcoal, so the above string is only 40 characters long. Explanation:

≔EηΠ…η⊕κη

Calculate the cumulative product of the intervals.

⪫┐┌×┬⊖θ↙

Print the first row of tick marks. The left and right characters are the wrong way around because the result is reflected later.

↓EE⊕θΣEη¬﹪ιλ⁺×│⊘ι×╵﹪ι²

Calculate the number of intervals that are a factor of each tick mark. Generate a string of s of half that length and add for odd lengths. Print each string downwards with subsequent strings in previous columns, i.e. reverse order.

Reflect everything to get the ruler in left-to-right order.

\$\endgroup\$
2
\$\begingroup\$

Canvas, 42 41 40 bytes

┐”┬”┌”O⁸╷×oo╵(0)⁷{*J╵;∔}m{2n“╵”×;“│”××]⤢

Try it here! (with a font that monospaces the output)

\$\endgroup\$
2
\$\begingroup\$

Emacs Lisp, 303 bytes

(defun f(a)(princ'┌)(dotimes(i(1-(car a)))(princ'┬))(princ'┐)(let((m 1))(while(cadr a)(let((q(caadr a))(w (cadadr a)))(princ"\n")(dotimes(i(1+(car a)))(cond((if w(= 0(mod i(* m q w))))(princ'│))((= 0(mod i (* m q)))(princ'╵))(t(princ" "))))(setq m(* m q(if w w 1)))(setcdr a`(,(cddadr a)))))))

Use this function as (f '(30 (5 2))).

Better readable version:

(defun f (a)
  (princ '┌)
  (dotimes (i (1- (car a)))
    (princ '┬))
  (princ '┐)
  (let ((m 1))
    (while (cadr a)
      (let ((q (caadr a)) (w (cadadr a)))
    (princ "\n")
    (dotimes (i (1+ (car a)))
      (cond ((if w (= 0 (mod i (* m q w))))
        (princ '│))
       ((= 0 (mod i (* m q)))
        (princ '╵))
       (t
        (princ " "))))
    (setq m (* m q (if w w 1)))
    (setcdr a `(,(cddadr a)))))))
\$\endgroup\$
2
\$\begingroup\$

Jelly,  42  41 bytes

‘Rm×\}Ṭ€+2/
⁽!ṣ;“½¥÷I‘ÄỌṙ-;⁶
Ḷ¬;.Ḥ~W;ñị¢Y

A full program.
Try it online!

Or see a test-suite
Note: this code has been altered from a full program -- ñ (next link as a dyad) has been replaced with (link at index 1 as a dyad) to allow it to be called multiple times by the footer.

How?

‘Rm×\}Ṭ€+2/ - Link 1, lower interval tick types: length; intervals  e.g. 7; [3,2]
‘           - increment length                                           8
 R          - range                                                      [1,2,3,4,5,6,7,8]
     }      - use right argument for this monad as if it were a dyad:
   ×\       -   cumulative reduce by multiplication                      [3,6]
  m         - modulo slice (vectorises)                                  [[1,4,7],[1,7]]
      Ṭ€    - untruth €ach                               [[1,0,0,1,0,0,1],[1,0,0,0,0,0,1]]
        +2/ - pairwise reduce with addition                              [[2,0,0,1,0,0,2]]
            -   -- yielding a list of types for each row of characters below the first
            -      where 0 is a space, 1 is a short tick-mark and 2 is a long tick-mark

⁽!ṣ;“½¥÷I‘ÄỌṙ-;⁶ - Link 2, make character set: no arguments
⁽!ṣ              - literal 9474
    “½¥÷I‘       - list of code-page indices   = [10,4,28,73]
   ;             - concatenate              [9474,10,4,28,73]
          Ä      - cumulative addition      [9474,9484,9488,9516,9589]
           Ọ     - to characters            "│┌┐┬╵"
            ṙ-   - rotate left by -1        "╵│┌┐┬"
               ⁶ - literal space character  ' '
              ;  - concatenate              "╵│┌┐┬ "

Ḷ¬;.Ḥ~W;ñị¢Y - Main link: length, L; intervals, I
Ḷ            - lowered range         [ 0, 1, 2, ..., L-1]
 ¬           - logical Not           [ 1, 0, 0, ..., 0]
   .         - literal 0.5
  ;          - concatenate           [ 1, 0, 0, ..., 0, 0.5]
    Ḥ        - double                [ 2, 0, 0, ..., 0, 1]
     ~       - bitwise NOT           [-3,-1,-1, ...,-1,-2]
      W      - wrap that in a list  [[-3,-1,-1, ...,-1,-2]]
        ñ    - call next Link (1) as a dyad (f(L, I))
       ;     - (left) concatenated with (right)
          ¢  - call last Link (2) as a nilad (f())
         ị   - (left) index into (right)  (1-indexed and modular)
           Y - join with newline characters
             - implicit print
\$\endgroup\$
1
\$\begingroup\$

Ruby, 126 bytes

->l,i{y=1;[?┌+?┬*~-l+?┐]+i.each_slice(2).map{|j,k|x=y*j;y=k&&x*k;(0..l).map{|z|'│╵ '[(z%x<=>0)+(k ?z%y<=>0:1)]}*''}}

Try it online!

Looks rather verbose with all that each_slice stuff, but will do for now, unless I manage to find a golfier approach.

Takes input as l for length and i for intervals, returns an array of strings.

\$\endgroup\$
1
\$\begingroup\$

R, 175 170 bytes

function(l,i,`&`=rep)rbind(c('┌','┬'&l-1,'┐'),if(i)sapply(rowSums(!outer(0:l,cumprod(i),`%%`)),function(j,x=j%/%2,y=j%%2)c('│'&x,'╵'&y,' '&(1+sum(1|i))/2-x-y)))

Try it online!

Takes empty intervals as 0, returns a matrix of characters. TIO link displays the output pretty-printed.

\$\endgroup\$
1
\$\begingroup\$

Haskell, 167 164 149 bytes

n%l=unlines$("┌"++([2..n]>>"┬")++"┐"):[do p<-[0..n];let(j#a)b|1>p`rem`product(take j l)=a|1>0=b in(i-1)#(i#"│"$"╵")$" "|i<-[1,3..length l]]

Try it online! Slightly golfed different approach by Οurous.


n%l|let c=take(n+1).cycle;m&(x:y:r)=c('│':init([1..y]>>(m*x)!" "++"╵"))++'\n':(m*x*y)&r;m&[x]=c$'╵':(m*x)!" ";m&e=[]='┌':n!"┬"++"┐\n"++1&l
n!s=[2..n]>>s

Try it online! There are still some redundancies which look like they could be exploited, but so far they withstood all further golfing attempts.


The previous 167 byte solution is the same apart from newline handling and is probably slightly better readable:

n%l=unlines$('┌':n!"┬"++"┐"):(take(n+1)<$>1&l)
n!s=[2..n]>>s
m&(x:y:r)=cycle('│':init([1..y]>>(m*x)!" "++"╵")):(m*x*y)&r
m&[x]=[cycle$'╵':(m*x)!" "]
m&e=[]

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Different approach at 158 bytes (Try it online!), can probably be shortened quite a bit more since I don't speak Haskell well. \$\endgroup\$
    – Οurous
    Commented Feb 19, 2019 at 0:46
1
\$\begingroup\$

PowerShell, 152 bytes

param($t,$i)"┌$('┬'*--$t)┐"
$i|%{$s=++$s*$_-1;$p=".(.{$s}|.*$)"
if($r){$r-replace$p,'│$1';rv r}else{$r=' '*($t+2)-replace$p,'╵$1'}}
if($r){$r}

Try it online!

Unrolled:

param($ticks,$intervals)
"┌$('┬'*--$ticks)┐"                         # implicit output
$intervals|%{
    $step=++$step*$_-1
    $pattern=".(.{$step}|.*$)"
    if($row){
        $row-replace$pattern,'│$1'          # implicit output
        Remove-Variable row
    }else{
        $row=' '*($ticks+2)-replace$pattern,'╵$1'
    }
}
if($row){$row}                              # implicit output
\$\endgroup\$
2
  • 1
    \$\begingroup\$ hmm... the fails with the right output \$\endgroup\$
    – Veskah
    Commented Jul 18, 2019 at 14:26
  • 1
    \$\begingroup\$ You are right. 1) I didn't see a rule that allows trailing new line at the end. 2) and I don't like that the code sometimes adds a new line at the end, and sometimes it doesn't. :) \$\endgroup\$
    – mazzy
    Commented Jul 18, 2019 at 14:34
0
\$\begingroup\$

C# (Visual C# Interactive Compiler), 204 bytes

a=>b=>{Write("┌"+"┐\n".PadLeft(++a,'┬'));for(int i=1;;i++,WriteLine())for(int j=0;j<a;){var m=b.Select((c,d)=>b.Take(d+1).Aggregate((e,f)=>e*f)).Count(c=>j++%c<1);Write(m<1|i>m?" ":m<2?"╵":"|");}}

Try it online!

Outputs, but gets stuck in an infinite loop.

\$\endgroup\$
0
\$\begingroup\$

Clean, 221 201 195 162 bytes

import StdEnv
$n l=[["┌":repeatn(n-1)"┬"]++["┐"]:[[if(?(i-1))if(?i&&l%(i,i)>[])"│""╵"" "\\p<-[0..n],let?j=1>p rem(prod(l%(0,j)))
]\\i<-[1,3..length l]]]

Try it online!

Returns a list of lists of UTF-8 characters (as strings, since Clean has no innate UTF-8 support).

Works by generating the first line, then taking the product of prefixes of the list provided in groups of two, and checks which mark to draw based on whether the product divides the current character position.

Ungolfed:

$ n l
    = [
        ["┌": repeatn (n - 1) "┬"] ++ ["┐"]:
        [
            [
                if(? (i - 1))
                    if(? i && l%(i, i) > [])
                        "│"
                        "╵"
                    " "
                \\ p <- [0..n]
                , let
                    ? j = 1 > p rem (prod (l%(0, j)))
            ]
            \\ i <- [1, 3.. length l]
        ]
    ]
\$\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.