13
\$\begingroup\$

Write a program which parses the output of uptime and generates an anatomically suggestive progress bar (as shown) with a length equal to the current uptime in days:

$ uptime
23:01  up 34 days,  7:30, 5 users, load averages: 0.23 0.27 0.24
$ uptime|<command>
8==================================D

(34 days = 34 equal signs)

Shortest answer wins.

\$\endgroup\$
8
  • \$\begingroup\$ @dmckee: Thanks for the edit. However, there's a minor spelling error: 'a anotomically' should read 'an anatomically'. \$\endgroup\$
    – Joey Adams
    Commented Mar 13, 2011 at 0:11
  • \$\begingroup\$ @Joey: And I misspelled "anatomically", too. Thanks. \$\endgroup\$ Commented Mar 13, 2011 at 0:13
  • 5
    \$\begingroup\$ Hmm, just a nitpick, a "progress bar" for something that doesn't have a known end (so, you can't measure its progress) sounds weird. Maybe just "bar" would be enough? \$\endgroup\$
    – houbysoft
    Commented Mar 13, 2011 at 1:19
  • 2
    \$\begingroup\$ My uptime is 27 minutes :( \$\endgroup\$
    – steenslag
    Commented Mar 13, 2011 at 22:24
  • 1
    \$\begingroup\$ @userunknown It had relevance in the first version of this question. Have a look at the edits... \$\endgroup\$
    – Gareth
    Commented Apr 13, 2012 at 11:24

20 Answers 20

6
\$\begingroup\$

Ruby, 39

No input required (by shelling out):

`w`=~/up (\d*)/;puts '8'+'='*$1.to_i+'D'

Or from stdin (40 chars):

gets=~/up (\d*)/;puts '8'+'='*$1.to_i+'D'
\$\endgroup\$
7
  • \$\begingroup\$ Reduce by 2 characters by using string interpolation and a character literal: gets=~/up (\d*)/;puts "8#{?=*$1.to_i}D" \$\endgroup\$ Commented Mar 13, 2011 at 13:27
  • \$\begingroup\$ The character count is exactly the same using interpolation and concatenation in this case. \$\endgroup\$
    – david4dev
    Commented Mar 13, 2011 at 13:32
  • \$\begingroup\$ It's not, copy paste them into your editor: twitpic.com/49413j \$\endgroup\$ Commented Mar 13, 2011 at 13:39
  • \$\begingroup\$ 'gets=~/up (\d*)/;puts "8#{"="*$1.to_i}D"'.length=="gets=~/up (\d*)/;puts '8'+'='*$1.to_i+'D'".length #true \$\endgroup\$
    – david4dev
    Commented Mar 13, 2011 at 13:46
  • \$\begingroup\$ Save some chars in the last line: $><<?8+?=*$1.to_i+?D \$\endgroup\$
    – steenslag
    Commented Mar 13, 2011 at 14:25
5
\$\begingroup\$

Bash, 71 characters

a=`uptime` a=${a#*p } a=${a%% *};while((a--));do p==$p;done;echo I${p}I

I'm not that much into exposing my genuptimes on the internet, so I'm drawing a fence instead. It's the same character count. Think of resetting p between calls.

Pure bash, 71 characters including uptime.

$ uptime
 23:35:12 up 159 days,  4:15, 15 users,  load average: 1.07, 0.63, 0.38

Well, well, well... mine's bigger than yours.

\$\endgroup\$
1
  • \$\begingroup\$ In case it wasn't clear: you can shell out to your favorite language if you want (it doesn't need to be pure bash). Should I clarify the question? \$\endgroup\$ Commented Mar 12, 2011 at 22:55
5
\$\begingroup\$

Perl - 26 24 characters

/p (\d+)/;say+8,"="x$1,D

Ran like so:

$ uptime
 04:52:39 up 17 days, 11:27,  3 users,  load average: 0.21, 0.07, 0.02
$ uptime | perl -nE '/p (\d+)/;say+8,"="x$1,D'
8=================D

edit: Unquoted the final 'D' - thanks J B

\$\endgroup\$
1
  • \$\begingroup\$ You can unquote the D. \$\endgroup\$
    – J B
    Commented Apr 1, 2011 at 12:10
4
\$\begingroup\$

Haskell, 88 characters

As is often the case, Haskell doesn't offer the most extreme golfability of some other languages, but does allow an elegant expression of the mathematical structures underlying the problem. Building on their seminal work in this field, I aim to introduce a Haskell implementation of the Holkins-Krahulik Approximation operator. Briefly, the operator ignores both its inputs and returns a phallus-printing function parameterized by shaft length.

_⁑≈≈≈⊃_=(\n->concat["8",take n$repeat '=',"D"])
main=interact$(0⁑≈≈≈⊃1).read.(!!2).words
\$\endgroup\$
4
\$\begingroup\$

Perl, 17 characters
Now with say:

say+8,"="x$F[2],D

Yields

uptime | perl -naE 'say+8,"="x$F[2],D'

was:

print"8"."="x$F[2]."D"

Yields

]# uptime | perl -anle 'print"8"."="x$F[2]."D"'
8=========================D

(I can't "say", sadly)

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

57 characters

python -c "print'8'+'='*int(raw_input().split()[2])+'D'"
\$\endgroup\$
3
\$\begingroup\$

35 characters

awk '{printf"#%"$3"sp",p}'|tr \  \*

so,

uptime
12:27μμ  up 111 days,  2:36, 1 user, load averages: 0,07 0,03 0,00
uptime | awk '{printf"#%"$3"sp",p}'|tr ' ' '*'

#***************************************************************************************************************p

Edit: was

printf "#%`awk '{print $3}'`sp"|tr ' ' '*'

Edit 2: J B's comment (use \ instead of '')

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Escape space and asterisk with a backslash instead of quoting for 2 chars. \$\endgroup\$
    – J B
    Commented Apr 1, 2011 at 9:35
3
\$\begingroup\$

Windows PowerShell, 28

"8$('='*(-split$input)[2])D"

At least

echo 23:01  up 34 days,  7:30, 5 users, load averages: 0.23 0.27 0.24|powershell -file uptime.ps1 

yields the correct output, so it should be able to handle uptime's output. I cannot test, though, since GNUWin32 includes a broken uptime that tries to read from a non-existant file (Note to people porting Unix tools: Don't try to assume that Windows is a Unix and adheres to the same principles or conventions; there is no file containing the boot time on Windows).

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

Common Lisp, 84 characters

(defun p(s)(write 8)(loop repeat(read-from-string(subseq s 9))do(write'=))(write'D))

This takes the uptime string as input. It seems like there ought to be a shorter solution mapping #'write over a list, but if so I can't come up with it.

\$\endgroup\$
1
  • \$\begingroup\$ +1 You rarely see Common Lisp used here. \$\endgroup\$ Commented Apr 28, 2011 at 19:27
3
\$\begingroup\$

GolfScript (24 characters)

' '/{(;}3*(\;~'='*8\'D'

Haskell (73 characters)

main=getLine>>=putStr.('8':).(++"D").(`take`repeat '=').read.(!!2).words

C (131 characters)

#define r(a);read(0,x,a),
#define p ;putchar(
x[9];main(i){for(r(1)i<3;i+=*x==32)r(9)0
p'8');for(i=atoi((int)x+2);i--p'='))p'D');}
\$\endgroup\$
1
  • \$\begingroup\$ your Golfscript is 23 chars, not 24. Also, you can shorten it to 16, using selection by index(equals sign): ' '/3=~8\'='*'D' \$\endgroup\$ Commented May 28, 2012 at 18:59
2
\$\begingroup\$

PHP, 62 characters

<?$d=split(" ",`uptime`);echo 8;while($d[3]--)echo"=";echo"D";

No input required, it shells out.

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

Python (42)

print('8'+int(input().split()[2])*"="+'D')

Note: Python 3 used for reducing total character count.

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

Python, 65 Bytes

import sys
print '8'+'='*int(sys.stdin.readline().split()[2])+'D'

test:

echo '23:01  up 34 days,  7:30, 5 users, load averages: 0.23 0.27 0.24'|./time.py
8==================================D
\$\endgroup\$
1
\$\begingroup\$

PHP, 61

<?$n=split(' ',$argv[1]);$v=8;while($n[2]--)$v.'=';echo"$vD";
\$\endgroup\$
1
\$\begingroup\$

Common Lisp, 57 characters

(excluding implementation/OS-specific shebang)

#!/opt/local/bin/sbcl --script
(do()((eql(read-char)#\p)(format t"8~v,,,'=<~>D"(read))))

Based on an attempt to satisfy Dr. Pain's answer's wish for a shorter way to write a repeated character. This one can be used in a pipeline as shown in the question.

(The format string specifies to right-justify the empty string (between ~< and ~>), in a field of width specified by an argument, padded using the = character.)

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

K, 35

-1"8",(("I"$(" "\:0:0)@3)#"="),"D";

.

$ uptime
17:21:47 up 242 days,  7:22, 35 users,  load average: 0.33, 0.34, 0.44
$ uptime | q t.k
8============================================================================ ...
\$\endgroup\$
0
\$\begingroup\$

Bash 67 chars

read t u d w
echo -e '\t'|expand -t $d|sed 's/^/8/;s/ /=/g;s/$/B/;'

invocation to the letter of the assignment:

uptime | ./cg1570uptime-bar.sh

Much shorter

just 54 chars:

with this variation:

echo -e '\t'|expand -t $3|sed 's/^/8/;s/ /=/g;s/$/B/;'

invocation, not 100% in accordance with the rules:

./cg1570uptime-bar.sh $(uptime)

output in both times:

uptime && uptime | ./cg1570uptime-bar.sh 
06:29:53 up 16 days, 21:03, 10 users,  load average: 1.29, 1.34, 1.23
8================B

Non-everyday tricks:

 read t u d w

reads 06:29:53=t, up=u, 16=d rest...=w
without w, everything to the end would be put into $d.

expand is normally used to translate a tab into an amount of blanks and takes a parameter if you don't like 8.

Grabbing the 3rd parameter with $3 in echo -e '\t'|expand -t $3|sed 's/ /=/g' is even shorter, but needs an invocation, not fitting to the words of the rules.

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

bash/zenity 38 short version, 68 longer version

read c o d e
zenity --scale --value=$d --max-value=497 --text=uptime

The short version ends after $d, the longer takes into account that we know a max-value and like to have helpful text:

Screenshot zenity as uptime display

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

Gema, 24 characters

<D> d=8@repeat{$1;=}D;?=

Sample run:

bash-4.4$ uptime
 18:35:31 up 13 days,  7:53, 16 users,  load average: 0.31, 0.85, 1.24

bash-4.4$ uptime | gema '<D> d=8@repeat{$1;=}D;?='
8=============D
\$\endgroup\$
0
\$\begingroup\$

AutoHotkey, 39 bytes

d:=A_TickCount//=8.64e+7
Send 8{= %d%}D

No input. Result is sent to active window.
The built-in A_TickCount is the number of milliseconds since the computer was rebooted.

\$\endgroup\$

Your Answer

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

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