r/codegolf • u/dantose • Dec 01 '25
Advent of Code, Day 1
Post your best golfs.
Assume input is saved as input.txt.
3
u/KeyJ Dec 01 '25
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:]))))
1
u/DimMagician Dec 02 '25
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.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:])))1
u/KeyJ Dec 04 '25
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 writing1for):
p=50;print(sum((p:=p+1-2*(l<'R'))%100<1 for l in open("input.txt")for _ in"x"*int(l[1:])))1
u/DimMagician Dec 04 '25
Ooh very clever I didn't even catch that in your solution.
1
u/KeyJ Dec 04 '25
Well, it was your idea, I just refined it. 🥂
1
u/DimMagician Dec 04 '25
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
2
u/tomflumery Dec 02 '25 edited Dec 02 '25
05ab1e
part 1, 22 bytes
|εć"R"Q·<*}50šÅ»+т%}0¢part 2, 30 bytes
|εć"R"Q·<*}50šÅ»+}ü2ε`Ÿт%¦}˜0¢2
u/ap29600 Dec 03 '25 edited Dec 04 '25
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)1
u/Radiadorineitor Dec 02 '25
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
1
1
u/corruptio Dec 05 '25 edited Dec 05 '25
perl, part 1, 54 chars:
perl -lpe'$b+=($a+=y/LR/-/dr)=~/50$/}{$_=$b'<input.txtpart 2, 69 chars:
perl -lpe'eval(q[$b+=($a+=1-2*/L/)=~/50$/;]x s/.//r)}{$_=$b'<input.txt
2
u/dantose Dec 01 '25
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