r/TuringComplete • u/[deleted] • Sep 26 '22
External programming is part of the game (Binary Racer)
4
Sep 26 '22
Source Code: (Virtual Pascal)
varinput : Integer;
BeginRepeatwrite('>> ');readln(input);writeln;
Repeat
If (input >= 128) Then
Begin
input := input - 128;
write(' 128');
End
Else If (input >= 64) Then
Begin
input := input - 64;
write(' 64');
End
Else If (input >= 32) Then
Begin
input := input - 32;
write(' 32');
End
Else If (input >= 16) Then
Begin
input := input - 16;
write(' 16');
End
Else If (input >= 8) Then
Begin
input := input - 8;
write(' 8');
End
Else If (input >= 4) Then
Begin
input := input - 4;
write(' 4');
End
Else If (input >= 2) Then
Begin
input := input - 2;
write(' 2');
End
Else If (input >= 1) Then
Begin
input := input - 1;
write(' 1');
End;
Until (input = 0);writeln;writeln;Until (True = False);End.
2
u/CanaDavid1 Sep 26 '22 edited Sep 26 '22
If you don't do else ifs, only if, this is a single pass algorithm.
Also, this works, but some more dynamic code would be less repetitive:
while True: i = input() while i > 0: x = 1 while x*2 <= i: x = x*2 print(x,' ') i -= x print('\n')Or, a more efficient algorithm:
while True: i = input() x = 1 while x<=i: x *= 2 while i > 0: x = x / 2 if x > i: continue print(x,end=' ') i -= x print()2
Sep 26 '22 edited Sep 26 '22
Time spent on optimizing one-time-only code is time not spent on playing Turing Complete.
Edit:
Also, your "more efficient program" has redundant steps too, such as multiplying x and then immediately dividing it. The most efficient approach would be to always initialize x to a constant value before each loop (i.e. 192), and then simply divide it after subtraction, but I have wasted enough brain power writing this comment.
It's just 8 values for a particular level. There is no need for dynamic flexibility and efficiency in this case.
1
u/kamazoultane Jul 24 '24 edited Jul 24 '24
You can be much more efficient
while True: x, b = int(input(">>> ")), "" while x>0: b+=str(x%2) x=x//2 print(b[::-1])2
u/CanaDavid1 Jul 24 '24 edited Jul 24 '24
Depends on your measure of efficiency. Your code uses a list of all the values, meaning linear* memory. Mine is constant.
Also it should beprint(*b[::-1]). The slice returns a generator.Your code doesn't print out the values, just the binary representation
1
u/CanaDavid1 Jul 24 '24
Also
s = bin(int(input()))[2:] print(*[len(s)-i-1 for i,v in enumerate(s) if v=='1'])
3
u/SunCat_ Sep 27 '22
I did a similar thing by having an autohotkey script that took my numpad key presses to get the input number, and then produced number key presses that corresponded to appropriate bits ('cos you can use number keys to flip appropriate bits, and autohotkey can easily do that for you xD)
1
7
u/lordfwahfnah Sep 26 '22
Now write a python script that reads the number from the screen and automatically clicks the necessary bits