53
\$\begingroup\$

This is my first golf contest.

What you need to do

Build me, in the shortest amount of bytes possible, my AC remote control system. My room is too cold right now, and I'm missing my remote.

Now, I don't want you literally building it or anything, just golf this:

A slow increment of temperature, starting at 40 degrees, and ending at exactly 72. The increment time must always be 500 millis per increment. It can wait another 500ms at the end. I would prefer it to stop however. The increment itself must go up by two each time, like my remote.

You should not clear the screen. You should have newlines.

What should happen

Example output (everything in parentheses shouldn't be outputted).

40
(wait 500 millis)
42
(wait 500 millis)
44
(..repeat until 72..)
72
(stop or wait 500ms)

Keep in mind This is my first golf, so I apologize if this is too hard to golf. :(

Best of luck, golfers!

\$\endgroup\$
  • 3
    \$\begingroup\$ Minor thing, but "must always be 500 millis" is fundamentally too strict for any reasonable device. I'd recommend specifying a variance, something like +/-10%. \$\endgroup\$ – FryAmTheEggman Dec 24 '16 at 2:52
  • 1
    \$\begingroup\$ Can you wait 500ms before showing initial output? \$\endgroup\$ – FlipTack Dec 24 '16 at 11:25
  • 35
    \$\begingroup\$ -1 for using Fahrenheit (not really, but you should at least say you're using it; 40 degrees celsius isn't too cold in the slightest) \$\endgroup\$ – John Dvorak Dec 24 '16 at 11:54
  • 20
    \$\begingroup\$ +1 for using Fahrenheit, it has better resolution than Celsius and is just as arbitrary as anything not Kelvin or Rankine \$\endgroup\$ – Nick T Dec 24 '16 at 18:20
  • 8
    \$\begingroup\$ @NickT then you're out of luck because this remote's resolution is 2°F which is higher than 1°C. And you can get higher resolution in Celcius than Fahrenheit with a remote that can display 0.5 and much more if it can display to 0.1. Anyway I'm a simple man and can't differentiate between 22 and 23°C so high resolution in this case is useless to me \$\endgroup\$ – phuclv Dec 27 '16 at 12:04

53 Answers 53

2
\$\begingroup\$

Windows Batch - 65 62 61 bytes

btw this is my first PPCG answer

@for /l %%p in (40,2,72)do @echo %%p&@ping 1.1 -n 1 -w 10>nul

This exits when the degree reaches 72.


76 75 bytes - Does not sleep at end

@for /l %%p in (40,2,72)do @echo %%p&if %%p lss 72 @ping 1.1 -n 1 -w 10>nul

Ungolfed and explanation:

@echo off                           - Turns of the prompt(C:\Foo\Bar\Baz>)
for /l %%p in (40,2,72) do (        - For loop that loops from 40 to 72, increase 2 each
    echo %%p                        - Outputs the loop counter(which is the temp.)

if %%p lss 72 (                     - If loop counter is less than 72,
        ping -n 1 -w 10 1.1 >nul    - ping an non-existing IP to make delay

                                    - note: ping will always make 500ms delay
                                    - if waiting time is less than 500ms
    )                               - Closing bracket(if statement)

)                                   - Closing bracket(for statement)
\$\endgroup\$
2
\$\begingroup\$

Excel VBA (32-Bit Only), 123 Bytes

Declares the windows Kernel32 Sleep(ByVal dwMilliseconds as Long) function and uses a recursive helper function. Outputs to the VBE immediate window.

Declare Sub Sleep Lib"kernel32"(ByVal t&)
Sub a(i)
Debug.?i
If i<72Then Sleep 500:DoEvents:a i+2
End Sub
Sub b:a 40:End Sub    

Usage and Ouput

as seen from an b subroutine call from the VBE immediate window

b        ''  <- Subroutine Call
 40      ''  <- Output
 42 
 44     
 46     
 48     
 50     
 52     
 54     
 56     
 58     
 60     
 62     
 64     
 66     
 68     
 70     
 72   
\$\endgroup\$
2
\$\begingroup\$

Common Lisp, 57 52

(loop for b from 40 while(<(print b)72)do(sleep .5))
  • Print returns its input parameter.
  • The program exits immediately after reaching 72.

Ungolfed:

(loop
  for b from 40
  do (print b)
  while (< b 72)
  do (sleep .5))
\$\endgroup\$
2
\$\begingroup\$

Browser Javascript, 52 51 chars

eval(s="s=alert(x=setTimeout(s,500)*2+38)|x<69&&s")

Test with console.log instead of alert:

eval(s="s=console.log(x=setTimeout(s,500)*2+38)|x<69&&s")

\$\endgroup\$
  • \$\begingroup\$ Always room to improve I see :—) Nice! \$\endgroup\$ – RudolfJelin Mar 20 at 16:59
2
\$\begingroup\$

PowerShell, 32 31 bytes

40..72|%{$_[$_%2];sleep -m 250}

Try it online!

Sleeps regardless if it's odd or even for half the time but will only output when the value is even due to out-of-bounds indexing returning nothing. Lost several bytes due to sleep defaulting to seconds and only taking ints.

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

*><>, 20 bytes

"H&"v
?;5S>2+:n:{:}=

Try it here!

This outputs the 40 and adds two every 500ms, outputting the new number. At 72 the program does not wait and exits immediately. Wasn't sure if we needed any sort of deliminator in-between the temperatures so there isn't one included.

Explanation

Setup:

"J("   push 72 and 38 (temp) to the stack
    v  enter main loop at ">"

Main loop:

>               move the IP right
 2+             add two to temp
   :n           copy and output temp
     :{:}       copy temp and 72
         =?;    if temp == 72, exit
            5S  sleep 500ms
\$\endgroup\$
1
\$\begingroup\$

Factor, 36 bytes

20 36 [a,b] [ 2 * . 5e8 sleep ] each

Pretty simple.

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

Python 3, 61 bytes

Not a winner, but a horrible, horrible abuse of a list comprehension: two side-effects and the result isn't even used.

import time
[print(2*x+40)==time.sleep(.5)for x in range(17)]

Similar:

[time.sleep(.5)for x in map(print,range(40,74,2))]
\$\endgroup\$
  • \$\begingroup\$ I like this. It's clever. Reminds me of a time when I had to print and return something inside of a lambda so I did lambda: (print(...), ...)[1] \$\endgroup\$ – Quelklef Dec 27 '16 at 5:15
1
\$\begingroup\$

GNU sed, 87 + 1 = 88 bytes

+1 byte for -u flag (prevents sed from waiting until the end to flush all of the output).

Assumes it's running in an environment where the shell command sleep .5 works.

s/.*/40/
:
p
esleep .5
/72/Q
/8/bz
h
y/0246/2468/
s/.//
H
x
s/.\n//
b
:z
y/4568/5670/
b

Try it online! (Using Dennis' handy bash wrapper to timestamp each output line.)

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

Python 2, 54 bytes

Thanks @xnor for cutting 2 bytes off!

import time;x=40
exec'print x;x+=2;time.sleep(.5);'*17

55 bytes

This one uses a dfferent counting approach, can probably still be golfed:

import time;x=34
while x:print 74-x;x-=2;time.sleep(.5)
\$\endgroup\$
  • 1
    \$\begingroup\$ Can't your exec loop do x+=2? \$\endgroup\$ – xnor Dec 25 '16 at 9:00
  • \$\begingroup\$ @xnor yep, brain derp... thanks \$\endgroup\$ – FlipTack Dec 25 '16 at 9:08
1
\$\begingroup\$

RBX.Lua, 36 bytes

Uses UTF-8 encoding. Golfed:

for i=40,72,2 do wait(.5)print(i)end

Should be pretty obvious to people with Lua knowledge. Here's an explanation:

for i=40,72,2 do --For-loop, counting from 40 to 72 in steps of 2.
wait(.5) --RBX.Lua builtin. Waits 0.5 seconds.
print(i) --Prints current temperature.
end --End of for loop.
\$\endgroup\$
1
\$\begingroup\$

Java, 93 bytes

a->{for(a=38;(a+=2)<73;){System.out.println(a);try{Thread.sleep(500);}catch(Exception e){}}};

This is a java.util.function.Consumer<Integer> that makes me want to take a new NullPointerException() and use it to slap whoever who made Thread.sleep() throw a checked exception.

Identical-in-length snippet that does the exact same thing with an "everyday" method:

void a(int A)throws Exception{for(A=38;(A+=2)<73;){System.out.println(a);Thread.sleep(500);}}
\$\endgroup\$
  • \$\begingroup\$ How long would this be as a full program? \$\endgroup\$ – RudolfJelin Dec 26 '16 at 11:03
1
\$\begingroup\$

postgresql9.6, 162 bytes

do language plpgsql $$ declare n smallint;begin foreach n in array array(select generate_series(40,72,2))loop raise info'%',n;perform pg_sleep(.5);end loop;end;$$

formatted sql is here:

do language plpgsql $$
declare n smallint;
begin
foreach n in array array(select generate_series(40, 72, 2)) loop
    raise info '%', n;
    perform pg_sleep(.5);
end loop;
end;
$$
\$\endgroup\$
1
\$\begingroup\$

ForceLang, 71 bytes

For some unknown reason, does not quite work if you run the interpreter using Eclipse, but still functions properly if you run the interpreter using your terminal directly. ¯\_(ツ)_/¯

set s 38
label 1
io.writeln set s s+2
datetime.wait 500
if -72+s
goto 1
\$\endgroup\$
1
\$\begingroup\$

SAS, 60 bytes

Something different :)

data _null_;do i=40 to 72; put i;s=sleep(.5,1);end;run;
\$\endgroup\$
1
\$\begingroup\$

Swift, 208 bytes

import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
func n(){if t<72{DispatchQueue.main.asyncAfter(deadline:.now()+0.5){t+=2;print(t);n()}}}
var t=40;print(t);n()

sleep() takes a UInt32 in Swift, so won't work for the 500 millisecond delay. If it was 1 second however:

import Foundation;(20...36).map{$0*2}.forEach{print($0);sleep(1)}

65 bytes (with a delay at the end)

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

Python 2, 57 bytes

import time as t
x=40
while x<73:print x;t.sleep(.5);x+=2

This does require an 'extra' newline to actually run, so arguably response is 58 bytes (please feel free to edit/comment if this is what the figure above should be).

Edit: Remove unecessary whitespace between : and print

Edit2: changed 0.5 to .5, saving one byte.

Edit3: failed to add/count the sleep import, which raises the score to 60 bytes.

Edit4: Saved 3 bytes by adding extra line

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

101, 98 78 bytes

00001 101000 001 11 00001 01001000 00001 0001 000010 1111 00001 1001 000101 111

EDIT: The language has beed updated to support equals etc. So i managed to narrow it down a little. Note: i did not update the explaination.

developed a programming language in python over the course of the 2 last days. Its fairly simple, but is a pain to program in, as it consists only of 1's and 0's, and can only hold binary numbers with up to 6 numbers.

Explenation:

00001 101000 # declares variable 00001 to 40
00010 100010 # declares variable 00010 to 34

001 00010 # start of loop, checks if variable 00010 is over 0
00010 0011 000010 # 0011 is the function for decrementing, and i decrement by 2 (decimal)
1111 00001 # 1111 is the function for printing
00001 0001 000010 # same as above, only addition
1001 000101 # 1001 is timer, takes a whole number and divide by 10 to allow milliseconds
111 # same as end symbol in ruby

EDIT: Link to the language https://github.com/hrrs01/101 Easily tested by running the demo file by doing (if python in PATH):

101.py demo.txt

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

Common Lisp, 54 bytes (45 if trailing newline is allowed)

(dotimes(i 17)(format t"~a~&"(+(* 2 i)40))(sleep 0.5))

shorter - with trailing newline, because print adds one:

(dotimes(i 17)(print(+(* 2 i)40))(sleep 0.5))
\$\endgroup\$
1
\$\begingroup\$

RbxLua, 36 bytes

for i=40,72,2 do print(i)Wait(.5)end

Thought I'd use RbxLua, which is a modified version of Lua 5.1 used on ROBLOX.com, as it has a built-in Wait(s) function that is one byte shorter than sleep(ms) in Lua 5.3.

Edit: Didn't see the other RbxLua solution. This solution uses a non-deprecated form of wait.

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

dc, 50 bytes

0sm[40lm+p0se[le1+dse120000>j]dsjxlm2+dsm33>p]dspx

Well, this seems to be the first answer which does not utilize any sleep built-in as dc seemingly does not have one. Instead, this simply utilizes a macro loop which during each of 16 iterations increments 40 by 2*iteration #, outputs the sum on a new-line, and utilizes another macro loop which is iterated through 120000 times giving us our desired 500 millisecond sleep with a very slight and unnoticeable variance.

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

05AB1E, 11 bytes

40žvƒ=Ì₄;.W

Try it online.

Explanation:

40           # Push 40
  žvƒ        # Loop 17 times (in the range [0,16]):
     =       #  Print the top of the stack with newline (without popping)
      Ì      #  Increase the number by 2
       ₄;    #  Push 1000 halved to 500
         .W  #  And sleep that many ms
\$\endgroup\$
0
\$\begingroup\$

Javascript (ES6), 48 bytes

f=(i=40)=>alert(i)|i-72&&setTimeout(f,500,i+2)
    
f()

Based on @ETHproductions answer.

\$\endgroup\$
  • \$\begingroup\$ Problem: I can stop the execution by not pressing the ok button. Tested on my Win10 64-bit, Chrome 56.0.2924.87 \$\endgroup\$ – stevefestl Mar 26 '17 at 8:22
  • 2
    \$\begingroup\$ @SteveFest that is normal with javascript... nothing to downvote about \$\endgroup\$ – arodebaugh May 25 '17 at 21:14
  • \$\begingroup\$ Ok I see, btw I didn't down vote you \$\endgroup\$ – stevefestl May 25 '17 at 22:31
  • \$\begingroup\$ Hmm well sorry.. \$\endgroup\$ – arodebaugh May 26 '17 at 0:05
  • \$\begingroup\$ Never mind :) \$\endgroup\$ – stevefestl May 26 '17 at 9:58

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.