12
\$\begingroup\$

My apologies for the wordiness. We had fun with it at work in our internal golfing, but it required a few emails for clarification, so I hope I captured most of that the first time.

PROBLEM

Given a “simulated” snowfall snapshot (as a string, stdin or parameter to your function/method), report on 4 values: minimum depth, maximum depth, average depth, and duration.

Here’s a formatted example to illustrate the input:

* * **     
** * ***    
** *  *    
*          
--------

The ‘*’ is a snowflake and the ‘-‘ is the ground. Imagine all of those “snowflakes” falling toward the ground. This is the final “snapshot”:

*      
*          
** * **    
********    
--------

You need to report on:

  1. Minimum depth - count the shortest “pile” (1, in example above)
  2. Maximum depth - count the tallest “pile” (4, in example above)
  3. Average depth - average count of all “piles” (1.9, in example above - rounding to tenths)
  4. Duration - (each “fall” = 1 second) - time from the first snapshot to the last snapshot (3 seconds, in example above)

Another example for reference:

     * 
   *   *
      *
*      
--------

Final snapshot:

*  * ***
--------

INPUT

The starting “snapshot” will have 8 “rows”, 8 “columns”, and be pipe-delimited (the pipes separate the rows). For example (excluding quotes):

“        |        |        |        |     *  |   *   *|      * |*       |--------” 

OUTPUT

The 4 values pipe-delimited on a single line: 0|1|.6|3 or 0|5|1|1

\$\endgroup\$
  • \$\begingroup\$ Can the ground have varying heights? \$\endgroup\$ – Nemo157 Feb 8 '11 at 7:12
  • \$\begingroup\$ @Nemo157 - that wasn't in our original problem, so for the purposes here, no. That'd be an interesting variation to the problem. \$\endgroup\$ – Steve Feb 8 '11 at 18:53
  • \$\begingroup\$ Do you require rounding the average? \$\endgroup\$ – Titus Jul 23 '16 at 10:18
1
\$\begingroup\$

Perl, 128 chars

map{$d++;s/\*/$d{$-[0]}++;$t&&$t<$d or$t=$d/ge}split'\|',<>;map{$A+=$_/8}@m=sort values%d;printf"$m[0]|$m[-1]|%.1f|%d\n",$A,8-$t
\$\endgroup\$
6
\$\begingroup\$

Python, 153 characters

s=raw_input()
R=range(8)
C=[s[i::9].count('*')for i in R]
print"%d|%d|%.1f|%d"%(min(C),max(C),sum(C)/8.,max(8-s[i::9].find('*')-C[i]for i in R if C[i]))
\$\endgroup\$
1
\$\begingroup\$

Windows PowerShell, 180 189

$a=(0..7|%{$x=$_;-join(0..7|%{$s[$_*9+$x]})})
$b=$a-replace' '|%{$_.length}|sort
"{0}|{1}|{2:.0}|{3}"-f($b[-1..0]+(($b-join'+'|iex)/8),($a|%{($_-replace'^ +|\*').length}|sort)[-1])

Input comes in as $s.

History:

  • 2011-02-10 01:53 (189) – First attempt.
  • 2011-02-10 02:03 (180) – Format string to the rescue. Only downside: It outputs numbers in regional format, no longer strictly conforming to the task specification. Still, that's a problem shared by the C# solution as well so I guess that's ok.
\$\endgroup\$
0
\$\begingroup\$

A couple other language results from our internal golfing:

C#, 188 characters (easily readable)

int a = 0, b = 8, c = 0, d = 0, f, i, j, x;    
for (i = 0; i < 8; i++)    
{    
      f = x = 0;    
      for (j = 0; j < 8; )
            if (s[i + j++ * 9] == '*')    
            {    
                  x = x > 0 ? x : 9 - j;    
                  f++;    
            }         

      d += f;    
      a = f > a ? f : a;    
      b = f < b ? f : b;    
      c = x - f > c ? x - f : c;    
}

Console.Write("{0}|{1}|{2:.0}|{3}", b, a, d / 8f, c);

Ruby, 173 characters

s=gets;n=8;a=o=l=0;8.times{|i|f=x=0;8.times{|j|if(s[i+j*9].chr=='*'):x=8-j if x==0;f+=1;end}
o+=f;a=f if f>a;n=f if f<n;l=x-f if x-f>l}
printf "%d|%d|%.1f|%d",n,a,o.to_f/8,l
\$\endgroup\$
0
\$\begingroup\$

PHP, 139 bytes

<?for(;$y<8;$y++)for($x=0;++$n[$x],$x<8;$x++)'!'>$argv[1][$y*9+$x]&&--$n[$x]?$b[$x]++:0;print_r([min($n),max($n),array_sum($n)/8,max($b)]);

for each column, count no. of snowflakes and no. of spaces below the first snowflake, then calculate
takes string as argument from command line

breakdown and golfing steps

$i=$argv[1];
for(;$y<8;$y++)         # $y=row
{
    for($x=0;$x<8;$x++) # $x=column
    {
        // loop body: 32+26=58 bytes
        if('*'==$i[$p=$y*9+$x])$n[$x]++;# if snowflake: increase snowflakes count
        elseif($n[$x]+=0)$b[$x]++;      # elseif has snowflakes: increase blanks count
                                        # +=0: min needs a value in every column

        // -> golf if/elseif to ternaries, 16+9+25+1 = 51 (-7)
#       '*'==$i[$y*9+$x]
#           ? $n[$x]++
#           : (($n[$x]+=0)?$b[$x]++:0)
#       ;

        // golfing on the ternary, 15+23+9+1 = 48 (-3)
#       '!'>$i[$y*9+$x]                 # -1: char<'!' <=> char==' '
#           ? ($n[$x]+=0)?$b[$x]++:0    # -2: inverted condition -> eliminated parens
#           : $n[$x]++
#       ; # 15+23+9+1-2=46

        // increase snowflakes count in any case (initialization)
        // (needs curlies; elminate them by moving increase to the for condition)
        // decrease snowflakes count for blank
#       ++$n[$x];if('!'>$i[$y*9+$x]&&--$n[$x])$b[$x]++; # if: 47
#       ++$n[$x];'!'>$i[$y*9+$x]&&--$n[$x]?$b[$x]++:0;  # ternary: 46
    }
}
print_r([min($n),max($n),array_sum($n)/8,max($b)]); # calculate and print results

add round(...,1) around array_sum($n)/8 for rounding (+9)

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

Mathematica, ~115

"        \n        \n* * **  \n        \n** * ***\n** *  * \n*       \n--------"

Most/@ToCharacterCode@%~Partition~9/10-16/5
Min@#|Max@#|Mean@#~Round~.1|Position[#,1][[{-1,1},1]].{1,-1}-1&@Total@%
1 | 4 | 1.9 | 4

In this post I had to resort to the escaped string form \n as paste and copy loses spaces. In the Front End I can enter it like this:

enter image description here

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

JavaScript, 328 Bytes

Not particularly short, but it's what I've got.

var input = "        |        |        |* * **  |        |** * ***|** *  * |*       |--------";

(s=>{var l=s.split("|"),a=[0,0,0,0,0,0,0,0],o=[8,0,0,0],b;for(i=0;i<l.length;i++){if(!b&&l[i]!="        ")o[3]=7-i,b=1;for(j=0;j<l[i].length;j++){a[j]+=l[i][j]=="*"?1:0}}for(i=0;i<a.length;i++){if(a[i]<o[0])o[0]=a[i];if(a[i]>o[1])o[1]=a[i];o[2]+=a[i];}console.log(o[0]+"|"+o[1]+"|"+(o[2]/a.length).toFixed(1)+"|"+o[3]);})(input)

De-golfed:

var input = "        |        |        |        |     *  |   *   *|      * |*       |--------";

(s=>{
  var splits = s.split("|");
  var arr = [0,0,0,0,0,0,0,0];
  var b;
  var o=[8,0,0,0];
  for (var i = 0; i < splits.length; i++) {
    if (!b&&splits[i]!="        ") {
      o[3] = 7-i;b=1;
    }
    for (var j = 0; j < splits[i].length; j++) {
      arr[j]+=splits[i][j]=="*"?1:0;
    }
  }
  for (var i = 0; i < arr.length; i++) {
    if (arr[i]<o[0]) {
      o[0]=arr[i];
    }
    if (arr[i]>o[1]) {
      o[1]=arr[i];
    }
    o[2]+=arr[i];
  }
  console.log(o[0]+"|"+o[1]+"|"+(o[2]/arr.length).toFixed(1)+"|"+o[3]);
})(input)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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