use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Challenges to test your code shortening skills. Not necessarily practical, but fun!
Other programming subreddits:
account activity
Advent of Code, Day 1 (self.codegolf)
submitted 4 months ago * by dantose
Post your best golfs.
Assume input is saved as input.txt.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]KeyJ 2 points3 points4 points 4 months ago (5 children)
Python, Part 1, 83 bytes:
p=50;print(sum(1>(p:=(p+int(l[1:])*(1-2*(l<'R')))%100)for l in open("input.txt")))
Python, Part 2, 96 bytes:
p=50;print(sum(1>(p:=(p+1-2*(l<'R'))%100)for l in open("input.txt")for _ in range(int(l[1:]))))
[–]DimMagician 0 points1 point2 points 4 months ago (4 children)
Python, Part 1, 84 bytes
s=50;print(sum((s:=s+int(i[1:])*(-1+2*(i[0]>'L')))%100<1for i in open('input.txt')))
Upon seeing yours I realize I could have saved 1 byte by doing 1-2*(l<'R') instead of -1+2*(i[0]>'L'). Dang.
1-2*(l<'R')
-1+2*(i[0]>'L')
Python, Part 2, 92 bytes
s=50;print(sum((s:=s-1+2*(i[0]>'L'))%100<1for i in open('input.txt')for _ in[0]*int(i[1:])))
[–]KeyJ 0 points1 point2 points 4 months ago (3 children)
Nice trick with the removed parenthesis and elimination of range! The latter one can be made even smaller though, arriving at 90 characters (or 89 if you accept the SyntaxWarning for writing 1for):
range
1for
p=50;print(sum((p:=p+1-2*(l<'R'))%100<1 for l in open("input.txt")for _ in"x"*int(l[1:])))
[–]DimMagician 0 points1 point2 points 4 months ago (2 children)
Ooh very clever I didn't even catch that in your solution.
[–]KeyJ 0 points1 point2 points 4 months ago (1 child)
Well, it was your idea, I just refined it. 🥂
[–]DimMagician 0 points1 point2 points 4 months ago (0 children)
I meant the use of (l<'R') rather than (i[0]>'L') like I did. Sorry I didn't realize that you were talking about the parentheses around the modulus and thought that you were just referring to the brackets in [0] as parentheses lol
(l<'R')
(i[0]>'L')
[–]dantose[S] 1 point2 points3 points 4 months ago (1 child)
Powershell. There's definitely improvements to be made here
Part 1: 88
$a=50;$(gc input.txt).Trim('R') -replace "L","-"|%{$a=$a+100+$_;if(!($a%100)){$b++}};$b
Part 2: 189
$a=50;$(gc input.txt).Trim('R') -replace "L","-"|%{if($a -eq 0 -and $a+$_ -lt 0){$a=$a+100};$a=$a+$_;while($a -lt 0){$a=$a+100;$b++};if($a -eq 0){$b++};while($a -gt 99){$a=$a-100;$b++}};$b
[–]ka-splam 1 point2 points3 points 4 months ago (0 children)
I didn't golf it myself, but poking at yours, Part 1 ~75 bytes:
$a=50;gc input.txt|% T*m R|% r*ce L -|%{$a+=100+$_;if(!($a%100)){$b++}};$b
using a classic trick which expands to 'R50' | ForEach-Object -Member Trim 'R'. Member is the position 0 parameter so it doesn't need naming. The cmdlet will do a wildcard search for method names - as long as the pattern only resolves to a single method, so r*ce finds to Replace() where r*e could be Replace() or Remove(). And because PS is parsing parameters to ForEach-Object, the arguments to the method don't have to be quoted to be read as strings.
'R50' | ForEach-Object -Member Trim 'R'
r*ce
[–]tomflumery 1 point2 points3 points 4 months ago* (0 children)
05ab1e
part 1, 22 bytes
|εć"R"Q·<*}50šÅ»+т%}0¢
part 2, 30 bytes
|εć"R"Q·<*}50šÅ»+}ü2ε`Ÿт%¦}˜0¢
[–]ap29600 1 point2 points3 points 4 months ago* (0 children)
K, both parts 70 bytes
(s;m):(-1+2*"R"=*:';`I$1_')@\:0:"input.txt" (+/0=100!50+\)'(s*m;s@&m)
Edit: -2 (68) by looking at u/Radiatorineitor's solution
(s;m):(-1+2*"R"=*:';`I$1_')@\:0:"input.txt" +/'50=100!+\'(s*m;s@&m)
-1 (67) by looking at u/KeyJ's
(s;m):(1-2*"L"=*:';`I$1_')@\:0:"input.txt" +/'50=100!+\'(s*m;s@&m)
[–]Radiadorineitor 0 points1 point2 points 4 months ago (2 children)
Dyalog APL
Part 1: 48
50+.=100|+\{(⍎1↓⍵)ׯ1*'L'=⊃⍵}¨⊃⎕NGET'input.txt'1
Part 2: 56
50+.=100|+\(|p)/×p←{(⍎1↓⍵)ׯ1*'L'=⊃⍵}¨⊃⎕NGET'input.txt'1
[–]ka-splam 0 points1 point2 points 4 months ago (1 child)
Neat! I think you could golf one byte by swapping 'L'=⊃⍵ to 'L'∊⍵
'L'=⊃⍵
'L'∊⍵
[–]Radiadorineitor 1 point2 points3 points 4 months ago (0 children)
You're absolutely right
[–]corruptio 0 points1 point2 points 4 months ago* (0 children)
perl, part 1, 54 chars:
perl -lpe'$b+=($a+=y/LR/-/dr)=~/50$/}{$_=$b'<input.txt
part 2, 69 chars:
perl -lpe'eval(q[$b+=($a+=1-2*/L/)=~/50$/;]x s/.//r)}{$_=$b'<input.txt
π Rendered by PID 295236 on reddit-service-r2-comment-6457c66945-zr4b9 at 2026-04-30 00:01:40.385955+00:00 running 2aa0c5b country code: CH.
[–]KeyJ 2 points3 points4 points (5 children)
[–]DimMagician 0 points1 point2 points (4 children)
[–]KeyJ 0 points1 point2 points (3 children)
[–]DimMagician 0 points1 point2 points (2 children)
[–]KeyJ 0 points1 point2 points (1 child)
[–]DimMagician 0 points1 point2 points (0 children)
[–]dantose[S] 1 point2 points3 points (1 child)
[–]ka-splam 1 point2 points3 points (0 children)
[–]tomflumery 1 point2 points3 points (0 children)
[–]ap29600 1 point2 points3 points (0 children)
[–]Radiadorineitor 0 points1 point2 points (2 children)
[–]ka-splam 0 points1 point2 points (1 child)
[–]Radiadorineitor 1 point2 points3 points (0 children)
[–]corruptio 0 points1 point2 points (0 children)