Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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.

share|improve this question
@dmckee: Thanks for the edit. However, there's a minor spelling error: 'a anotomically' should read 'an anatomically'. – Joey Adams Mar 13 '11 at 0:11
@Joey: And I misspelled "anatomically", too. Thanks. – dmckee Mar 13 '11 at 0:13
4  
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? – houbysoft Mar 13 '11 at 1:19
2  
My uptime is 27 minutes :( – steenslag Mar 13 '11 at 22:24
1  
@userunknown It had relevance in the first version of this question. Have a look at the edits... – Gareth Apr 13 '12 at 11:24
show 3 more comments

18 Answers

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

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.

share|improve this answer
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? – Magnus Holm Mar 12 '11 at 22:55
+1 Parameter substitution FTW! – Helper Method Apr 28 '11 at 19:26

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

share|improve this answer
You can unquote the D. – J B Apr 1 '11 at 12:10

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)

share|improve this answer

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
share|improve this answer

57 characters

python -c "print'8'+'='*int(raw_input().split()[2])+'D'"
share|improve this answer

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 '')

share|improve this answer
1  
Escape space and asterisk with a backslash instead of quoting for 2 chars. – J B Apr 1 '11 at 9:35

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.

share|improve this answer
+1 You rarely see Common Lisp used here. – Helper Method Apr 28 '11 at 19:27

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).

share|improve this answer

PHP, 62 characters

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

No input required, it shells out.

share|improve this answer

Python (42)

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

Note: Python 3 used for reducing total character count.

share|improve this answer

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');}
share|improve this answer
your Golfscript is 23 chars, not 24. Also, you can shorten it to 16, using selection by index(equals sign): ' '/3=~8\'='*'D' – w0lf May 28 '12 at 18:59

Python 65

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

share|improve this answer

PHP, 61

<?$n=split(' ',$argv[1]);$v=8;while($n[2]--)$v.'=';echo"$vD";
share|improve this answer

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.)

share|improve this answer

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============================================================================ ...
share|improve this answer

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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