Covering a Skyline with brush strokes












43












$begingroup$


Given a non-negative integer skyline height list, answer how many uninterrupted 1-unit-high horizontal brush strokes are needed to cover it.



[1,3,2,1,2,1,5,3,3,4,2], visualised as:



      5    
5 4
3 5334
32 2 53342
13212153342


needs nine brush strokes:



      1    
2 3
4 5555
66 7 88888
99999999999


Examples



[1,3,2,1,2,1,5,3,3,4,2]9



[5,8]8



[1,1,1,1]1



0



[0,0]0



[2]2



[2,0,2]4



[10,9,8,9]11










share|improve this question











$endgroup$












  • $begingroup$
    For interested high-rep users: Based on this as per this.
    $endgroup$
    – Adám
    Feb 4 at 11:22






  • 2




    $begingroup$
    So, all brush strokes are horizontal?
    $endgroup$
    – tsh
    Feb 4 at 11:38






  • 1




    $begingroup$
    @tsh Good point. Added.
    $endgroup$
    – Adám
    Feb 4 at 11:46










  • $begingroup$
    It wasnt codegolf, but I had this question for an interview code test about a year ago.
    $endgroup$
    – luizfzs
    Feb 6 at 22:19
















43












$begingroup$


Given a non-negative integer skyline height list, answer how many uninterrupted 1-unit-high horizontal brush strokes are needed to cover it.



[1,3,2,1,2,1,5,3,3,4,2], visualised as:



      5    
5 4
3 5334
32 2 53342
13212153342


needs nine brush strokes:



      1    
2 3
4 5555
66 7 88888
99999999999


Examples



[1,3,2,1,2,1,5,3,3,4,2]9



[5,8]8



[1,1,1,1]1



0



[0,0]0



[2]2



[2,0,2]4



[10,9,8,9]11










share|improve this question











$endgroup$












  • $begingroup$
    For interested high-rep users: Based on this as per this.
    $endgroup$
    – Adám
    Feb 4 at 11:22






  • 2




    $begingroup$
    So, all brush strokes are horizontal?
    $endgroup$
    – tsh
    Feb 4 at 11:38






  • 1




    $begingroup$
    @tsh Good point. Added.
    $endgroup$
    – Adám
    Feb 4 at 11:46










  • $begingroup$
    It wasnt codegolf, but I had this question for an interview code test about a year ago.
    $endgroup$
    – luizfzs
    Feb 6 at 22:19














43












43








43


1



$begingroup$


Given a non-negative integer skyline height list, answer how many uninterrupted 1-unit-high horizontal brush strokes are needed to cover it.



[1,3,2,1,2,1,5,3,3,4,2], visualised as:



      5    
5 4
3 5334
32 2 53342
13212153342


needs nine brush strokes:



      1    
2 3
4 5555
66 7 88888
99999999999


Examples



[1,3,2,1,2,1,5,3,3,4,2]9



[5,8]8



[1,1,1,1]1



0



[0,0]0



[2]2



[2,0,2]4



[10,9,8,9]11










share|improve this question











$endgroup$




Given a non-negative integer skyline height list, answer how many uninterrupted 1-unit-high horizontal brush strokes are needed to cover it.



[1,3,2,1,2,1,5,3,3,4,2], visualised as:



      5    
5 4
3 5334
32 2 53342
13212153342


needs nine brush strokes:



      1    
2 3
4 5555
66 7 88888
99999999999


Examples



[1,3,2,1,2,1,5,3,3,4,2]9



[5,8]8



[1,1,1,1]1



0



[0,0]0



[2]2



[2,0,2]4



[10,9,8,9]11







code-golf number array-manipulation geometry counting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 4 at 11:46







Adám

















asked Feb 4 at 11:12









AdámAdám

28.5k273197




28.5k273197












  • $begingroup$
    For interested high-rep users: Based on this as per this.
    $endgroup$
    – Adám
    Feb 4 at 11:22






  • 2




    $begingroup$
    So, all brush strokes are horizontal?
    $endgroup$
    – tsh
    Feb 4 at 11:38






  • 1




    $begingroup$
    @tsh Good point. Added.
    $endgroup$
    – Adám
    Feb 4 at 11:46










  • $begingroup$
    It wasnt codegolf, but I had this question for an interview code test about a year ago.
    $endgroup$
    – luizfzs
    Feb 6 at 22:19


















  • $begingroup$
    For interested high-rep users: Based on this as per this.
    $endgroup$
    – Adám
    Feb 4 at 11:22






  • 2




    $begingroup$
    So, all brush strokes are horizontal?
    $endgroup$
    – tsh
    Feb 4 at 11:38






  • 1




    $begingroup$
    @tsh Good point. Added.
    $endgroup$
    – Adám
    Feb 4 at 11:46










  • $begingroup$
    It wasnt codegolf, but I had this question for an interview code test about a year ago.
    $endgroup$
    – luizfzs
    Feb 6 at 22:19
















$begingroup$
For interested high-rep users: Based on this as per this.
$endgroup$
– Adám
Feb 4 at 11:22




$begingroup$
For interested high-rep users: Based on this as per this.
$endgroup$
– Adám
Feb 4 at 11:22




2




2




$begingroup$
So, all brush strokes are horizontal?
$endgroup$
– tsh
Feb 4 at 11:38




$begingroup$
So, all brush strokes are horizontal?
$endgroup$
– tsh
Feb 4 at 11:38




1




1




$begingroup$
@tsh Good point. Added.
$endgroup$
– Adám
Feb 4 at 11:46




$begingroup$
@tsh Good point. Added.
$endgroup$
– Adám
Feb 4 at 11:46












$begingroup$
It wasnt codegolf, but I had this question for an interview code test about a year ago.
$endgroup$
– luizfzs
Feb 6 at 22:19




$begingroup$
It wasnt codegolf, but I had this question for an interview code test about a year ago.
$endgroup$
– luizfzs
Feb 6 at 22:19










23 Answers
23






active

oldest

votes


















35












$begingroup$


JavaScript (Node.js), 38 bytes





a=>a.map(v=>(n+=v>p&&v-p,p=v),p=n=0)|n


Try it online!



Simply a greedy algorithm which scan from left to right, only draw lines if needed, and draw it as long as possible.



Thanks Arnauld, save 2 3 bytes






share|improve this answer











$endgroup$













  • $begingroup$
    @Arnauld nice catch. totally forgot it.
    $endgroup$
    – tsh
    Feb 4 at 11:56










  • $begingroup$
    How did you realise this?
    $endgroup$
    – Adám
    Feb 4 at 13:19










  • $begingroup$
    @Adám Nothing magic. First time I read the question I was confused by how to search until i realize all lines are horizontal only. And then this formula just came up to my mind naturally....
    $endgroup$
    – tsh
    Feb 4 at 13:45






  • 4




    $begingroup$
    magic seems like a well-fitting word to describe that process.
    $endgroup$
    – Adám
    Feb 4 at 13:46






  • 1




    $begingroup$
    While this is the origin of the now widely used algorithm, it is explained here.
    $endgroup$
    – Adám
    Feb 5 at 12:14





















28












$begingroup$


05AB1E,  8 7  5 bytes



Saved 2 bytes thanks to @Adnan



0š¥þO


Try it online!



How?



This is using the algorithm that was first found by @tsh. If you like this answer, make sure to upvote their answer as well!



Each time a skyscraper is lower than or as high as the previous one, it can be painted 'for free' by simply extending the brushstrokes.



For instance, painting skyscrapers $B$ and $C$ in the figure below costs nothing.



On the other hand, we need 2 new brushstrokes to paint skyscraper $E$, no matter if they're going to be reused after that or not.



buildings



For the first skyscraper, we always need as many brushstrokes as there are floors in it.



Turning this into maths:



$$S=h_0+sum_{i=1}^n max(h_i-h_{i-1},0)$$



If we prepend $0$ to the list, this can be simplified to:



$$S=sum_{i=1}^n max(h_i-h_{i-1},0)$$



Commented



0š¥þO     # expects a list of non-negative integers  e.g. [10, 9, 8, 9]
0š # prepend 0 to the list --> [0, 10, 9, 8, 9]
¥ # compute deltas --> [10, -1, -1, 1]
þ # keep only values made of decimal digits
# (i.e. without a minus sign) --> ["10", "1"]
O # sum --> 11





share|improve this answer











$endgroup$













  • $begingroup$
    I think 0š¥ʒd}O saves you a byte.
    $endgroup$
    – Don't be a x-triple dot
    Feb 4 at 12:27










  • $begingroup$
    @Don'tbeax-tripledot I was editing my answer to exactly that when I saw your comment ;)
    $endgroup$
    – Arnauld
    Feb 4 at 12:29






  • 4




    $begingroup$
    Beautiful explanation.
    $endgroup$
    – Adám
    Feb 4 at 14:32






  • 1




    $begingroup$
    Replacing ʒd} with þ should save you two bytes.
    $endgroup$
    – Adnan
    Feb 4 at 15:23










  • $begingroup$
    @Adnan Ah, nice. Thanks!
    $endgroup$
    – Arnauld
    Feb 4 at 15:34



















7












$begingroup$


Python 3, 37 bytes





lambda a:sum(a)-sum(map(min,a[1:],a))


Try it online!



-5 bytes by switching to Python 3, thanks to Sarien






Python 2, 47 43 42 bytes





lambda a:sum(a)-sum(map(min,a[1:],a[:-1]))


Try it online!



Alt:





lambda a:sum(a)-sum(map(min,zip(a[1:],a)))


Try it online!






share|improve this answer











$endgroup$













  • $begingroup$
    In Python 3 you can ditch the [:-1], saving 5 bytes.
    $endgroup$
    – Sarien
    Feb 4 at 13:24










  • $begingroup$
    @Sarien Thanks :D, I didn't know map is different in python 2 and 3
    $endgroup$
    – TFeld
    Feb 4 at 13:28



















7












$begingroup$


Haskell, 32 bytes





(0%)
p%(h:t)=max(h-p)0+h%t
p%_=0


Try it online!



An improvement on Lynn's solution that tracks the previous element p instead of looking at the next element. This makes the base case and recursive call shorter in exchange for needing to invoke (0%).



max(h-p)0 could be max h p-p for the same length.






share|improve this answer









$endgroup$





















    5












    $begingroup$


    Haskell, 35 bytes





    f(a:b:c)=max(a-b)0+f(b:c)
    f a=sum a


    Try it online!



    Line 2 could be f[a]=a if I didn't also have to handle the case .






    share|improve this answer









    $endgroup$













    • $begingroup$
      Here's some competition.
      $endgroup$
      – TRITICIMAGVS
      Feb 4 at 19:34



















    5












    $begingroup$


    K (oK), 12 7 bytes



    -5 bytes thanks to ngn!



    A k (oK) port of Arnauld's 05AB1E solution (and tsh's JavaScript solution):



    +/0|-':


    Try it online!




    J, 15 bytes



    A J port of Arnauld's 05AB1E solution (and tsh's JavaScript solution):



    1#.0>./2-~/,]


    Try it online!



    My naive solution:




    J, 27 bytes



    1#.2(1 0-:]),@|:@,~1$~"0]


    Try it online!






    share|improve this answer











    $endgroup$









    • 2




      $begingroup$
      oK: each prior (':) uses an implicit identity element (0 for -) before the list, so 0, is unnecessary. you can omit { x} to make it a composition: +/0|-':
      $endgroup$
      – ngn
      Feb 4 at 15:11










    • $begingroup$
      @ngn Thanks! Apparently I've forgotten this: Some primitive verbs result in a different special-cased initial value: +, *, - and & are provided with 0, 1, 0 or the first element of the sequence, respectively
      $endgroup$
      – Galen Ivanov
      Feb 4 at 15:25



















    5












    $begingroup$


    Haskell, 34 32 bytes



    2 bytes trimmed by Lynn





    g x=sum$max 0<$>zipWith(-)x(0:x)


    Try it online!



    So to start we have zipWith(-). This takes two lists and produces a new list of their pairwise differences. We then combine it with x and (0:x). (0:) is a function that adds zero to the front of a list and by combining it with zipWith(-) we get the differences between consecutive elements of that list with a zero at the front. Then we turn all the negative ones to zero with (max 0<$>). This creates a new list where each element is the number of new strokes that has to be started at each tower. To get the total we just sum these with sum.






    share|improve this answer











    $endgroup$









    • 2




      $begingroup$
      g x=sum$max 0<$>zipWith(-)x(0:x) is 32 bytes :)
      $endgroup$
      – Lynn
      Feb 4 at 19:39










    • $begingroup$
      As is sum.zipWith((max 0.).(-))<*>(0:)
      $endgroup$
      – Lynn
      Feb 4 at 19:41










    • $begingroup$
      @Lynn Your second one is going to need extra parentheses since the . has higher precedence than <*>.
      $endgroup$
      – TRITICIMAGVS
      Feb 4 at 19:43



















    3












    $begingroup$


    Japt, 8 bytes



    -2 bytes from @Shaggy



    mîT Õ¸¸è


    Explanation



    mîT Õ¸¸è      Full program. Implicit input U
    e.g. U = [2,0,2]
    mîT Map each item X and repeat T(0) X times
    U = ["00","","00"]
    Õ Transpose rows with columns
    U = ["0 0","0 0"]
    ¸¸ Join using space and then split in space
    U = ["0","0","0","0"]
    è Return the count of the truthy values


    Try it online!






    share|improve this answer











    $endgroup$













    • $begingroup$
      8 bytes: mîT Õ¸¸è
      $endgroup$
      – Shaggy
      Feb 4 at 12:27






    • 1




      $begingroup$
      Nice use of A.y()'s padding, by the way.
      $endgroup$
      – Shaggy
      Feb 4 at 20:21



















    3












    $begingroup$


    MATL, 8 bytes



    0whd3Y%s


    Try it online!



    Pretty much @Arnauld's algorithm. Saved one byte (thanks @LuisMendo) by casting to uint64 rather than selecting ) positive entries.






    share|improve this answer











    $endgroup$





















      3












      $begingroup$


      Japt, 7 6 bytes



      änT fq


      Try it



      1 byte saved thanks to Oliver.



      änT xwT    :Implicit input of integer array
      än :Consecutive differences / Deltas
      T : After first prepending 0
      f :Filter elements by
      q : Square root (The square root of a negative being NaN)
      :Implicitly reduce by addition and output





      share|improve this answer











      $endgroup$









      • 1




        $begingroup$
        6 bytes
        $endgroup$
        – Oliver
        Feb 4 at 18:46










      • $begingroup$
        Nice one, @Oliver; wouldn't have thought of that.
        $endgroup$
        – Shaggy
        Feb 4 at 20:20



















      3












      $begingroup$


      R, 30 bytes





      sum(pmax(0,diff(c(0,scan()))))


      Try it online!



      Porting of @Arnauld solution which in turn derives from @tsh great solution






      share|improve this answer











      $endgroup$





















        2












        $begingroup$


        Retina 0.8.2, 21 bytes



        d+
        $*
        (1+)(?=,1)

        1


        Try it online! Link includes test cases. Explanation:



        d+
        $*


        Convert to unary.



        (1+)(?=,1)


        Delete all overlaps with the next tower, which don't need a new stroke.



        1


        Count the remaining strokes.






        share|improve this answer









        $endgroup$





















          2












          $begingroup$


          Jelly, 5 bytes



          A port of my 05AB1E answer, which itself is similar to @tsh JS answer.



          ŻI0»S


          Try it online!



          Commented



          ŻI0»S    - main link, expecting a list of non-negative integers  e.g. [10, 9, 8, 9]
          Ż - prepend 0 --> [0, 10, 9, 8, 9]
          I - compute the deltas --> [10, -1, -1, 1]
          0» - compute max(0, v) for each term v --> [10, 0, 0, 1]
          S - sum --> 11





          share|improve this answer











          $endgroup$





















            2












            $begingroup$

            Common Lisp, 88 87 bytes



            (lambda(s)(let((o 0))(dolist(c s)(incf o(max 0 c))(mapl(lambda(y)(decf(car y)c))s))o))


            non-minified



            (lambda (skyline)
            (let ((output 0))
            (dolist (current-skyscraper-height skyline)
            (incf output (max 0 current-skyscraper-height))
            (mapl (lambda (skyscraper)
            (decf (car skyscraper) current-skyscraper-height))
            skyline))
            output)))


            Test it



            When one tower is painted, it takes a number of brushstrokes equal to it's height. These brushstrokes translate to all the following ones, which is indicated here by subtracting the current tower's height from all other towers (and itself, but that doesn't matter). If a following tower is shorter, then it will be pushed to a negative number, and this negative number will subsequently subtracted from the towers that follow (indicating brushstrokes that could not be translated from a previous tower to the next ones). It actually just subtracts the number from all tower heights, including previous ones, but this doesn't matter because we don't look at the previous ones again.






            share|improve this answer










            New contributor




            Charlim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            $endgroup$













            • $begingroup$
              Welcome to PPCG. Could you possibly provide a link to an online testing environment for ease of verification?
              $endgroup$
              – Jonathan Frech
              Feb 7 at 0:52










            • $begingroup$
              Yes, absolutely. rextester.com/TKBU14782 The answer will be updated shortly
              $endgroup$
              – Charlim
              Feb 7 at 0:55










            • $begingroup$
              Well done. +1 for a nice, working first post. Have fun golfing.
              $endgroup$
              – Jonathan Frech
              Feb 7 at 0:58



















            1












            $begingroup$


            05AB1E, 13 10 bytes



            Z>Lε@γPO}O


            Try it online or verify all test cases.



            Explanation:





            Z            # Get the maximum of the (implicit) input-list
            > # Increase it by 1 (if the list only contains 0s)
            L # Create a list in the range [1, max]
            ε # Map each value to:
            @ # Check if this value is >= for each value in the (implicit) input
            γ # Split into chunks of adjacent equal digits
            P # Take the product of each inner list
            O # Take the sum
            }O # And after the map: take the sum (which is output implicitly)





            share|improve this answer











            $endgroup$





















              1












              $begingroup$


              C# (Visual C# Interactive Compiler) with flag /u:System.Math, 47 bytes





              n=>n.Select((a,i)=>i<1?a:Max(a-n[i-1],0)).Sum()


              Try it online!



              Old version, with flag /u:System.Math, 63 bytes





              n=>n.Aggregate((0,0),(a,b)=>(a.Item1+Max(0,b-a.Item2),b)).Item1


              I feel like this solution is more elegant than the first one. It goes through the array with a two-value tuple as a starting value, picking up values, and stores the value before it in the second part of the tuple.



              Try it online!






              share|improve this answer











              $endgroup$





















                1












                $begingroup$

                Pyth, 8 bytes



                s>#0.++0


                Yet another port of @tsh's marvellous answer. Takes the sum (s) of the positive values (>#0) of the deltas (.+) of the input with 0 prepended (+0Q, trailing Q inferred).



                Try it online here, or verify all the test cases at once here.



                String joining method, 10 bytes



                This was the solution I wrote before browsing the other answers.



                lcj.t+d*LN


                Test suite.



                lcj.t+d*LNQ   Implicit: Q=eval(input()), b=<newline>, N=<quote mark>
                Trailing Q inferred
                L Q Map each element of Q...
                * N ... to N repeated that many times
                +b Prepend a newline
                .t Transpose, padding with spaces
                j Join on newlines
                c Split on whitespace
                l Take the length, implicit print





                share|improve this answer









                $endgroup$





















                  1












                  $begingroup$

                  Clojure, 50 bytes



                  #((reduce(fn[[s n]i][(+(max(- i n)0)s)i])[0 0]%)0)


                  Try it online! (Why doesn't this print anything?)



                  #( ; begin anonymous function
                  (reduce
                  (fn [[s n] i] ; internal anonymous reducing function, destructures accumulator argument into a sum and the previous item
                  [(+ (max (- i n) 0) s ; the sum part of the accumulator becomes the previous sum plus the larger of zero and the difference between the current number and the last one, which is how many new strokes need to be started at this point
                  i]) ; ...and the previous item part becomes the current item
                  [0 0] ; the initial value of the accumulator gives no strokes yet, and nothing for them to cover yet
                  %) ; reduce over the argument to the function
                  0) ; and get the sum element of the last value of the accumulator.





                  share|improve this answer








                  New contributor




                  Tried Creating an Account is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  $endgroup$













                  • $begingroup$
                    Welcome to PPCG! I don't know anything about Clojure, but a quick search shows that you'll need to evaluate the lazy for loop. Try it Online! (Tip: you can use the link button to auto-format your answer). Hope you stick around and have some fun!
                    $endgroup$
                    – Jo King
                    Feb 6 at 8:47





















                  1












                  $begingroup$


                  Java (JDK), 57 bytes





                  a->{int s=0,p=0;for(int x:a)s-=(x>p?p:x)-(p=x);return s;}


                  Try it online!



                  Another port of tsh's Javascript answer. So make sure you've upvoted them.



                  Note that I used subtraction instead of addition because it allowed me to write (p=x) as right operand in the same statement.






                  share|improve this answer











                  $endgroup$





















                    0












                    $begingroup$


                    MATL, 15 14 13 bytes



                    ts:<~"@Y'x]vs


                    Input is a column vector, using ; as separator.



                    Try it online! Or verify all test cases.



                    Explanation



                    t       % Implicit input: column vector. Duplicate
                    s % Sum
                    : % Range from 1 to that. Gives a row vector
                    <~ % Greater or equal? Element-wise with broadcast
                    " % For each column
                    @ % Push current columnn
                    Y' % Run-length encoding. Gives vector of values (0, 1) and vector of lengths
                    x % Delete vector of lengths
                    ] % End
                    v % Vertically concatenate. May give an empty array
                    s % Sum. Implicit display





                    share|improve this answer











                    $endgroup$





















                      0












                      $begingroup$

                      Perl 5, 21 bytes



                      $+=$_>$'&&$_-$';//}{


                      TIO



                      How





                      • -p + }{ + $ trick


                      • // matches empty string so that for next line postmatch $' will contain previous line


                      • $+=$_>$'&&$_-$' to accumulate difference between current line and previous if current is greater than previous, (could also be written $+=$_-$' if$_>$', but perl doesn't parse $+=$_-$'if$_>$' the same)






                      share|improve this answer











                      $endgroup$





















                        0












                        $begingroup$


                        Stax, 8 bytes



                        Φ┐Γ╟φφ.╨


                        Run and debug it



                        Uses the widely used approach from tsh's JavaScript solution.






                        share|improve this answer









                        $endgroup$





















                          0












                          $begingroup$


                          Wolfram Language (Mathematica), 34 bytes



                          A port of @Arnauld's solution.



                          Tr@Select[{##,0}-{0,##},#>0&]&@@#&


                          Try it online!






                          share|improve this answer









                          $endgroup$













                            Your Answer





                            StackExchange.ifUsing("editor", function () {
                            return StackExchange.using("mathjaxEditing", function () {
                            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
                            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
                            });
                            });
                            }, "mathjax-editing");

                            StackExchange.ifUsing("editor", function () {
                            StackExchange.using("externalEditor", function () {
                            StackExchange.using("snippets", function () {
                            StackExchange.snippets.init();
                            });
                            });
                            }, "code-snippets");

                            StackExchange.ready(function() {
                            var channelOptions = {
                            tags: "".split(" "),
                            id: "200"
                            };
                            initTagRenderer("".split(" "), "".split(" "), channelOptions);

                            StackExchange.using("externalEditor", function() {
                            // Have to fire editor after snippets, if snippets enabled
                            if (StackExchange.settings.snippets.snippetsEnabled) {
                            StackExchange.using("snippets", function() {
                            createEditor();
                            });
                            }
                            else {
                            createEditor();
                            }
                            });

                            function createEditor() {
                            StackExchange.prepareEditor({
                            heartbeatType: 'answer',
                            autoActivateHeartbeat: false,
                            convertImagesToLinks: false,
                            noModals: true,
                            showLowRepImageUploadWarning: true,
                            reputationToPostImages: null,
                            bindNavPrevention: true,
                            postfix: "",
                            imageUploader: {
                            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                            allowUrls: true
                            },
                            onDemand: true,
                            discardSelector: ".discard-answer"
                            ,immediatelyShowMarkdownHelp:true
                            });


                            }
                            });














                            draft saved

                            draft discarded


















                            StackExchange.ready(
                            function () {
                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f179464%2fcovering-a-skyline-with-brush-strokes%23new-answer', 'question_page');
                            }
                            );

                            Post as a guest















                            Required, but never shown

























                            23 Answers
                            23






                            active

                            oldest

                            votes








                            23 Answers
                            23






                            active

                            oldest

                            votes









                            active

                            oldest

                            votes






                            active

                            oldest

                            votes









                            35












                            $begingroup$


                            JavaScript (Node.js), 38 bytes





                            a=>a.map(v=>(n+=v>p&&v-p,p=v),p=n=0)|n


                            Try it online!



                            Simply a greedy algorithm which scan from left to right, only draw lines if needed, and draw it as long as possible.



                            Thanks Arnauld, save 2 3 bytes






                            share|improve this answer











                            $endgroup$













                            • $begingroup$
                              @Arnauld nice catch. totally forgot it.
                              $endgroup$
                              – tsh
                              Feb 4 at 11:56










                            • $begingroup$
                              How did you realise this?
                              $endgroup$
                              – Adám
                              Feb 4 at 13:19










                            • $begingroup$
                              @Adám Nothing magic. First time I read the question I was confused by how to search until i realize all lines are horizontal only. And then this formula just came up to my mind naturally....
                              $endgroup$
                              – tsh
                              Feb 4 at 13:45






                            • 4




                              $begingroup$
                              magic seems like a well-fitting word to describe that process.
                              $endgroup$
                              – Adám
                              Feb 4 at 13:46






                            • 1




                              $begingroup$
                              While this is the origin of the now widely used algorithm, it is explained here.
                              $endgroup$
                              – Adám
                              Feb 5 at 12:14


















                            35












                            $begingroup$


                            JavaScript (Node.js), 38 bytes





                            a=>a.map(v=>(n+=v>p&&v-p,p=v),p=n=0)|n


                            Try it online!



                            Simply a greedy algorithm which scan from left to right, only draw lines if needed, and draw it as long as possible.



                            Thanks Arnauld, save 2 3 bytes






                            share|improve this answer











                            $endgroup$













                            • $begingroup$
                              @Arnauld nice catch. totally forgot it.
                              $endgroup$
                              – tsh
                              Feb 4 at 11:56










                            • $begingroup$
                              How did you realise this?
                              $endgroup$
                              – Adám
                              Feb 4 at 13:19










                            • $begingroup$
                              @Adám Nothing magic. First time I read the question I was confused by how to search until i realize all lines are horizontal only. And then this formula just came up to my mind naturally....
                              $endgroup$
                              – tsh
                              Feb 4 at 13:45






                            • 4




                              $begingroup$
                              magic seems like a well-fitting word to describe that process.
                              $endgroup$
                              – Adám
                              Feb 4 at 13:46






                            • 1




                              $begingroup$
                              While this is the origin of the now widely used algorithm, it is explained here.
                              $endgroup$
                              – Adám
                              Feb 5 at 12:14
















                            35












                            35








                            35





                            $begingroup$


                            JavaScript (Node.js), 38 bytes





                            a=>a.map(v=>(n+=v>p&&v-p,p=v),p=n=0)|n


                            Try it online!



                            Simply a greedy algorithm which scan from left to right, only draw lines if needed, and draw it as long as possible.



                            Thanks Arnauld, save 2 3 bytes






                            share|improve this answer











                            $endgroup$




                            JavaScript (Node.js), 38 bytes





                            a=>a.map(v=>(n+=v>p&&v-p,p=v),p=n=0)|n


                            Try it online!



                            Simply a greedy algorithm which scan from left to right, only draw lines if needed, and draw it as long as possible.



                            Thanks Arnauld, save 2 3 bytes







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Feb 4 at 14:03

























                            answered Feb 4 at 11:47









                            tshtsh

                            8,99511650




                            8,99511650












                            • $begingroup$
                              @Arnauld nice catch. totally forgot it.
                              $endgroup$
                              – tsh
                              Feb 4 at 11:56










                            • $begingroup$
                              How did you realise this?
                              $endgroup$
                              – Adám
                              Feb 4 at 13:19










                            • $begingroup$
                              @Adám Nothing magic. First time I read the question I was confused by how to search until i realize all lines are horizontal only. And then this formula just came up to my mind naturally....
                              $endgroup$
                              – tsh
                              Feb 4 at 13:45






                            • 4




                              $begingroup$
                              magic seems like a well-fitting word to describe that process.
                              $endgroup$
                              – Adám
                              Feb 4 at 13:46






                            • 1




                              $begingroup$
                              While this is the origin of the now widely used algorithm, it is explained here.
                              $endgroup$
                              – Adám
                              Feb 5 at 12:14




















                            • $begingroup$
                              @Arnauld nice catch. totally forgot it.
                              $endgroup$
                              – tsh
                              Feb 4 at 11:56










                            • $begingroup$
                              How did you realise this?
                              $endgroup$
                              – Adám
                              Feb 4 at 13:19










                            • $begingroup$
                              @Adám Nothing magic. First time I read the question I was confused by how to search until i realize all lines are horizontal only. And then this formula just came up to my mind naturally....
                              $endgroup$
                              – tsh
                              Feb 4 at 13:45






                            • 4




                              $begingroup$
                              magic seems like a well-fitting word to describe that process.
                              $endgroup$
                              – Adám
                              Feb 4 at 13:46






                            • 1




                              $begingroup$
                              While this is the origin of the now widely used algorithm, it is explained here.
                              $endgroup$
                              – Adám
                              Feb 5 at 12:14


















                            $begingroup$
                            @Arnauld nice catch. totally forgot it.
                            $endgroup$
                            – tsh
                            Feb 4 at 11:56




                            $begingroup$
                            @Arnauld nice catch. totally forgot it.
                            $endgroup$
                            – tsh
                            Feb 4 at 11:56












                            $begingroup$
                            How did you realise this?
                            $endgroup$
                            – Adám
                            Feb 4 at 13:19




                            $begingroup$
                            How did you realise this?
                            $endgroup$
                            – Adám
                            Feb 4 at 13:19












                            $begingroup$
                            @Adám Nothing magic. First time I read the question I was confused by how to search until i realize all lines are horizontal only. And then this formula just came up to my mind naturally....
                            $endgroup$
                            – tsh
                            Feb 4 at 13:45




                            $begingroup$
                            @Adám Nothing magic. First time I read the question I was confused by how to search until i realize all lines are horizontal only. And then this formula just came up to my mind naturally....
                            $endgroup$
                            – tsh
                            Feb 4 at 13:45




                            4




                            4




                            $begingroup$
                            magic seems like a well-fitting word to describe that process.
                            $endgroup$
                            – Adám
                            Feb 4 at 13:46




                            $begingroup$
                            magic seems like a well-fitting word to describe that process.
                            $endgroup$
                            – Adám
                            Feb 4 at 13:46




                            1




                            1




                            $begingroup$
                            While this is the origin of the now widely used algorithm, it is explained here.
                            $endgroup$
                            – Adám
                            Feb 5 at 12:14






                            $begingroup$
                            While this is the origin of the now widely used algorithm, it is explained here.
                            $endgroup$
                            – Adám
                            Feb 5 at 12:14













                            28












                            $begingroup$


                            05AB1E,  8 7  5 bytes



                            Saved 2 bytes thanks to @Adnan



                            0š¥þO


                            Try it online!



                            How?



                            This is using the algorithm that was first found by @tsh. If you like this answer, make sure to upvote their answer as well!



                            Each time a skyscraper is lower than or as high as the previous one, it can be painted 'for free' by simply extending the brushstrokes.



                            For instance, painting skyscrapers $B$ and $C$ in the figure below costs nothing.



                            On the other hand, we need 2 new brushstrokes to paint skyscraper $E$, no matter if they're going to be reused after that or not.



                            buildings



                            For the first skyscraper, we always need as many brushstrokes as there are floors in it.



                            Turning this into maths:



                            $$S=h_0+sum_{i=1}^n max(h_i-h_{i-1},0)$$



                            If we prepend $0$ to the list, this can be simplified to:



                            $$S=sum_{i=1}^n max(h_i-h_{i-1},0)$$



                            Commented



                            0š¥þO     # expects a list of non-negative integers  e.g. [10, 9, 8, 9]
                            0š # prepend 0 to the list --> [0, 10, 9, 8, 9]
                            ¥ # compute deltas --> [10, -1, -1, 1]
                            þ # keep only values made of decimal digits
                            # (i.e. without a minus sign) --> ["10", "1"]
                            O # sum --> 11





                            share|improve this answer











                            $endgroup$













                            • $begingroup$
                              I think 0š¥ʒd}O saves you a byte.
                              $endgroup$
                              – Don't be a x-triple dot
                              Feb 4 at 12:27










                            • $begingroup$
                              @Don'tbeax-tripledot I was editing my answer to exactly that when I saw your comment ;)
                              $endgroup$
                              – Arnauld
                              Feb 4 at 12:29






                            • 4




                              $begingroup$
                              Beautiful explanation.
                              $endgroup$
                              – Adám
                              Feb 4 at 14:32






                            • 1




                              $begingroup$
                              Replacing ʒd} with þ should save you two bytes.
                              $endgroup$
                              – Adnan
                              Feb 4 at 15:23










                            • $begingroup$
                              @Adnan Ah, nice. Thanks!
                              $endgroup$
                              – Arnauld
                              Feb 4 at 15:34
















                            28












                            $begingroup$


                            05AB1E,  8 7  5 bytes



                            Saved 2 bytes thanks to @Adnan



                            0š¥þO


                            Try it online!



                            How?



                            This is using the algorithm that was first found by @tsh. If you like this answer, make sure to upvote their answer as well!



                            Each time a skyscraper is lower than or as high as the previous one, it can be painted 'for free' by simply extending the brushstrokes.



                            For instance, painting skyscrapers $B$ and $C$ in the figure below costs nothing.



                            On the other hand, we need 2 new brushstrokes to paint skyscraper $E$, no matter if they're going to be reused after that or not.



                            buildings



                            For the first skyscraper, we always need as many brushstrokes as there are floors in it.



                            Turning this into maths:



                            $$S=h_0+sum_{i=1}^n max(h_i-h_{i-1},0)$$



                            If we prepend $0$ to the list, this can be simplified to:



                            $$S=sum_{i=1}^n max(h_i-h_{i-1},0)$$



                            Commented



                            0š¥þO     # expects a list of non-negative integers  e.g. [10, 9, 8, 9]
                            0š # prepend 0 to the list --> [0, 10, 9, 8, 9]
                            ¥ # compute deltas --> [10, -1, -1, 1]
                            þ # keep only values made of decimal digits
                            # (i.e. without a minus sign) --> ["10", "1"]
                            O # sum --> 11





                            share|improve this answer











                            $endgroup$













                            • $begingroup$
                              I think 0š¥ʒd}O saves you a byte.
                              $endgroup$
                              – Don't be a x-triple dot
                              Feb 4 at 12:27










                            • $begingroup$
                              @Don'tbeax-tripledot I was editing my answer to exactly that when I saw your comment ;)
                              $endgroup$
                              – Arnauld
                              Feb 4 at 12:29






                            • 4




                              $begingroup$
                              Beautiful explanation.
                              $endgroup$
                              – Adám
                              Feb 4 at 14:32






                            • 1




                              $begingroup$
                              Replacing ʒd} with þ should save you two bytes.
                              $endgroup$
                              – Adnan
                              Feb 4 at 15:23










                            • $begingroup$
                              @Adnan Ah, nice. Thanks!
                              $endgroup$
                              – Arnauld
                              Feb 4 at 15:34














                            28












                            28








                            28





                            $begingroup$


                            05AB1E,  8 7  5 bytes



                            Saved 2 bytes thanks to @Adnan



                            0š¥þO


                            Try it online!



                            How?



                            This is using the algorithm that was first found by @tsh. If you like this answer, make sure to upvote their answer as well!



                            Each time a skyscraper is lower than or as high as the previous one, it can be painted 'for free' by simply extending the brushstrokes.



                            For instance, painting skyscrapers $B$ and $C$ in the figure below costs nothing.



                            On the other hand, we need 2 new brushstrokes to paint skyscraper $E$, no matter if they're going to be reused after that or not.



                            buildings



                            For the first skyscraper, we always need as many brushstrokes as there are floors in it.



                            Turning this into maths:



                            $$S=h_0+sum_{i=1}^n max(h_i-h_{i-1},0)$$



                            If we prepend $0$ to the list, this can be simplified to:



                            $$S=sum_{i=1}^n max(h_i-h_{i-1},0)$$



                            Commented



                            0š¥þO     # expects a list of non-negative integers  e.g. [10, 9, 8, 9]
                            0š # prepend 0 to the list --> [0, 10, 9, 8, 9]
                            ¥ # compute deltas --> [10, -1, -1, 1]
                            þ # keep only values made of decimal digits
                            # (i.e. without a minus sign) --> ["10", "1"]
                            O # sum --> 11





                            share|improve this answer











                            $endgroup$




                            05AB1E,  8 7  5 bytes



                            Saved 2 bytes thanks to @Adnan



                            0š¥þO


                            Try it online!



                            How?



                            This is using the algorithm that was first found by @tsh. If you like this answer, make sure to upvote their answer as well!



                            Each time a skyscraper is lower than or as high as the previous one, it can be painted 'for free' by simply extending the brushstrokes.



                            For instance, painting skyscrapers $B$ and $C$ in the figure below costs nothing.



                            On the other hand, we need 2 new brushstrokes to paint skyscraper $E$, no matter if they're going to be reused after that or not.



                            buildings



                            For the first skyscraper, we always need as many brushstrokes as there are floors in it.



                            Turning this into maths:



                            $$S=h_0+sum_{i=1}^n max(h_i-h_{i-1},0)$$



                            If we prepend $0$ to the list, this can be simplified to:



                            $$S=sum_{i=1}^n max(h_i-h_{i-1},0)$$



                            Commented



                            0š¥þO     # expects a list of non-negative integers  e.g. [10, 9, 8, 9]
                            0š # prepend 0 to the list --> [0, 10, 9, 8, 9]
                            ¥ # compute deltas --> [10, -1, -1, 1]
                            þ # keep only values made of decimal digits
                            # (i.e. without a minus sign) --> ["10", "1"]
                            O # sum --> 11






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Feb 4 at 15:33

























                            answered Feb 4 at 12:11









                            ArnauldArnauld

                            75.9k693320




                            75.9k693320












                            • $begingroup$
                              I think 0š¥ʒd}O saves you a byte.
                              $endgroup$
                              – Don't be a x-triple dot
                              Feb 4 at 12:27










                            • $begingroup$
                              @Don'tbeax-tripledot I was editing my answer to exactly that when I saw your comment ;)
                              $endgroup$
                              – Arnauld
                              Feb 4 at 12:29






                            • 4




                              $begingroup$
                              Beautiful explanation.
                              $endgroup$
                              – Adám
                              Feb 4 at 14:32






                            • 1




                              $begingroup$
                              Replacing ʒd} with þ should save you two bytes.
                              $endgroup$
                              – Adnan
                              Feb 4 at 15:23










                            • $begingroup$
                              @Adnan Ah, nice. Thanks!
                              $endgroup$
                              – Arnauld
                              Feb 4 at 15:34


















                            • $begingroup$
                              I think 0š¥ʒd}O saves you a byte.
                              $endgroup$
                              – Don't be a x-triple dot
                              Feb 4 at 12:27










                            • $begingroup$
                              @Don'tbeax-tripledot I was editing my answer to exactly that when I saw your comment ;)
                              $endgroup$
                              – Arnauld
                              Feb 4 at 12:29






                            • 4




                              $begingroup$
                              Beautiful explanation.
                              $endgroup$
                              – Adám
                              Feb 4 at 14:32






                            • 1




                              $begingroup$
                              Replacing ʒd} with þ should save you two bytes.
                              $endgroup$
                              – Adnan
                              Feb 4 at 15:23










                            • $begingroup$
                              @Adnan Ah, nice. Thanks!
                              $endgroup$
                              – Arnauld
                              Feb 4 at 15:34
















                            $begingroup$
                            I think 0š¥ʒd}O saves you a byte.
                            $endgroup$
                            – Don't be a x-triple dot
                            Feb 4 at 12:27




                            $begingroup$
                            I think 0š¥ʒd}O saves you a byte.
                            $endgroup$
                            – Don't be a x-triple dot
                            Feb 4 at 12:27












                            $begingroup$
                            @Don'tbeax-tripledot I was editing my answer to exactly that when I saw your comment ;)
                            $endgroup$
                            – Arnauld
                            Feb 4 at 12:29




                            $begingroup$
                            @Don'tbeax-tripledot I was editing my answer to exactly that when I saw your comment ;)
                            $endgroup$
                            – Arnauld
                            Feb 4 at 12:29




                            4




                            4




                            $begingroup$
                            Beautiful explanation.
                            $endgroup$
                            – Adám
                            Feb 4 at 14:32




                            $begingroup$
                            Beautiful explanation.
                            $endgroup$
                            – Adám
                            Feb 4 at 14:32




                            1




                            1




                            $begingroup$
                            Replacing ʒd} with þ should save you two bytes.
                            $endgroup$
                            – Adnan
                            Feb 4 at 15:23




                            $begingroup$
                            Replacing ʒd} with þ should save you two bytes.
                            $endgroup$
                            – Adnan
                            Feb 4 at 15:23












                            $begingroup$
                            @Adnan Ah, nice. Thanks!
                            $endgroup$
                            – Arnauld
                            Feb 4 at 15:34




                            $begingroup$
                            @Adnan Ah, nice. Thanks!
                            $endgroup$
                            – Arnauld
                            Feb 4 at 15:34











                            7












                            $begingroup$


                            Python 3, 37 bytes





                            lambda a:sum(a)-sum(map(min,a[1:],a))


                            Try it online!



                            -5 bytes by switching to Python 3, thanks to Sarien






                            Python 2, 47 43 42 bytes





                            lambda a:sum(a)-sum(map(min,a[1:],a[:-1]))


                            Try it online!



                            Alt:





                            lambda a:sum(a)-sum(map(min,zip(a[1:],a)))


                            Try it online!






                            share|improve this answer











                            $endgroup$













                            • $begingroup$
                              In Python 3 you can ditch the [:-1], saving 5 bytes.
                              $endgroup$
                              – Sarien
                              Feb 4 at 13:24










                            • $begingroup$
                              @Sarien Thanks :D, I didn't know map is different in python 2 and 3
                              $endgroup$
                              – TFeld
                              Feb 4 at 13:28
















                            7












                            $begingroup$


                            Python 3, 37 bytes





                            lambda a:sum(a)-sum(map(min,a[1:],a))


                            Try it online!



                            -5 bytes by switching to Python 3, thanks to Sarien






                            Python 2, 47 43 42 bytes





                            lambda a:sum(a)-sum(map(min,a[1:],a[:-1]))


                            Try it online!



                            Alt:





                            lambda a:sum(a)-sum(map(min,zip(a[1:],a)))


                            Try it online!






                            share|improve this answer











                            $endgroup$













                            • $begingroup$
                              In Python 3 you can ditch the [:-1], saving 5 bytes.
                              $endgroup$
                              – Sarien
                              Feb 4 at 13:24










                            • $begingroup$
                              @Sarien Thanks :D, I didn't know map is different in python 2 and 3
                              $endgroup$
                              – TFeld
                              Feb 4 at 13:28














                            7












                            7








                            7





                            $begingroup$


                            Python 3, 37 bytes





                            lambda a:sum(a)-sum(map(min,a[1:],a))


                            Try it online!



                            -5 bytes by switching to Python 3, thanks to Sarien






                            Python 2, 47 43 42 bytes





                            lambda a:sum(a)-sum(map(min,a[1:],a[:-1]))


                            Try it online!



                            Alt:





                            lambda a:sum(a)-sum(map(min,zip(a[1:],a)))


                            Try it online!






                            share|improve this answer











                            $endgroup$




                            Python 3, 37 bytes





                            lambda a:sum(a)-sum(map(min,a[1:],a))


                            Try it online!



                            -5 bytes by switching to Python 3, thanks to Sarien






                            Python 2, 47 43 42 bytes





                            lambda a:sum(a)-sum(map(min,a[1:],a[:-1]))


                            Try it online!



                            Alt:





                            lambda a:sum(a)-sum(map(min,zip(a[1:],a)))


                            Try it online!







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Feb 4 at 13:27

























                            answered Feb 4 at 11:40









                            TFeldTFeld

                            15.1k21244




                            15.1k21244












                            • $begingroup$
                              In Python 3 you can ditch the [:-1], saving 5 bytes.
                              $endgroup$
                              – Sarien
                              Feb 4 at 13:24










                            • $begingroup$
                              @Sarien Thanks :D, I didn't know map is different in python 2 and 3
                              $endgroup$
                              – TFeld
                              Feb 4 at 13:28


















                            • $begingroup$
                              In Python 3 you can ditch the [:-1], saving 5 bytes.
                              $endgroup$
                              – Sarien
                              Feb 4 at 13:24










                            • $begingroup$
                              @Sarien Thanks :D, I didn't know map is different in python 2 and 3
                              $endgroup$
                              – TFeld
                              Feb 4 at 13:28
















                            $begingroup$
                            In Python 3 you can ditch the [:-1], saving 5 bytes.
                            $endgroup$
                            – Sarien
                            Feb 4 at 13:24




                            $begingroup$
                            In Python 3 you can ditch the [:-1], saving 5 bytes.
                            $endgroup$
                            – Sarien
                            Feb 4 at 13:24












                            $begingroup$
                            @Sarien Thanks :D, I didn't know map is different in python 2 and 3
                            $endgroup$
                            – TFeld
                            Feb 4 at 13:28




                            $begingroup$
                            @Sarien Thanks :D, I didn't know map is different in python 2 and 3
                            $endgroup$
                            – TFeld
                            Feb 4 at 13:28











                            7












                            $begingroup$


                            Haskell, 32 bytes





                            (0%)
                            p%(h:t)=max(h-p)0+h%t
                            p%_=0


                            Try it online!



                            An improvement on Lynn's solution that tracks the previous element p instead of looking at the next element. This makes the base case and recursive call shorter in exchange for needing to invoke (0%).



                            max(h-p)0 could be max h p-p for the same length.






                            share|improve this answer









                            $endgroup$


















                              7












                              $begingroup$


                              Haskell, 32 bytes





                              (0%)
                              p%(h:t)=max(h-p)0+h%t
                              p%_=0


                              Try it online!



                              An improvement on Lynn's solution that tracks the previous element p instead of looking at the next element. This makes the base case and recursive call shorter in exchange for needing to invoke (0%).



                              max(h-p)0 could be max h p-p for the same length.






                              share|improve this answer









                              $endgroup$
















                                7












                                7








                                7





                                $begingroup$


                                Haskell, 32 bytes





                                (0%)
                                p%(h:t)=max(h-p)0+h%t
                                p%_=0


                                Try it online!



                                An improvement on Lynn's solution that tracks the previous element p instead of looking at the next element. This makes the base case and recursive call shorter in exchange for needing to invoke (0%).



                                max(h-p)0 could be max h p-p for the same length.






                                share|improve this answer









                                $endgroup$




                                Haskell, 32 bytes





                                (0%)
                                p%(h:t)=max(h-p)0+h%t
                                p%_=0


                                Try it online!



                                An improvement on Lynn's solution that tracks the previous element p instead of looking at the next element. This makes the base case and recursive call shorter in exchange for needing to invoke (0%).



                                max(h-p)0 could be max h p-p for the same length.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Feb 5 at 2:39









                                xnorxnor

                                90.4k18185439




                                90.4k18185439























                                    5












                                    $begingroup$


                                    Haskell, 35 bytes





                                    f(a:b:c)=max(a-b)0+f(b:c)
                                    f a=sum a


                                    Try it online!



                                    Line 2 could be f[a]=a if I didn't also have to handle the case .






                                    share|improve this answer









                                    $endgroup$













                                    • $begingroup$
                                      Here's some competition.
                                      $endgroup$
                                      – TRITICIMAGVS
                                      Feb 4 at 19:34
















                                    5












                                    $begingroup$


                                    Haskell, 35 bytes





                                    f(a:b:c)=max(a-b)0+f(b:c)
                                    f a=sum a


                                    Try it online!



                                    Line 2 could be f[a]=a if I didn't also have to handle the case .






                                    share|improve this answer









                                    $endgroup$













                                    • $begingroup$
                                      Here's some competition.
                                      $endgroup$
                                      – TRITICIMAGVS
                                      Feb 4 at 19:34














                                    5












                                    5








                                    5





                                    $begingroup$


                                    Haskell, 35 bytes





                                    f(a:b:c)=max(a-b)0+f(b:c)
                                    f a=sum a


                                    Try it online!



                                    Line 2 could be f[a]=a if I didn't also have to handle the case .






                                    share|improve this answer









                                    $endgroup$




                                    Haskell, 35 bytes





                                    f(a:b:c)=max(a-b)0+f(b:c)
                                    f a=sum a


                                    Try it online!



                                    Line 2 could be f[a]=a if I didn't also have to handle the case .







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Feb 4 at 15:44









                                    LynnLynn

                                    49.1k794227




                                    49.1k794227












                                    • $begingroup$
                                      Here's some competition.
                                      $endgroup$
                                      – TRITICIMAGVS
                                      Feb 4 at 19:34


















                                    • $begingroup$
                                      Here's some competition.
                                      $endgroup$
                                      – TRITICIMAGVS
                                      Feb 4 at 19:34
















                                    $begingroup$
                                    Here's some competition.
                                    $endgroup$
                                    – TRITICIMAGVS
                                    Feb 4 at 19:34




                                    $begingroup$
                                    Here's some competition.
                                    $endgroup$
                                    – TRITICIMAGVS
                                    Feb 4 at 19:34











                                    5












                                    $begingroup$


                                    K (oK), 12 7 bytes



                                    -5 bytes thanks to ngn!



                                    A k (oK) port of Arnauld's 05AB1E solution (and tsh's JavaScript solution):



                                    +/0|-':


                                    Try it online!




                                    J, 15 bytes



                                    A J port of Arnauld's 05AB1E solution (and tsh's JavaScript solution):



                                    1#.0>./2-~/,]


                                    Try it online!



                                    My naive solution:




                                    J, 27 bytes



                                    1#.2(1 0-:]),@|:@,~1$~"0]


                                    Try it online!






                                    share|improve this answer











                                    $endgroup$









                                    • 2




                                      $begingroup$
                                      oK: each prior (':) uses an implicit identity element (0 for -) before the list, so 0, is unnecessary. you can omit { x} to make it a composition: +/0|-':
                                      $endgroup$
                                      – ngn
                                      Feb 4 at 15:11










                                    • $begingroup$
                                      @ngn Thanks! Apparently I've forgotten this: Some primitive verbs result in a different special-cased initial value: +, *, - and & are provided with 0, 1, 0 or the first element of the sequence, respectively
                                      $endgroup$
                                      – Galen Ivanov
                                      Feb 4 at 15:25
















                                    5












                                    $begingroup$


                                    K (oK), 12 7 bytes



                                    -5 bytes thanks to ngn!



                                    A k (oK) port of Arnauld's 05AB1E solution (and tsh's JavaScript solution):



                                    +/0|-':


                                    Try it online!




                                    J, 15 bytes



                                    A J port of Arnauld's 05AB1E solution (and tsh's JavaScript solution):



                                    1#.0>./2-~/,]


                                    Try it online!



                                    My naive solution:




                                    J, 27 bytes



                                    1#.2(1 0-:]),@|:@,~1$~"0]


                                    Try it online!






                                    share|improve this answer











                                    $endgroup$









                                    • 2




                                      $begingroup$
                                      oK: each prior (':) uses an implicit identity element (0 for -) before the list, so 0, is unnecessary. you can omit { x} to make it a composition: +/0|-':
                                      $endgroup$
                                      – ngn
                                      Feb 4 at 15:11










                                    • $begingroup$
                                      @ngn Thanks! Apparently I've forgotten this: Some primitive verbs result in a different special-cased initial value: +, *, - and & are provided with 0, 1, 0 or the first element of the sequence, respectively
                                      $endgroup$
                                      – Galen Ivanov
                                      Feb 4 at 15:25














                                    5












                                    5








                                    5





                                    $begingroup$


                                    K (oK), 12 7 bytes



                                    -5 bytes thanks to ngn!



                                    A k (oK) port of Arnauld's 05AB1E solution (and tsh's JavaScript solution):



                                    +/0|-':


                                    Try it online!




                                    J, 15 bytes



                                    A J port of Arnauld's 05AB1E solution (and tsh's JavaScript solution):



                                    1#.0>./2-~/,]


                                    Try it online!



                                    My naive solution:




                                    J, 27 bytes



                                    1#.2(1 0-:]),@|:@,~1$~"0]


                                    Try it online!






                                    share|improve this answer











                                    $endgroup$




                                    K (oK), 12 7 bytes



                                    -5 bytes thanks to ngn!



                                    A k (oK) port of Arnauld's 05AB1E solution (and tsh's JavaScript solution):



                                    +/0|-':


                                    Try it online!




                                    J, 15 bytes



                                    A J port of Arnauld's 05AB1E solution (and tsh's JavaScript solution):



                                    1#.0>./2-~/,]


                                    Try it online!



                                    My naive solution:




                                    J, 27 bytes



                                    1#.2(1 0-:]),@|:@,~1$~"0]


                                    Try it online!







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Feb 4 at 15:45

























                                    answered Feb 4 at 11:58









                                    Galen IvanovGalen Ivanov

                                    6,76711033




                                    6,76711033








                                    • 2




                                      $begingroup$
                                      oK: each prior (':) uses an implicit identity element (0 for -) before the list, so 0, is unnecessary. you can omit { x} to make it a composition: +/0|-':
                                      $endgroup$
                                      – ngn
                                      Feb 4 at 15:11










                                    • $begingroup$
                                      @ngn Thanks! Apparently I've forgotten this: Some primitive verbs result in a different special-cased initial value: +, *, - and & are provided with 0, 1, 0 or the first element of the sequence, respectively
                                      $endgroup$
                                      – Galen Ivanov
                                      Feb 4 at 15:25














                                    • 2




                                      $begingroup$
                                      oK: each prior (':) uses an implicit identity element (0 for -) before the list, so 0, is unnecessary. you can omit { x} to make it a composition: +/0|-':
                                      $endgroup$
                                      – ngn
                                      Feb 4 at 15:11










                                    • $begingroup$
                                      @ngn Thanks! Apparently I've forgotten this: Some primitive verbs result in a different special-cased initial value: +, *, - and & are provided with 0, 1, 0 or the first element of the sequence, respectively
                                      $endgroup$
                                      – Galen Ivanov
                                      Feb 4 at 15:25








                                    2




                                    2




                                    $begingroup$
                                    oK: each prior (':) uses an implicit identity element (0 for -) before the list, so 0, is unnecessary. you can omit { x} to make it a composition: +/0|-':
                                    $endgroup$
                                    – ngn
                                    Feb 4 at 15:11




                                    $begingroup$
                                    oK: each prior (':) uses an implicit identity element (0 for -) before the list, so 0, is unnecessary. you can omit { x} to make it a composition: +/0|-':
                                    $endgroup$
                                    – ngn
                                    Feb 4 at 15:11












                                    $begingroup$
                                    @ngn Thanks! Apparently I've forgotten this: Some primitive verbs result in a different special-cased initial value: +, *, - and & are provided with 0, 1, 0 or the first element of the sequence, respectively
                                    $endgroup$
                                    – Galen Ivanov
                                    Feb 4 at 15:25




                                    $begingroup$
                                    @ngn Thanks! Apparently I've forgotten this: Some primitive verbs result in a different special-cased initial value: +, *, - and & are provided with 0, 1, 0 or the first element of the sequence, respectively
                                    $endgroup$
                                    – Galen Ivanov
                                    Feb 4 at 15:25











                                    5












                                    $begingroup$


                                    Haskell, 34 32 bytes



                                    2 bytes trimmed by Lynn





                                    g x=sum$max 0<$>zipWith(-)x(0:x)


                                    Try it online!



                                    So to start we have zipWith(-). This takes two lists and produces a new list of their pairwise differences. We then combine it with x and (0:x). (0:) is a function that adds zero to the front of a list and by combining it with zipWith(-) we get the differences between consecutive elements of that list with a zero at the front. Then we turn all the negative ones to zero with (max 0<$>). This creates a new list where each element is the number of new strokes that has to be started at each tower. To get the total we just sum these with sum.






                                    share|improve this answer











                                    $endgroup$









                                    • 2




                                      $begingroup$
                                      g x=sum$max 0<$>zipWith(-)x(0:x) is 32 bytes :)
                                      $endgroup$
                                      – Lynn
                                      Feb 4 at 19:39










                                    • $begingroup$
                                      As is sum.zipWith((max 0.).(-))<*>(0:)
                                      $endgroup$
                                      – Lynn
                                      Feb 4 at 19:41










                                    • $begingroup$
                                      @Lynn Your second one is going to need extra parentheses since the . has higher precedence than <*>.
                                      $endgroup$
                                      – TRITICIMAGVS
                                      Feb 4 at 19:43
















                                    5












                                    $begingroup$


                                    Haskell, 34 32 bytes



                                    2 bytes trimmed by Lynn





                                    g x=sum$max 0<$>zipWith(-)x(0:x)


                                    Try it online!



                                    So to start we have zipWith(-). This takes two lists and produces a new list of their pairwise differences. We then combine it with x and (0:x). (0:) is a function that adds zero to the front of a list and by combining it with zipWith(-) we get the differences between consecutive elements of that list with a zero at the front. Then we turn all the negative ones to zero with (max 0<$>). This creates a new list where each element is the number of new strokes that has to be started at each tower. To get the total we just sum these with sum.






                                    share|improve this answer











                                    $endgroup$









                                    • 2




                                      $begingroup$
                                      g x=sum$max 0<$>zipWith(-)x(0:x) is 32 bytes :)
                                      $endgroup$
                                      – Lynn
                                      Feb 4 at 19:39










                                    • $begingroup$
                                      As is sum.zipWith((max 0.).(-))<*>(0:)
                                      $endgroup$
                                      – Lynn
                                      Feb 4 at 19:41










                                    • $begingroup$
                                      @Lynn Your second one is going to need extra parentheses since the . has higher precedence than <*>.
                                      $endgroup$
                                      – TRITICIMAGVS
                                      Feb 4 at 19:43














                                    5












                                    5








                                    5





                                    $begingroup$


                                    Haskell, 34 32 bytes



                                    2 bytes trimmed by Lynn





                                    g x=sum$max 0<$>zipWith(-)x(0:x)


                                    Try it online!



                                    So to start we have zipWith(-). This takes two lists and produces a new list of their pairwise differences. We then combine it with x and (0:x). (0:) is a function that adds zero to the front of a list and by combining it with zipWith(-) we get the differences between consecutive elements of that list with a zero at the front. Then we turn all the negative ones to zero with (max 0<$>). This creates a new list where each element is the number of new strokes that has to be started at each tower. To get the total we just sum these with sum.






                                    share|improve this answer











                                    $endgroup$




                                    Haskell, 34 32 bytes



                                    2 bytes trimmed by Lynn





                                    g x=sum$max 0<$>zipWith(-)x(0:x)


                                    Try it online!



                                    So to start we have zipWith(-). This takes two lists and produces a new list of their pairwise differences. We then combine it with x and (0:x). (0:) is a function that adds zero to the front of a list and by combining it with zipWith(-) we get the differences between consecutive elements of that list with a zero at the front. Then we turn all the negative ones to zero with (max 0<$>). This creates a new list where each element is the number of new strokes that has to be started at each tower. To get the total we just sum these with sum.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Feb 6 at 14:27

























                                    answered Feb 4 at 14:46









                                    TRITICIMAGVSTRITICIMAGVS

                                    34.7k10157369




                                    34.7k10157369








                                    • 2




                                      $begingroup$
                                      g x=sum$max 0<$>zipWith(-)x(0:x) is 32 bytes :)
                                      $endgroup$
                                      – Lynn
                                      Feb 4 at 19:39










                                    • $begingroup$
                                      As is sum.zipWith((max 0.).(-))<*>(0:)
                                      $endgroup$
                                      – Lynn
                                      Feb 4 at 19:41










                                    • $begingroup$
                                      @Lynn Your second one is going to need extra parentheses since the . has higher precedence than <*>.
                                      $endgroup$
                                      – TRITICIMAGVS
                                      Feb 4 at 19:43














                                    • 2




                                      $begingroup$
                                      g x=sum$max 0<$>zipWith(-)x(0:x) is 32 bytes :)
                                      $endgroup$
                                      – Lynn
                                      Feb 4 at 19:39










                                    • $begingroup$
                                      As is sum.zipWith((max 0.).(-))<*>(0:)
                                      $endgroup$
                                      – Lynn
                                      Feb 4 at 19:41










                                    • $begingroup$
                                      @Lynn Your second one is going to need extra parentheses since the . has higher precedence than <*>.
                                      $endgroup$
                                      – TRITICIMAGVS
                                      Feb 4 at 19:43








                                    2




                                    2




                                    $begingroup$
                                    g x=sum$max 0<$>zipWith(-)x(0:x) is 32 bytes :)
                                    $endgroup$
                                    – Lynn
                                    Feb 4 at 19:39




                                    $begingroup$
                                    g x=sum$max 0<$>zipWith(-)x(0:x) is 32 bytes :)
                                    $endgroup$
                                    – Lynn
                                    Feb 4 at 19:39












                                    $begingroup$
                                    As is sum.zipWith((max 0.).(-))<*>(0:)
                                    $endgroup$
                                    – Lynn
                                    Feb 4 at 19:41




                                    $begingroup$
                                    As is sum.zipWith((max 0.).(-))<*>(0:)
                                    $endgroup$
                                    – Lynn
                                    Feb 4 at 19:41












                                    $begingroup$
                                    @Lynn Your second one is going to need extra parentheses since the . has higher precedence than <*>.
                                    $endgroup$
                                    – TRITICIMAGVS
                                    Feb 4 at 19:43




                                    $begingroup$
                                    @Lynn Your second one is going to need extra parentheses since the . has higher precedence than <*>.
                                    $endgroup$
                                    – TRITICIMAGVS
                                    Feb 4 at 19:43











                                    3












                                    $begingroup$


                                    Japt, 8 bytes



                                    -2 bytes from @Shaggy



                                    mîT Õ¸¸è


                                    Explanation



                                    mîT Õ¸¸è      Full program. Implicit input U
                                    e.g. U = [2,0,2]
                                    mîT Map each item X and repeat T(0) X times
                                    U = ["00","","00"]
                                    Õ Transpose rows with columns
                                    U = ["0 0","0 0"]
                                    ¸¸ Join using space and then split in space
                                    U = ["0","0","0","0"]
                                    è Return the count of the truthy values


                                    Try it online!






                                    share|improve this answer











                                    $endgroup$













                                    • $begingroup$
                                      8 bytes: mîT Õ¸¸è
                                      $endgroup$
                                      – Shaggy
                                      Feb 4 at 12:27






                                    • 1




                                      $begingroup$
                                      Nice use of A.y()'s padding, by the way.
                                      $endgroup$
                                      – Shaggy
                                      Feb 4 at 20:21
















                                    3












                                    $begingroup$


                                    Japt, 8 bytes



                                    -2 bytes from @Shaggy



                                    mîT Õ¸¸è


                                    Explanation



                                    mîT Õ¸¸è      Full program. Implicit input U
                                    e.g. U = [2,0,2]
                                    mîT Map each item X and repeat T(0) X times
                                    U = ["00","","00"]
                                    Õ Transpose rows with columns
                                    U = ["0 0","0 0"]
                                    ¸¸ Join using space and then split in space
                                    U = ["0","0","0","0"]
                                    è Return the count of the truthy values


                                    Try it online!






                                    share|improve this answer











                                    $endgroup$













                                    • $begingroup$
                                      8 bytes: mîT Õ¸¸è
                                      $endgroup$
                                      – Shaggy
                                      Feb 4 at 12:27






                                    • 1




                                      $begingroup$
                                      Nice use of A.y()'s padding, by the way.
                                      $endgroup$
                                      – Shaggy
                                      Feb 4 at 20:21














                                    3












                                    3








                                    3





                                    $begingroup$


                                    Japt, 8 bytes



                                    -2 bytes from @Shaggy



                                    mîT Õ¸¸è


                                    Explanation



                                    mîT Õ¸¸è      Full program. Implicit input U
                                    e.g. U = [2,0,2]
                                    mîT Map each item X and repeat T(0) X times
                                    U = ["00","","00"]
                                    Õ Transpose rows with columns
                                    U = ["0 0","0 0"]
                                    ¸¸ Join using space and then split in space
                                    U = ["0","0","0","0"]
                                    è Return the count of the truthy values


                                    Try it online!






                                    share|improve this answer











                                    $endgroup$




                                    Japt, 8 bytes



                                    -2 bytes from @Shaggy



                                    mîT Õ¸¸è


                                    Explanation



                                    mîT Õ¸¸è      Full program. Implicit input U
                                    e.g. U = [2,0,2]
                                    mîT Map each item X and repeat T(0) X times
                                    U = ["00","","00"]
                                    Õ Transpose rows with columns
                                    U = ["0 0","0 0"]
                                    ¸¸ Join using space and then split in space
                                    U = ["0","0","0","0"]
                                    è Return the count of the truthy values


                                    Try it online!







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Feb 4 at 12:29

























                                    answered Feb 4 at 11:56









                                    Luis felipe De jesus MunozLuis felipe De jesus Munoz

                                    4,60221364




                                    4,60221364












                                    • $begingroup$
                                      8 bytes: mîT Õ¸¸è
                                      $endgroup$
                                      – Shaggy
                                      Feb 4 at 12:27






                                    • 1




                                      $begingroup$
                                      Nice use of A.y()'s padding, by the way.
                                      $endgroup$
                                      – Shaggy
                                      Feb 4 at 20:21


















                                    • $begingroup$
                                      8 bytes: mîT Õ¸¸è
                                      $endgroup$
                                      – Shaggy
                                      Feb 4 at 12:27






                                    • 1




                                      $begingroup$
                                      Nice use of A.y()'s padding, by the way.
                                      $endgroup$
                                      – Shaggy
                                      Feb 4 at 20:21
















                                    $begingroup$
                                    8 bytes: mîT Õ¸¸è
                                    $endgroup$
                                    – Shaggy
                                    Feb 4 at 12:27




                                    $begingroup$
                                    8 bytes: mîT Õ¸¸è
                                    $endgroup$
                                    – Shaggy
                                    Feb 4 at 12:27




                                    1




                                    1




                                    $begingroup$
                                    Nice use of A.y()'s padding, by the way.
                                    $endgroup$
                                    – Shaggy
                                    Feb 4 at 20:21




                                    $begingroup$
                                    Nice use of A.y()'s padding, by the way.
                                    $endgroup$
                                    – Shaggy
                                    Feb 4 at 20:21











                                    3












                                    $begingroup$


                                    MATL, 8 bytes



                                    0whd3Y%s


                                    Try it online!



                                    Pretty much @Arnauld's algorithm. Saved one byte (thanks @LuisMendo) by casting to uint64 rather than selecting ) positive entries.






                                    share|improve this answer











                                    $endgroup$


















                                      3












                                      $begingroup$


                                      MATL, 8 bytes



                                      0whd3Y%s


                                      Try it online!



                                      Pretty much @Arnauld's algorithm. Saved one byte (thanks @LuisMendo) by casting to uint64 rather than selecting ) positive entries.






                                      share|improve this answer











                                      $endgroup$
















                                        3












                                        3








                                        3





                                        $begingroup$


                                        MATL, 8 bytes



                                        0whd3Y%s


                                        Try it online!



                                        Pretty much @Arnauld's algorithm. Saved one byte (thanks @LuisMendo) by casting to uint64 rather than selecting ) positive entries.






                                        share|improve this answer











                                        $endgroup$




                                        MATL, 8 bytes



                                        0whd3Y%s


                                        Try it online!



                                        Pretty much @Arnauld's algorithm. Saved one byte (thanks @LuisMendo) by casting to uint64 rather than selecting ) positive entries.







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Feb 4 at 13:04

























                                        answered Feb 4 at 12:38









                                        SanchisesSanchises

                                        5,88212351




                                        5,88212351























                                            3












                                            $begingroup$


                                            Japt, 7 6 bytes



                                            änT fq


                                            Try it



                                            1 byte saved thanks to Oliver.



                                            änT xwT    :Implicit input of integer array
                                            än :Consecutive differences / Deltas
                                            T : After first prepending 0
                                            f :Filter elements by
                                            q : Square root (The square root of a negative being NaN)
                                            :Implicitly reduce by addition and output





                                            share|improve this answer











                                            $endgroup$









                                            • 1




                                              $begingroup$
                                              6 bytes
                                              $endgroup$
                                              – Oliver
                                              Feb 4 at 18:46










                                            • $begingroup$
                                              Nice one, @Oliver; wouldn't have thought of that.
                                              $endgroup$
                                              – Shaggy
                                              Feb 4 at 20:20
















                                            3












                                            $begingroup$


                                            Japt, 7 6 bytes



                                            änT fq


                                            Try it



                                            1 byte saved thanks to Oliver.



                                            änT xwT    :Implicit input of integer array
                                            än :Consecutive differences / Deltas
                                            T : After first prepending 0
                                            f :Filter elements by
                                            q : Square root (The square root of a negative being NaN)
                                            :Implicitly reduce by addition and output





                                            share|improve this answer











                                            $endgroup$









                                            • 1




                                              $begingroup$
                                              6 bytes
                                              $endgroup$
                                              – Oliver
                                              Feb 4 at 18:46










                                            • $begingroup$
                                              Nice one, @Oliver; wouldn't have thought of that.
                                              $endgroup$
                                              – Shaggy
                                              Feb 4 at 20:20














                                            3












                                            3








                                            3





                                            $begingroup$


                                            Japt, 7 6 bytes



                                            änT fq


                                            Try it



                                            1 byte saved thanks to Oliver.



                                            änT xwT    :Implicit input of integer array
                                            än :Consecutive differences / Deltas
                                            T : After first prepending 0
                                            f :Filter elements by
                                            q : Square root (The square root of a negative being NaN)
                                            :Implicitly reduce by addition and output





                                            share|improve this answer











                                            $endgroup$




                                            Japt, 7 6 bytes



                                            änT fq


                                            Try it



                                            1 byte saved thanks to Oliver.



                                            änT xwT    :Implicit input of integer array
                                            än :Consecutive differences / Deltas
                                            T : After first prepending 0
                                            f :Filter elements by
                                            q : Square root (The square root of a negative being NaN)
                                            :Implicitly reduce by addition and output






                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Feb 4 at 20:20

























                                            answered Feb 4 at 13:03









                                            ShaggyShaggy

                                            19.8k21667




                                            19.8k21667








                                            • 1




                                              $begingroup$
                                              6 bytes
                                              $endgroup$
                                              – Oliver
                                              Feb 4 at 18:46










                                            • $begingroup$
                                              Nice one, @Oliver; wouldn't have thought of that.
                                              $endgroup$
                                              – Shaggy
                                              Feb 4 at 20:20














                                            • 1




                                              $begingroup$
                                              6 bytes
                                              $endgroup$
                                              – Oliver
                                              Feb 4 at 18:46










                                            • $begingroup$
                                              Nice one, @Oliver; wouldn't have thought of that.
                                              $endgroup$
                                              – Shaggy
                                              Feb 4 at 20:20








                                            1




                                            1




                                            $begingroup$
                                            6 bytes
                                            $endgroup$
                                            – Oliver
                                            Feb 4 at 18:46




                                            $begingroup$
                                            6 bytes
                                            $endgroup$
                                            – Oliver
                                            Feb 4 at 18:46












                                            $begingroup$
                                            Nice one, @Oliver; wouldn't have thought of that.
                                            $endgroup$
                                            – Shaggy
                                            Feb 4 at 20:20




                                            $begingroup$
                                            Nice one, @Oliver; wouldn't have thought of that.
                                            $endgroup$
                                            – Shaggy
                                            Feb 4 at 20:20











                                            3












                                            $begingroup$


                                            R, 30 bytes





                                            sum(pmax(0,diff(c(0,scan()))))


                                            Try it online!



                                            Porting of @Arnauld solution which in turn derives from @tsh great solution






                                            share|improve this answer











                                            $endgroup$


















                                              3












                                              $begingroup$


                                              R, 30 bytes





                                              sum(pmax(0,diff(c(0,scan()))))


                                              Try it online!



                                              Porting of @Arnauld solution which in turn derives from @tsh great solution






                                              share|improve this answer











                                              $endgroup$
















                                                3












                                                3








                                                3





                                                $begingroup$


                                                R, 30 bytes





                                                sum(pmax(0,diff(c(0,scan()))))


                                                Try it online!



                                                Porting of @Arnauld solution which in turn derives from @tsh great solution






                                                share|improve this answer











                                                $endgroup$




                                                R, 30 bytes





                                                sum(pmax(0,diff(c(0,scan()))))


                                                Try it online!



                                                Porting of @Arnauld solution which in turn derives from @tsh great solution







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Feb 5 at 7:26

























                                                answered Feb 4 at 12:51









                                                digEmAlldigEmAll

                                                3,149414




                                                3,149414























                                                    2












                                                    $begingroup$


                                                    Retina 0.8.2, 21 bytes



                                                    d+
                                                    $*
                                                    (1+)(?=,1)

                                                    1


                                                    Try it online! Link includes test cases. Explanation:



                                                    d+
                                                    $*


                                                    Convert to unary.



                                                    (1+)(?=,1)


                                                    Delete all overlaps with the next tower, which don't need a new stroke.



                                                    1


                                                    Count the remaining strokes.






                                                    share|improve this answer









                                                    $endgroup$


















                                                      2












                                                      $begingroup$


                                                      Retina 0.8.2, 21 bytes



                                                      d+
                                                      $*
                                                      (1+)(?=,1)

                                                      1


                                                      Try it online! Link includes test cases. Explanation:



                                                      d+
                                                      $*


                                                      Convert to unary.



                                                      (1+)(?=,1)


                                                      Delete all overlaps with the next tower, which don't need a new stroke.



                                                      1


                                                      Count the remaining strokes.






                                                      share|improve this answer









                                                      $endgroup$
















                                                        2












                                                        2








                                                        2





                                                        $begingroup$


                                                        Retina 0.8.2, 21 bytes



                                                        d+
                                                        $*
                                                        (1+)(?=,1)

                                                        1


                                                        Try it online! Link includes test cases. Explanation:



                                                        d+
                                                        $*


                                                        Convert to unary.



                                                        (1+)(?=,1)


                                                        Delete all overlaps with the next tower, which don't need a new stroke.



                                                        1


                                                        Count the remaining strokes.






                                                        share|improve this answer









                                                        $endgroup$




                                                        Retina 0.8.2, 21 bytes



                                                        d+
                                                        $*
                                                        (1+)(?=,1)

                                                        1


                                                        Try it online! Link includes test cases. Explanation:



                                                        d+
                                                        $*


                                                        Convert to unary.



                                                        (1+)(?=,1)


                                                        Delete all overlaps with the next tower, which don't need a new stroke.



                                                        1


                                                        Count the remaining strokes.







                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Feb 4 at 13:04









                                                        NeilNeil

                                                        80.6k744178




                                                        80.6k744178























                                                            2












                                                            $begingroup$


                                                            Jelly, 5 bytes



                                                            A port of my 05AB1E answer, which itself is similar to @tsh JS answer.



                                                            ŻI0»S


                                                            Try it online!



                                                            Commented



                                                            ŻI0»S    - main link, expecting a list of non-negative integers  e.g. [10, 9, 8, 9]
                                                            Ż - prepend 0 --> [0, 10, 9, 8, 9]
                                                            I - compute the deltas --> [10, -1, -1, 1]
                                                            0» - compute max(0, v) for each term v --> [10, 0, 0, 1]
                                                            S - sum --> 11





                                                            share|improve this answer











                                                            $endgroup$


















                                                              2












                                                              $begingroup$


                                                              Jelly, 5 bytes



                                                              A port of my 05AB1E answer, which itself is similar to @tsh JS answer.



                                                              ŻI0»S


                                                              Try it online!



                                                              Commented



                                                              ŻI0»S    - main link, expecting a list of non-negative integers  e.g. [10, 9, 8, 9]
                                                              Ż - prepend 0 --> [0, 10, 9, 8, 9]
                                                              I - compute the deltas --> [10, -1, -1, 1]
                                                              0» - compute max(0, v) for each term v --> [10, 0, 0, 1]
                                                              S - sum --> 11





                                                              share|improve this answer











                                                              $endgroup$
















                                                                2












                                                                2








                                                                2





                                                                $begingroup$


                                                                Jelly, 5 bytes



                                                                A port of my 05AB1E answer, which itself is similar to @tsh JS answer.



                                                                ŻI0»S


                                                                Try it online!



                                                                Commented



                                                                ŻI0»S    - main link, expecting a list of non-negative integers  e.g. [10, 9, 8, 9]
                                                                Ż - prepend 0 --> [0, 10, 9, 8, 9]
                                                                I - compute the deltas --> [10, -1, -1, 1]
                                                                0» - compute max(0, v) for each term v --> [10, 0, 0, 1]
                                                                S - sum --> 11





                                                                share|improve this answer











                                                                $endgroup$




                                                                Jelly, 5 bytes



                                                                A port of my 05AB1E answer, which itself is similar to @tsh JS answer.



                                                                ŻI0»S


                                                                Try it online!



                                                                Commented



                                                                ŻI0»S    - main link, expecting a list of non-negative integers  e.g. [10, 9, 8, 9]
                                                                Ż - prepend 0 --> [0, 10, 9, 8, 9]
                                                                I - compute the deltas --> [10, -1, -1, 1]
                                                                0» - compute max(0, v) for each term v --> [10, 0, 0, 1]
                                                                S - sum --> 11






                                                                share|improve this answer














                                                                share|improve this answer



                                                                share|improve this answer








                                                                edited Feb 4 at 13:22

























                                                                answered Feb 4 at 13:08









                                                                ArnauldArnauld

                                                                75.9k693320




                                                                75.9k693320























                                                                    2












                                                                    $begingroup$

                                                                    Common Lisp, 88 87 bytes



                                                                    (lambda(s)(let((o 0))(dolist(c s)(incf o(max 0 c))(mapl(lambda(y)(decf(car y)c))s))o))


                                                                    non-minified



                                                                    (lambda (skyline)
                                                                    (let ((output 0))
                                                                    (dolist (current-skyscraper-height skyline)
                                                                    (incf output (max 0 current-skyscraper-height))
                                                                    (mapl (lambda (skyscraper)
                                                                    (decf (car skyscraper) current-skyscraper-height))
                                                                    skyline))
                                                                    output)))


                                                                    Test it



                                                                    When one tower is painted, it takes a number of brushstrokes equal to it's height. These brushstrokes translate to all the following ones, which is indicated here by subtracting the current tower's height from all other towers (and itself, but that doesn't matter). If a following tower is shorter, then it will be pushed to a negative number, and this negative number will subsequently subtracted from the towers that follow (indicating brushstrokes that could not be translated from a previous tower to the next ones). It actually just subtracts the number from all tower heights, including previous ones, but this doesn't matter because we don't look at the previous ones again.






                                                                    share|improve this answer










                                                                    New contributor




                                                                    Charlim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                    Check out our Code of Conduct.






                                                                    $endgroup$













                                                                    • $begingroup$
                                                                      Welcome to PPCG. Could you possibly provide a link to an online testing environment for ease of verification?
                                                                      $endgroup$
                                                                      – Jonathan Frech
                                                                      Feb 7 at 0:52










                                                                    • $begingroup$
                                                                      Yes, absolutely. rextester.com/TKBU14782 The answer will be updated shortly
                                                                      $endgroup$
                                                                      – Charlim
                                                                      Feb 7 at 0:55










                                                                    • $begingroup$
                                                                      Well done. +1 for a nice, working first post. Have fun golfing.
                                                                      $endgroup$
                                                                      – Jonathan Frech
                                                                      Feb 7 at 0:58
















                                                                    2












                                                                    $begingroup$

                                                                    Common Lisp, 88 87 bytes



                                                                    (lambda(s)(let((o 0))(dolist(c s)(incf o(max 0 c))(mapl(lambda(y)(decf(car y)c))s))o))


                                                                    non-minified



                                                                    (lambda (skyline)
                                                                    (let ((output 0))
                                                                    (dolist (current-skyscraper-height skyline)
                                                                    (incf output (max 0 current-skyscraper-height))
                                                                    (mapl (lambda (skyscraper)
                                                                    (decf (car skyscraper) current-skyscraper-height))
                                                                    skyline))
                                                                    output)))


                                                                    Test it



                                                                    When one tower is painted, it takes a number of brushstrokes equal to it's height. These brushstrokes translate to all the following ones, which is indicated here by subtracting the current tower's height from all other towers (and itself, but that doesn't matter). If a following tower is shorter, then it will be pushed to a negative number, and this negative number will subsequently subtracted from the towers that follow (indicating brushstrokes that could not be translated from a previous tower to the next ones). It actually just subtracts the number from all tower heights, including previous ones, but this doesn't matter because we don't look at the previous ones again.






                                                                    share|improve this answer










                                                                    New contributor




                                                                    Charlim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                    Check out our Code of Conduct.






                                                                    $endgroup$













                                                                    • $begingroup$
                                                                      Welcome to PPCG. Could you possibly provide a link to an online testing environment for ease of verification?
                                                                      $endgroup$
                                                                      – Jonathan Frech
                                                                      Feb 7 at 0:52










                                                                    • $begingroup$
                                                                      Yes, absolutely. rextester.com/TKBU14782 The answer will be updated shortly
                                                                      $endgroup$
                                                                      – Charlim
                                                                      Feb 7 at 0:55










                                                                    • $begingroup$
                                                                      Well done. +1 for a nice, working first post. Have fun golfing.
                                                                      $endgroup$
                                                                      – Jonathan Frech
                                                                      Feb 7 at 0:58














                                                                    2












                                                                    2








                                                                    2





                                                                    $begingroup$

                                                                    Common Lisp, 88 87 bytes



                                                                    (lambda(s)(let((o 0))(dolist(c s)(incf o(max 0 c))(mapl(lambda(y)(decf(car y)c))s))o))


                                                                    non-minified



                                                                    (lambda (skyline)
                                                                    (let ((output 0))
                                                                    (dolist (current-skyscraper-height skyline)
                                                                    (incf output (max 0 current-skyscraper-height))
                                                                    (mapl (lambda (skyscraper)
                                                                    (decf (car skyscraper) current-skyscraper-height))
                                                                    skyline))
                                                                    output)))


                                                                    Test it



                                                                    When one tower is painted, it takes a number of brushstrokes equal to it's height. These brushstrokes translate to all the following ones, which is indicated here by subtracting the current tower's height from all other towers (and itself, but that doesn't matter). If a following tower is shorter, then it will be pushed to a negative number, and this negative number will subsequently subtracted from the towers that follow (indicating brushstrokes that could not be translated from a previous tower to the next ones). It actually just subtracts the number from all tower heights, including previous ones, but this doesn't matter because we don't look at the previous ones again.






                                                                    share|improve this answer










                                                                    New contributor




                                                                    Charlim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                    Check out our Code of Conduct.






                                                                    $endgroup$



                                                                    Common Lisp, 88 87 bytes



                                                                    (lambda(s)(let((o 0))(dolist(c s)(incf o(max 0 c))(mapl(lambda(y)(decf(car y)c))s))o))


                                                                    non-minified



                                                                    (lambda (skyline)
                                                                    (let ((output 0))
                                                                    (dolist (current-skyscraper-height skyline)
                                                                    (incf output (max 0 current-skyscraper-height))
                                                                    (mapl (lambda (skyscraper)
                                                                    (decf (car skyscraper) current-skyscraper-height))
                                                                    skyline))
                                                                    output)))


                                                                    Test it



                                                                    When one tower is painted, it takes a number of brushstrokes equal to it's height. These brushstrokes translate to all the following ones, which is indicated here by subtracting the current tower's height from all other towers (and itself, but that doesn't matter). If a following tower is shorter, then it will be pushed to a negative number, and this negative number will subsequently subtracted from the towers that follow (indicating brushstrokes that could not be translated from a previous tower to the next ones). It actually just subtracts the number from all tower heights, including previous ones, but this doesn't matter because we don't look at the previous ones again.







                                                                    share|improve this answer










                                                                    New contributor




                                                                    Charlim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                    Check out our Code of Conduct.









                                                                    share|improve this answer



                                                                    share|improve this answer








                                                                    edited 9 hours ago





















                                                                    New contributor




                                                                    Charlim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                    Check out our Code of Conduct.









                                                                    answered Feb 7 at 0:27









                                                                    CharlimCharlim

                                                                    813




                                                                    813




                                                                    New contributor




                                                                    Charlim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                    Check out our Code of Conduct.





                                                                    New contributor





                                                                    Charlim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                    Check out our Code of Conduct.






                                                                    Charlim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                    Check out our Code of Conduct.












                                                                    • $begingroup$
                                                                      Welcome to PPCG. Could you possibly provide a link to an online testing environment for ease of verification?
                                                                      $endgroup$
                                                                      – Jonathan Frech
                                                                      Feb 7 at 0:52










                                                                    • $begingroup$
                                                                      Yes, absolutely. rextester.com/TKBU14782 The answer will be updated shortly
                                                                      $endgroup$
                                                                      – Charlim
                                                                      Feb 7 at 0:55










                                                                    • $begingroup$
                                                                      Well done. +1 for a nice, working first post. Have fun golfing.
                                                                      $endgroup$
                                                                      – Jonathan Frech
                                                                      Feb 7 at 0:58


















                                                                    • $begingroup$
                                                                      Welcome to PPCG. Could you possibly provide a link to an online testing environment for ease of verification?
                                                                      $endgroup$
                                                                      – Jonathan Frech
                                                                      Feb 7 at 0:52










                                                                    • $begingroup$
                                                                      Yes, absolutely. rextester.com/TKBU14782 The answer will be updated shortly
                                                                      $endgroup$
                                                                      – Charlim
                                                                      Feb 7 at 0:55










                                                                    • $begingroup$
                                                                      Well done. +1 for a nice, working first post. Have fun golfing.
                                                                      $endgroup$
                                                                      – Jonathan Frech
                                                                      Feb 7 at 0:58
















                                                                    $begingroup$
                                                                    Welcome to PPCG. Could you possibly provide a link to an online testing environment for ease of verification?
                                                                    $endgroup$
                                                                    – Jonathan Frech
                                                                    Feb 7 at 0:52




                                                                    $begingroup$
                                                                    Welcome to PPCG. Could you possibly provide a link to an online testing environment for ease of verification?
                                                                    $endgroup$
                                                                    – Jonathan Frech
                                                                    Feb 7 at 0:52












                                                                    $begingroup$
                                                                    Yes, absolutely. rextester.com/TKBU14782 The answer will be updated shortly
                                                                    $endgroup$
                                                                    – Charlim
                                                                    Feb 7 at 0:55




                                                                    $begingroup$
                                                                    Yes, absolutely. rextester.com/TKBU14782 The answer will be updated shortly
                                                                    $endgroup$
                                                                    – Charlim
                                                                    Feb 7 at 0:55












                                                                    $begingroup$
                                                                    Well done. +1 for a nice, working first post. Have fun golfing.
                                                                    $endgroup$
                                                                    – Jonathan Frech
                                                                    Feb 7 at 0:58




                                                                    $begingroup$
                                                                    Well done. +1 for a nice, working first post. Have fun golfing.
                                                                    $endgroup$
                                                                    – Jonathan Frech
                                                                    Feb 7 at 0:58











                                                                    1












                                                                    $begingroup$


                                                                    05AB1E, 13 10 bytes



                                                                    Z>Lε@γPO}O


                                                                    Try it online or verify all test cases.



                                                                    Explanation:





                                                                    Z            # Get the maximum of the (implicit) input-list
                                                                    > # Increase it by 1 (if the list only contains 0s)
                                                                    L # Create a list in the range [1, max]
                                                                    ε # Map each value to:
                                                                    @ # Check if this value is >= for each value in the (implicit) input
                                                                    γ # Split into chunks of adjacent equal digits
                                                                    P # Take the product of each inner list
                                                                    O # Take the sum
                                                                    }O # And after the map: take the sum (which is output implicitly)





                                                                    share|improve this answer











                                                                    $endgroup$


















                                                                      1












                                                                      $begingroup$


                                                                      05AB1E, 13 10 bytes



                                                                      Z>Lε@γPO}O


                                                                      Try it online or verify all test cases.



                                                                      Explanation:





                                                                      Z            # Get the maximum of the (implicit) input-list
                                                                      > # Increase it by 1 (if the list only contains 0s)
                                                                      L # Create a list in the range [1, max]
                                                                      ε # Map each value to:
                                                                      @ # Check if this value is >= for each value in the (implicit) input
                                                                      γ # Split into chunks of adjacent equal digits
                                                                      P # Take the product of each inner list
                                                                      O # Take the sum
                                                                      }O # And after the map: take the sum (which is output implicitly)





                                                                      share|improve this answer











                                                                      $endgroup$
















                                                                        1












                                                                        1








                                                                        1





                                                                        $begingroup$


                                                                        05AB1E, 13 10 bytes



                                                                        Z>Lε@γPO}O


                                                                        Try it online or verify all test cases.



                                                                        Explanation:





                                                                        Z            # Get the maximum of the (implicit) input-list
                                                                        > # Increase it by 1 (if the list only contains 0s)
                                                                        L # Create a list in the range [1, max]
                                                                        ε # Map each value to:
                                                                        @ # Check if this value is >= for each value in the (implicit) input
                                                                        γ # Split into chunks of adjacent equal digits
                                                                        P # Take the product of each inner list
                                                                        O # Take the sum
                                                                        }O # And after the map: take the sum (which is output implicitly)





                                                                        share|improve this answer











                                                                        $endgroup$




                                                                        05AB1E, 13 10 bytes



                                                                        Z>Lε@γPO}O


                                                                        Try it online or verify all test cases.



                                                                        Explanation:





                                                                        Z            # Get the maximum of the (implicit) input-list
                                                                        > # Increase it by 1 (if the list only contains 0s)
                                                                        L # Create a list in the range [1, max]
                                                                        ε # Map each value to:
                                                                        @ # Check if this value is >= for each value in the (implicit) input
                                                                        γ # Split into chunks of adjacent equal digits
                                                                        P # Take the product of each inner list
                                                                        O # Take the sum
                                                                        }O # And after the map: take the sum (which is output implicitly)






                                                                        share|improve this answer














                                                                        share|improve this answer



                                                                        share|improve this answer








                                                                        edited Feb 4 at 12:28

























                                                                        answered Feb 4 at 11:58









                                                                        Kevin CruijssenKevin Cruijssen

                                                                        37.9k557195




                                                                        37.9k557195























                                                                            1












                                                                            $begingroup$


                                                                            C# (Visual C# Interactive Compiler) with flag /u:System.Math, 47 bytes





                                                                            n=>n.Select((a,i)=>i<1?a:Max(a-n[i-1],0)).Sum()


                                                                            Try it online!



                                                                            Old version, with flag /u:System.Math, 63 bytes





                                                                            n=>n.Aggregate((0,0),(a,b)=>(a.Item1+Max(0,b-a.Item2),b)).Item1


                                                                            I feel like this solution is more elegant than the first one. It goes through the array with a two-value tuple as a starting value, picking up values, and stores the value before it in the second part of the tuple.



                                                                            Try it online!






                                                                            share|improve this answer











                                                                            $endgroup$


















                                                                              1












                                                                              $begingroup$


                                                                              C# (Visual C# Interactive Compiler) with flag /u:System.Math, 47 bytes





                                                                              n=>n.Select((a,i)=>i<1?a:Max(a-n[i-1],0)).Sum()


                                                                              Try it online!



                                                                              Old version, with flag /u:System.Math, 63 bytes





                                                                              n=>n.Aggregate((0,0),(a,b)=>(a.Item1+Max(0,b-a.Item2),b)).Item1


                                                                              I feel like this solution is more elegant than the first one. It goes through the array with a two-value tuple as a starting value, picking up values, and stores the value before it in the second part of the tuple.



                                                                              Try it online!






                                                                              share|improve this answer











                                                                              $endgroup$
















                                                                                1












                                                                                1








                                                                                1





                                                                                $begingroup$


                                                                                C# (Visual C# Interactive Compiler) with flag /u:System.Math, 47 bytes





                                                                                n=>n.Select((a,i)=>i<1?a:Max(a-n[i-1],0)).Sum()


                                                                                Try it online!



                                                                                Old version, with flag /u:System.Math, 63 bytes





                                                                                n=>n.Aggregate((0,0),(a,b)=>(a.Item1+Max(0,b-a.Item2),b)).Item1


                                                                                I feel like this solution is more elegant than the first one. It goes through the array with a two-value tuple as a starting value, picking up values, and stores the value before it in the second part of the tuple.



                                                                                Try it online!






                                                                                share|improve this answer











                                                                                $endgroup$




                                                                                C# (Visual C# Interactive Compiler) with flag /u:System.Math, 47 bytes





                                                                                n=>n.Select((a,i)=>i<1?a:Max(a-n[i-1],0)).Sum()


                                                                                Try it online!



                                                                                Old version, with flag /u:System.Math, 63 bytes





                                                                                n=>n.Aggregate((0,0),(a,b)=>(a.Item1+Max(0,b-a.Item2),b)).Item1


                                                                                I feel like this solution is more elegant than the first one. It goes through the array with a two-value tuple as a starting value, picking up values, and stores the value before it in the second part of the tuple.



                                                                                Try it online!







                                                                                share|improve this answer














                                                                                share|improve this answer



                                                                                share|improve this answer








                                                                                edited Feb 5 at 5:11

























                                                                                answered Feb 4 at 19:59









                                                                                Embodiment of IgnoranceEmbodiment of Ignorance

                                                                                920119




                                                                                920119























                                                                                    1












                                                                                    $begingroup$

                                                                                    Pyth, 8 bytes



                                                                                    s>#0.++0


                                                                                    Yet another port of @tsh's marvellous answer. Takes the sum (s) of the positive values (>#0) of the deltas (.+) of the input with 0 prepended (+0Q, trailing Q inferred).



                                                                                    Try it online here, or verify all the test cases at once here.



                                                                                    String joining method, 10 bytes



                                                                                    This was the solution I wrote before browsing the other answers.



                                                                                    lcj.t+d*LN


                                                                                    Test suite.



                                                                                    lcj.t+d*LNQ   Implicit: Q=eval(input()), b=<newline>, N=<quote mark>
                                                                                    Trailing Q inferred
                                                                                    L Q Map each element of Q...
                                                                                    * N ... to N repeated that many times
                                                                                    +b Prepend a newline
                                                                                    .t Transpose, padding with spaces
                                                                                    j Join on newlines
                                                                                    c Split on whitespace
                                                                                    l Take the length, implicit print





                                                                                    share|improve this answer









                                                                                    $endgroup$


















                                                                                      1












                                                                                      $begingroup$

                                                                                      Pyth, 8 bytes



                                                                                      s>#0.++0


                                                                                      Yet another port of @tsh's marvellous answer. Takes the sum (s) of the positive values (>#0) of the deltas (.+) of the input with 0 prepended (+0Q, trailing Q inferred).



                                                                                      Try it online here, or verify all the test cases at once here.



                                                                                      String joining method, 10 bytes



                                                                                      This was the solution I wrote before browsing the other answers.



                                                                                      lcj.t+d*LN


                                                                                      Test suite.



                                                                                      lcj.t+d*LNQ   Implicit: Q=eval(input()), b=<newline>, N=<quote mark>
                                                                                      Trailing Q inferred
                                                                                      L Q Map each element of Q...
                                                                                      * N ... to N repeated that many times
                                                                                      +b Prepend a newline
                                                                                      .t Transpose, padding with spaces
                                                                                      j Join on newlines
                                                                                      c Split on whitespace
                                                                                      l Take the length, implicit print





                                                                                      share|improve this answer









                                                                                      $endgroup$
















                                                                                        1












                                                                                        1








                                                                                        1





                                                                                        $begingroup$

                                                                                        Pyth, 8 bytes



                                                                                        s>#0.++0


                                                                                        Yet another port of @tsh's marvellous answer. Takes the sum (s) of the positive values (>#0) of the deltas (.+) of the input with 0 prepended (+0Q, trailing Q inferred).



                                                                                        Try it online here, or verify all the test cases at once here.



                                                                                        String joining method, 10 bytes



                                                                                        This was the solution I wrote before browsing the other answers.



                                                                                        lcj.t+d*LN


                                                                                        Test suite.



                                                                                        lcj.t+d*LNQ   Implicit: Q=eval(input()), b=<newline>, N=<quote mark>
                                                                                        Trailing Q inferred
                                                                                        L Q Map each element of Q...
                                                                                        * N ... to N repeated that many times
                                                                                        +b Prepend a newline
                                                                                        .t Transpose, padding with spaces
                                                                                        j Join on newlines
                                                                                        c Split on whitespace
                                                                                        l Take the length, implicit print





                                                                                        share|improve this answer









                                                                                        $endgroup$



                                                                                        Pyth, 8 bytes



                                                                                        s>#0.++0


                                                                                        Yet another port of @tsh's marvellous answer. Takes the sum (s) of the positive values (>#0) of the deltas (.+) of the input with 0 prepended (+0Q, trailing Q inferred).



                                                                                        Try it online here, or verify all the test cases at once here.



                                                                                        String joining method, 10 bytes



                                                                                        This was the solution I wrote before browsing the other answers.



                                                                                        lcj.t+d*LN


                                                                                        Test suite.



                                                                                        lcj.t+d*LNQ   Implicit: Q=eval(input()), b=<newline>, N=<quote mark>
                                                                                        Trailing Q inferred
                                                                                        L Q Map each element of Q...
                                                                                        * N ... to N repeated that many times
                                                                                        +b Prepend a newline
                                                                                        .t Transpose, padding with spaces
                                                                                        j Join on newlines
                                                                                        c Split on whitespace
                                                                                        l Take the length, implicit print






                                                                                        share|improve this answer












                                                                                        share|improve this answer



                                                                                        share|improve this answer










                                                                                        answered Feb 5 at 11:05









                                                                                        SokSok

                                                                                        3,987925




                                                                                        3,987925























                                                                                            1












                                                                                            $begingroup$

                                                                                            Clojure, 50 bytes



                                                                                            #((reduce(fn[[s n]i][(+(max(- i n)0)s)i])[0 0]%)0)


                                                                                            Try it online! (Why doesn't this print anything?)



                                                                                            #( ; begin anonymous function
                                                                                            (reduce
                                                                                            (fn [[s n] i] ; internal anonymous reducing function, destructures accumulator argument into a sum and the previous item
                                                                                            [(+ (max (- i n) 0) s ; the sum part of the accumulator becomes the previous sum plus the larger of zero and the difference between the current number and the last one, which is how many new strokes need to be started at this point
                                                                                            i]) ; ...and the previous item part becomes the current item
                                                                                            [0 0] ; the initial value of the accumulator gives no strokes yet, and nothing for them to cover yet
                                                                                            %) ; reduce over the argument to the function
                                                                                            0) ; and get the sum element of the last value of the accumulator.





                                                                                            share|improve this answer








                                                                                            New contributor




                                                                                            Tried Creating an Account is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                            Check out our Code of Conduct.






                                                                                            $endgroup$













                                                                                            • $begingroup$
                                                                                              Welcome to PPCG! I don't know anything about Clojure, but a quick search shows that you'll need to evaluate the lazy for loop. Try it Online! (Tip: you can use the link button to auto-format your answer). Hope you stick around and have some fun!
                                                                                              $endgroup$
                                                                                              – Jo King
                                                                                              Feb 6 at 8:47


















                                                                                            1












                                                                                            $begingroup$

                                                                                            Clojure, 50 bytes



                                                                                            #((reduce(fn[[s n]i][(+(max(- i n)0)s)i])[0 0]%)0)


                                                                                            Try it online! (Why doesn't this print anything?)



                                                                                            #( ; begin anonymous function
                                                                                            (reduce
                                                                                            (fn [[s n] i] ; internal anonymous reducing function, destructures accumulator argument into a sum and the previous item
                                                                                            [(+ (max (- i n) 0) s ; the sum part of the accumulator becomes the previous sum plus the larger of zero and the difference between the current number and the last one, which is how many new strokes need to be started at this point
                                                                                            i]) ; ...and the previous item part becomes the current item
                                                                                            [0 0] ; the initial value of the accumulator gives no strokes yet, and nothing for them to cover yet
                                                                                            %) ; reduce over the argument to the function
                                                                                            0) ; and get the sum element of the last value of the accumulator.





                                                                                            share|improve this answer








                                                                                            New contributor




                                                                                            Tried Creating an Account is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                            Check out our Code of Conduct.






                                                                                            $endgroup$













                                                                                            • $begingroup$
                                                                                              Welcome to PPCG! I don't know anything about Clojure, but a quick search shows that you'll need to evaluate the lazy for loop. Try it Online! (Tip: you can use the link button to auto-format your answer). Hope you stick around and have some fun!
                                                                                              $endgroup$
                                                                                              – Jo King
                                                                                              Feb 6 at 8:47
















                                                                                            1












                                                                                            1








                                                                                            1





                                                                                            $begingroup$

                                                                                            Clojure, 50 bytes



                                                                                            #((reduce(fn[[s n]i][(+(max(- i n)0)s)i])[0 0]%)0)


                                                                                            Try it online! (Why doesn't this print anything?)



                                                                                            #( ; begin anonymous function
                                                                                            (reduce
                                                                                            (fn [[s n] i] ; internal anonymous reducing function, destructures accumulator argument into a sum and the previous item
                                                                                            [(+ (max (- i n) 0) s ; the sum part of the accumulator becomes the previous sum plus the larger of zero and the difference between the current number and the last one, which is how many new strokes need to be started at this point
                                                                                            i]) ; ...and the previous item part becomes the current item
                                                                                            [0 0] ; the initial value of the accumulator gives no strokes yet, and nothing for them to cover yet
                                                                                            %) ; reduce over the argument to the function
                                                                                            0) ; and get the sum element of the last value of the accumulator.





                                                                                            share|improve this answer








                                                                                            New contributor




                                                                                            Tried Creating an Account is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                            Check out our Code of Conduct.






                                                                                            $endgroup$



                                                                                            Clojure, 50 bytes



                                                                                            #((reduce(fn[[s n]i][(+(max(- i n)0)s)i])[0 0]%)0)


                                                                                            Try it online! (Why doesn't this print anything?)



                                                                                            #( ; begin anonymous function
                                                                                            (reduce
                                                                                            (fn [[s n] i] ; internal anonymous reducing function, destructures accumulator argument into a sum and the previous item
                                                                                            [(+ (max (- i n) 0) s ; the sum part of the accumulator becomes the previous sum plus the larger of zero and the difference between the current number and the last one, which is how many new strokes need to be started at this point
                                                                                            i]) ; ...and the previous item part becomes the current item
                                                                                            [0 0] ; the initial value of the accumulator gives no strokes yet, and nothing for them to cover yet
                                                                                            %) ; reduce over the argument to the function
                                                                                            0) ; and get the sum element of the last value of the accumulator.






                                                                                            share|improve this answer








                                                                                            New contributor




                                                                                            Tried Creating an Account is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                            Check out our Code of Conduct.









                                                                                            share|improve this answer



                                                                                            share|improve this answer






                                                                                            New contributor




                                                                                            Tried Creating an Account is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                            Check out our Code of Conduct.









                                                                                            answered Feb 6 at 6:22









                                                                                            Tried Creating an AccountTried Creating an Account

                                                                                            111




                                                                                            111




                                                                                            New contributor




                                                                                            Tried Creating an Account is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                            Check out our Code of Conduct.





                                                                                            New contributor





                                                                                            Tried Creating an Account is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                            Check out our Code of Conduct.






                                                                                            Tried Creating an Account is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                                            Check out our Code of Conduct.












                                                                                            • $begingroup$
                                                                                              Welcome to PPCG! I don't know anything about Clojure, but a quick search shows that you'll need to evaluate the lazy for loop. Try it Online! (Tip: you can use the link button to auto-format your answer). Hope you stick around and have some fun!
                                                                                              $endgroup$
                                                                                              – Jo King
                                                                                              Feb 6 at 8:47




















                                                                                            • $begingroup$
                                                                                              Welcome to PPCG! I don't know anything about Clojure, but a quick search shows that you'll need to evaluate the lazy for loop. Try it Online! (Tip: you can use the link button to auto-format your answer). Hope you stick around and have some fun!
                                                                                              $endgroup$
                                                                                              – Jo King
                                                                                              Feb 6 at 8:47


















                                                                                            $begingroup$
                                                                                            Welcome to PPCG! I don't know anything about Clojure, but a quick search shows that you'll need to evaluate the lazy for loop. Try it Online! (Tip: you can use the link button to auto-format your answer). Hope you stick around and have some fun!
                                                                                            $endgroup$
                                                                                            – Jo King
                                                                                            Feb 6 at 8:47






                                                                                            $begingroup$
                                                                                            Welcome to PPCG! I don't know anything about Clojure, but a quick search shows that you'll need to evaluate the lazy for loop. Try it Online! (Tip: you can use the link button to auto-format your answer). Hope you stick around and have some fun!
                                                                                            $endgroup$
                                                                                            – Jo King
                                                                                            Feb 6 at 8:47













                                                                                            1












                                                                                            $begingroup$


                                                                                            Java (JDK), 57 bytes





                                                                                            a->{int s=0,p=0;for(int x:a)s-=(x>p?p:x)-(p=x);return s;}


                                                                                            Try it online!



                                                                                            Another port of tsh's Javascript answer. So make sure you've upvoted them.



                                                                                            Note that I used subtraction instead of addition because it allowed me to write (p=x) as right operand in the same statement.






                                                                                            share|improve this answer











                                                                                            $endgroup$


















                                                                                              1












                                                                                              $begingroup$


                                                                                              Java (JDK), 57 bytes





                                                                                              a->{int s=0,p=0;for(int x:a)s-=(x>p?p:x)-(p=x);return s;}


                                                                                              Try it online!



                                                                                              Another port of tsh's Javascript answer. So make sure you've upvoted them.



                                                                                              Note that I used subtraction instead of addition because it allowed me to write (p=x) as right operand in the same statement.






                                                                                              share|improve this answer











                                                                                              $endgroup$
















                                                                                                1












                                                                                                1








                                                                                                1





                                                                                                $begingroup$


                                                                                                Java (JDK), 57 bytes





                                                                                                a->{int s=0,p=0;for(int x:a)s-=(x>p?p:x)-(p=x);return s;}


                                                                                                Try it online!



                                                                                                Another port of tsh's Javascript answer. So make sure you've upvoted them.



                                                                                                Note that I used subtraction instead of addition because it allowed me to write (p=x) as right operand in the same statement.






                                                                                                share|improve this answer











                                                                                                $endgroup$




                                                                                                Java (JDK), 57 bytes





                                                                                                a->{int s=0,p=0;for(int x:a)s-=(x>p?p:x)-(p=x);return s;}


                                                                                                Try it online!



                                                                                                Another port of tsh's Javascript answer. So make sure you've upvoted them.



                                                                                                Note that I used subtraction instead of addition because it allowed me to write (p=x) as right operand in the same statement.







                                                                                                share|improve this answer














                                                                                                share|improve this answer



                                                                                                share|improve this answer








                                                                                                edited Feb 6 at 8:44

























                                                                                                answered Feb 6 at 8:18









                                                                                                Olivier GrégoireOlivier Grégoire

                                                                                                8,96511943




                                                                                                8,96511943























                                                                                                    0












                                                                                                    $begingroup$


                                                                                                    MATL, 15 14 13 bytes



                                                                                                    ts:<~"@Y'x]vs


                                                                                                    Input is a column vector, using ; as separator.



                                                                                                    Try it online! Or verify all test cases.



                                                                                                    Explanation



                                                                                                    t       % Implicit input: column vector. Duplicate
                                                                                                    s % Sum
                                                                                                    : % Range from 1 to that. Gives a row vector
                                                                                                    <~ % Greater or equal? Element-wise with broadcast
                                                                                                    " % For each column
                                                                                                    @ % Push current columnn
                                                                                                    Y' % Run-length encoding. Gives vector of values (0, 1) and vector of lengths
                                                                                                    x % Delete vector of lengths
                                                                                                    ] % End
                                                                                                    v % Vertically concatenate. May give an empty array
                                                                                                    s % Sum. Implicit display





                                                                                                    share|improve this answer











                                                                                                    $endgroup$


















                                                                                                      0












                                                                                                      $begingroup$


                                                                                                      MATL, 15 14 13 bytes



                                                                                                      ts:<~"@Y'x]vs


                                                                                                      Input is a column vector, using ; as separator.



                                                                                                      Try it online! Or verify all test cases.



                                                                                                      Explanation



                                                                                                      t       % Implicit input: column vector. Duplicate
                                                                                                      s % Sum
                                                                                                      : % Range from 1 to that. Gives a row vector
                                                                                                      <~ % Greater or equal? Element-wise with broadcast
                                                                                                      " % For each column
                                                                                                      @ % Push current columnn
                                                                                                      Y' % Run-length encoding. Gives vector of values (0, 1) and vector of lengths
                                                                                                      x % Delete vector of lengths
                                                                                                      ] % End
                                                                                                      v % Vertically concatenate. May give an empty array
                                                                                                      s % Sum. Implicit display





                                                                                                      share|improve this answer











                                                                                                      $endgroup$
















                                                                                                        0












                                                                                                        0








                                                                                                        0





                                                                                                        $begingroup$


                                                                                                        MATL, 15 14 13 bytes



                                                                                                        ts:<~"@Y'x]vs


                                                                                                        Input is a column vector, using ; as separator.



                                                                                                        Try it online! Or verify all test cases.



                                                                                                        Explanation



                                                                                                        t       % Implicit input: column vector. Duplicate
                                                                                                        s % Sum
                                                                                                        : % Range from 1 to that. Gives a row vector
                                                                                                        <~ % Greater or equal? Element-wise with broadcast
                                                                                                        " % For each column
                                                                                                        @ % Push current columnn
                                                                                                        Y' % Run-length encoding. Gives vector of values (0, 1) and vector of lengths
                                                                                                        x % Delete vector of lengths
                                                                                                        ] % End
                                                                                                        v % Vertically concatenate. May give an empty array
                                                                                                        s % Sum. Implicit display





                                                                                                        share|improve this answer











                                                                                                        $endgroup$




                                                                                                        MATL, 15 14 13 bytes



                                                                                                        ts:<~"@Y'x]vs


                                                                                                        Input is a column vector, using ; as separator.



                                                                                                        Try it online! Or verify all test cases.



                                                                                                        Explanation



                                                                                                        t       % Implicit input: column vector. Duplicate
                                                                                                        s % Sum
                                                                                                        : % Range from 1 to that. Gives a row vector
                                                                                                        <~ % Greater or equal? Element-wise with broadcast
                                                                                                        " % For each column
                                                                                                        @ % Push current columnn
                                                                                                        Y' % Run-length encoding. Gives vector of values (0, 1) and vector of lengths
                                                                                                        x % Delete vector of lengths
                                                                                                        ] % End
                                                                                                        v % Vertically concatenate. May give an empty array
                                                                                                        s % Sum. Implicit display






                                                                                                        share|improve this answer














                                                                                                        share|improve this answer



                                                                                                        share|improve this answer








                                                                                                        edited Feb 4 at 12:12

























                                                                                                        answered Feb 4 at 11:56









                                                                                                        Luis MendoLuis Mendo

                                                                                                        74.4k887291




                                                                                                        74.4k887291























                                                                                                            0












                                                                                                            $begingroup$

                                                                                                            Perl 5, 21 bytes



                                                                                                            $+=$_>$'&&$_-$';//}{


                                                                                                            TIO



                                                                                                            How





                                                                                                            • -p + }{ + $ trick


                                                                                                            • // matches empty string so that for next line postmatch $' will contain previous line


                                                                                                            • $+=$_>$'&&$_-$' to accumulate difference between current line and previous if current is greater than previous, (could also be written $+=$_-$' if$_>$', but perl doesn't parse $+=$_-$'if$_>$' the same)






                                                                                                            share|improve this answer











                                                                                                            $endgroup$


















                                                                                                              0












                                                                                                              $begingroup$

                                                                                                              Perl 5, 21 bytes



                                                                                                              $+=$_>$'&&$_-$';//}{


                                                                                                              TIO



                                                                                                              How





                                                                                                              • -p + }{ + $ trick


                                                                                                              • // matches empty string so that for next line postmatch $' will contain previous line


                                                                                                              • $+=$_>$'&&$_-$' to accumulate difference between current line and previous if current is greater than previous, (could also be written $+=$_-$' if$_>$', but perl doesn't parse $+=$_-$'if$_>$' the same)






                                                                                                              share|improve this answer











                                                                                                              $endgroup$
















                                                                                                                0












                                                                                                                0








                                                                                                                0





                                                                                                                $begingroup$

                                                                                                                Perl 5, 21 bytes



                                                                                                                $+=$_>$'&&$_-$';//}{


                                                                                                                TIO



                                                                                                                How





                                                                                                                • -p + }{ + $ trick


                                                                                                                • // matches empty string so that for next line postmatch $' will contain previous line


                                                                                                                • $+=$_>$'&&$_-$' to accumulate difference between current line and previous if current is greater than previous, (could also be written $+=$_-$' if$_>$', but perl doesn't parse $+=$_-$'if$_>$' the same)






                                                                                                                share|improve this answer











                                                                                                                $endgroup$



                                                                                                                Perl 5, 21 bytes



                                                                                                                $+=$_>$'&&$_-$';//}{


                                                                                                                TIO



                                                                                                                How





                                                                                                                • -p + }{ + $ trick


                                                                                                                • // matches empty string so that for next line postmatch $' will contain previous line


                                                                                                                • $+=$_>$'&&$_-$' to accumulate difference between current line and previous if current is greater than previous, (could also be written $+=$_-$' if$_>$', but perl doesn't parse $+=$_-$'if$_>$' the same)







                                                                                                                share|improve this answer














                                                                                                                share|improve this answer



                                                                                                                share|improve this answer








                                                                                                                edited Feb 4 at 17:38

























                                                                                                                answered Feb 4 at 14:24









                                                                                                                Nahuel FouilleulNahuel Fouilleul

                                                                                                                2,27029




                                                                                                                2,27029























                                                                                                                    0












                                                                                                                    $begingroup$


                                                                                                                    Stax, 8 bytes



                                                                                                                    Φ┐Γ╟φφ.╨


                                                                                                                    Run and debug it



                                                                                                                    Uses the widely used approach from tsh's JavaScript solution.






                                                                                                                    share|improve this answer









                                                                                                                    $endgroup$


















                                                                                                                      0












                                                                                                                      $begingroup$


                                                                                                                      Stax, 8 bytes



                                                                                                                      Φ┐Γ╟φφ.╨


                                                                                                                      Run and debug it



                                                                                                                      Uses the widely used approach from tsh's JavaScript solution.






                                                                                                                      share|improve this answer









                                                                                                                      $endgroup$
















                                                                                                                        0












                                                                                                                        0








                                                                                                                        0





                                                                                                                        $begingroup$


                                                                                                                        Stax, 8 bytes



                                                                                                                        Φ┐Γ╟φφ.╨


                                                                                                                        Run and debug it



                                                                                                                        Uses the widely used approach from tsh's JavaScript solution.






                                                                                                                        share|improve this answer









                                                                                                                        $endgroup$




                                                                                                                        Stax, 8 bytes



                                                                                                                        Φ┐Γ╟φφ.╨


                                                                                                                        Run and debug it



                                                                                                                        Uses the widely used approach from tsh's JavaScript solution.







                                                                                                                        share|improve this answer












                                                                                                                        share|improve this answer



                                                                                                                        share|improve this answer










                                                                                                                        answered Feb 5 at 19:47









                                                                                                                        wastlwastl

                                                                                                                        2,119525




                                                                                                                        2,119525























                                                                                                                            0












                                                                                                                            $begingroup$


                                                                                                                            Wolfram Language (Mathematica), 34 bytes



                                                                                                                            A port of @Arnauld's solution.



                                                                                                                            Tr@Select[{##,0}-{0,##},#>0&]&@@#&


                                                                                                                            Try it online!






                                                                                                                            share|improve this answer









                                                                                                                            $endgroup$


















                                                                                                                              0












                                                                                                                              $begingroup$


                                                                                                                              Wolfram Language (Mathematica), 34 bytes



                                                                                                                              A port of @Arnauld's solution.



                                                                                                                              Tr@Select[{##,0}-{0,##},#>0&]&@@#&


                                                                                                                              Try it online!






                                                                                                                              share|improve this answer









                                                                                                                              $endgroup$
















                                                                                                                                0












                                                                                                                                0








                                                                                                                                0





                                                                                                                                $begingroup$


                                                                                                                                Wolfram Language (Mathematica), 34 bytes



                                                                                                                                A port of @Arnauld's solution.



                                                                                                                                Tr@Select[{##,0}-{0,##},#>0&]&@@#&


                                                                                                                                Try it online!






                                                                                                                                share|improve this answer









                                                                                                                                $endgroup$




                                                                                                                                Wolfram Language (Mathematica), 34 bytes



                                                                                                                                A port of @Arnauld's solution.



                                                                                                                                Tr@Select[{##,0}-{0,##},#>0&]&@@#&


                                                                                                                                Try it online!







                                                                                                                                share|improve this answer












                                                                                                                                share|improve this answer



                                                                                                                                share|improve this answer










                                                                                                                                answered Feb 6 at 4:51









                                                                                                                                alephalphaalephalpha

                                                                                                                                21.3k32991




                                                                                                                                21.3k32991






























                                                                                                                                    draft saved

                                                                                                                                    draft discarded




















































                                                                                                                                    If this is an answer to a challenge…




                                                                                                                                    • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.


                                                                                                                                    • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
                                                                                                                                      Explanations of your answer make it more interesting to read and are very much encouraged.


                                                                                                                                    • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.



                                                                                                                                    More generally…




                                                                                                                                    • …Please make sure to answer the question and provide sufficient detail.


                                                                                                                                    • …Avoid asking for help, clarification or responding to other answers (use comments instead).





                                                                                                                                    draft saved


                                                                                                                                    draft discarded














                                                                                                                                    StackExchange.ready(
                                                                                                                                    function () {
                                                                                                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f179464%2fcovering-a-skyline-with-brush-strokes%23new-answer', 'question_page');
                                                                                                                                    }
                                                                                                                                    );

                                                                                                                                    Post as a guest















                                                                                                                                    Required, but never shown





















































                                                                                                                                    Required, but never shown














                                                                                                                                    Required, but never shown












                                                                                                                                    Required, but never shown







                                                                                                                                    Required, but never shown

































                                                                                                                                    Required, but never shown














                                                                                                                                    Required, but never shown












                                                                                                                                    Required, but never shown







                                                                                                                                    Required, but never shown







                                                                                                                                    Popular posts from this blog

                                                                                                                                    Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                                                                                                                                    ComboBox Display Member on multiple fields

                                                                                                                                    Is it possible to collect Nectar points via Trainline?