Time based One Time Password in #Jlang
#jlang #arrayprogramming #apljk
Why J? (The programming language) https://code.jsoftware.com/wiki/Essays/WhyJ
#jlang #apljk
d←{⌊5÷⍨⍵+⍺}
g←{⍵=0:''⋄r←5|⍵⋄r=3:'=',∇2d⍵⋄r=4:'-',∇1d⍵⋄(⍕r),∇0d⍵}
⌽1↓g+/∊{((⊂'210-='⍳⌽⍵)⌷3-⍳5)×5*⍳⍴⍵}¨⊃⎕NGET'input'1
It's over. It's done. Whew!
Not exactly elegant but it works. I might revisit it a later time!
#adventofcode #apljk #apl #AdventOfCode2022
/ Couldn't make it one this time, sorry
/ Read the jets
j:"><"?*0:"17.real"
/ 3 layers of air
a:((3,w:7)#0),
/ Define rock types
ts:({0 0,x,(-2+w-#:x)#0}(1_2\)@)''32\'31 10730 9519 101475 231
/ Check right and left for edges
cr:|/*|+:
cl:|/*+:
/ Push, accounting for collisions
p:{$[|//2=((#:n)# z)+n:{x_(x+#:y)# y}[((w!-1 1)*~(cr;cl)@\:x) y]'x;x;n]}
/ Try to drop one layer
d:{$[|//2=y+(h:#:y:p[y;z;x])#1_x;(y+h# x),h_x;x]}
Anyone up for another AoC in K live coding stream later today? Today's problem looks fun. https://www.twitch.tv/janstepien #apljk #apl #adventofcode #k
/ ...of course there was a better way
f:"15.txt"
n:2000000
c:"-",,/$!10
i:{2 2#.`c$(" "*~i)+x*i:+/c=\:x}'0:f
a:{x|-x}
d:{+/a x-y}
r:{x@&&/(&/~0>+x;&/~(2*n)<+x)}
f:{$[#i:&&/x@\:+y;y i;!0]}[{{d[z;y]>x}[d .x;*x;]}'i;]
(x;y):2#,//(1 1;1 -1;-1 1;-1 -1){f r@+(*y)+x*(,p),,|p:!2+d. y}\:/:i
y+4000000*x
/ Instead of scanning every row,
/ scan only the diamonds'
/ edges
/ Now it's 10 times faster,
/ down to 25s
/ Still, there must be a better way
#i #adventofcode #k #apljk #apl
f:"15.txt"
n:2000000
c:"-",,/$!10
i:{2 2#.`c$(" "*~i)+x*i:+/c=\:x}'0:f
a:{x|-x}
d:{+/a x-y}
m:{$[~~#:i:&x[0;1]>-2+1_x[;0]
(,x[0;0],|/x[0,1+*i;1]),x[(!#:x)^0,(1+*i)]
x]}
r:{cl:i@&{d[1_x;x[1],y]<1+*x}[;x]'(d.'i),'*+i;
r@<*+r:{x[1]+(-s)+0,2*s:x[0]-a y-x[2]}[;x]'(d.'cl),'*+cl}
--/*m/r[n]
x+4000000*1+*|*m/r[x:*&1<#'(m/r@)'!n*2]
/ Takes five minutes using
/ https://codeberg.org/ngn/k
/ There must be a better way
#k #apljk #apl #adventofcode #arrayprogramming
i:(`j?'1_)'0N 3#(,""),l:0:"13.txt"
p:{y,((0&(#:y)-#:x)#-0w)}
s:{(x>0)-x<0}
/ A platter of assorted comparison functions
FF:{$[x~y;0;v@*&~0=v:s@-/(p[x;y];p[y;x])]}
Ff:{FF[x;,y]}
fF:{FF[,x;y]}
ff:-s@-/
AF:FA:AA:{$[x~y;0;0=#:y;-1;0=#:x;1;v:c(*x;*y);v;o[1_x;1_y]]}
Af:{AF[x;,y]}
fA:{FA[,x;y]}
/ “Parametric Polymorphism”
c:{(.,/$@'x).x}
+/&0,0<c'i
d:`j?'("[[2]]";"[[6]]")
*/1 2+{+/0<{c(x;y)}[;x]',/i}'d
/ Try out with https://codeberg.org/ngn/k
/ #k #apljk #apl #adventofcode
i:{.'c@&~|/'">"=c:" "\x}'0:"14.txt"
h:2+|/*|+,/i
s:500
p:{(|/{x|-x}d:y-x)(((d>0)-d<0)+)\x}.
r:,/,/(p'2':)'i
f:{@[x;y[1];(y[0]=!#:*x)|]}
m:((h,2*s)# 0) f/r
c:{{v_((v:(n-y-1))+n:#:x)# x}[2 3 1,((-3+#*m)# 0);x]}
/ Keep simulating sand until we converge
t:{d:*&d=b:|/d:c[z]*~x[y+1];
$[~y<-1+#:x;l[x;y;z];~b;f[x;z,y];o[x;y+1;d]]}
a:{+//-m-t[;0;s]/m}
/ The last row behaviour is the only difference
l:{z;x}
a[]
l:{f[x;z,y]}
a[]
#k #apljk #apl #adventofcode #programming
l:{p,x,p}'f,l,f:,(#*l:0:"12.txt")#p:"z"
m:`i$(-"a")+{@[@[x;&"E"=x;{"z"}];&"S"=x;{"a"}]}'l
g:{2>x[m]-m}
sh:{y_(y+#x)#x}
(s;n):{sh[;x]}'1,-1+#l
(e;w):{sh[;x]'}'1,-1+#*l
i:1000
nc:{cn/cw/cs/ce/x}
/ Part one, from S
c:{x&t+i*0=t:z@g[y]*x+1}
ce:c[;e;w]
cw:c[;w;e]
cs:c[;s;n]
cn:c[;n;s]
|//("E"=l)*nc/i*~"S"=l
/ Part two, backwards from E
c:{x&t+i*0=t:g[y]*y@x+1}
ce:c[;e]
cw:c[;w]
cs:c[;s]
cn:c[;n]
&//(i*~r)+(r:l="a")*nc/i*~"E"=l
#x #l #k #apljk #apl #adventofcode #programming
/ read & parse
v:!#:i:0N 7#(,""),0:"11.txt"
s:(.18_)'i[;2]
n:19_'i[;3]
a:$*/.'d:(*|" "\)'i[;4]
t:|'(.*|)''i[;5 6]
/ simians → functions
sf:{."{[old](,w),,t[",($x),"]@~",(d x),"!w:",a,"!_(",n[x],")%rf}"}'v
/ turn, round, play
tr:{@[y;x;0#],'*'(t[0]@&)''(!#:y)=\:1_t:sf[x;y[x]]}
rn:{x{tr[y;x]}\!#:x}
pl:{*/t 2#>t:+/(m# v){y*x=v}'(m:(#:i)*x)##''h,,/(h:,s){y; rn@*|x}\!x}
/ relief factor and round no.
rf:3
pl 20
rf:1
pl 10000
#k #apljk #apl #adventofcode #arrayprogramming
/ The screen size
(h;w):s:6 40
/ The cycles that matter
c:20+w*!h
/ This parses our input
p:{$[x[3]="x";(`I$5_x),0;,0]}
/ Parts one and two
+/c*(r:+\1,,/p'0:"10.txt")c-2
(`0:" #"(!w){3>d*d:y-x}')'s# 1,r;
/ How about another live solving stream this Monday?
/ https://www.twitch.tv/janstepien
/ #k #apljk #apl #adventofcode #arrayprogramming #programming
#k #apljk #apl #adventofcode #arrayprogramming #programming
/ The screen size
(h;w):s:6 40
/ The cycles that matter
c:20+w*!h
/ This parses our input
p:{$[x[3]="x";(`I$5_x),0;,0]}
/ Parts one and two
+/c*(r:+\1,,/p'0:"10.real")c-2
(`0:" #"(!w){3>d*d:y-x}')'s# 1,r;
/ How about another live solving stream this Monday?
/ https://www.twitch.tv/janstepien
/ #k #apljk #apl #adventofcode #arrayprogramming #programming
#k #apljk #apl #adventofcode #arrayprogramming #programming
i:`I$''0:"8.txt"
/ external view from left side
v:{1_|1_|+1_|2_|+(x,\:0)>(|\0,)'x}
/ count trees on the edges
e:4*-1+#:i
/ count trees visible from the left side
m:|~0=!-1+#:i
l:{1+(*&)'~((m&1_)'(*'x)>x)}
/ count trees visible to the right in the
/ entire forest
f:{+(|!#:x)&{l@(y_(y+s:#:x)#)'x}[x]'!#:x}
/ invoke x with all four rotations of y
a:{(4-y)(+|:)/ x y(+|:)/i}
e++//|/a[v;]'!4
|//*/a[f;]'!4
/ #k #apljk #apl #adventofcode #arrayprogramming #programming
#k #apljk #apl #adventofcode #arrayprogramming #programming
e:{(" "\x) y}
a:{(,x,(,"/"),*y),y}
c:{n:e[y;2];@[x;0;$[n~,"/";{,,"/"};n~"..";1_;a[n]]]}
d:l:{y; x}
s:{.e[y;0]; @[x;1;((`$'*x)!(#*x)#.e[y;0])+]}
f:{."clds"@*&("clr"=x[2]),1}
d:(!0;`!!0){(f y)[x;y]}/i:0:"7.real"
+/v@&~100000<v:.*|d
g:40000000
t@*<t:-:g-r+t@&0<t:(.m)+g-r:(m:*|d)@`"/"
/ Fold the input lines using a dynamic dispatch
/ and keeping the directory sizes in a dictionary.
/ The language is growing on me.
/ #k #apljk #apl #adventofcode #arrayprogramming #programming
#k #apljk #apl #adventofcode #arrayprogramming #programming
This was a lot more straightfoward than day 5 :blobfoxdealwithit:
{1-⍨⍵+⍵⍳⍨≢¨⍵∪/⊃⊃⎕NGET'input'1}¨(4 14)
#adventofcode #apljk #apl #AdventOfCode2022
p←⊃⎕NGET'input'1
j←⊂{⊃' '(≠⊆⊢)⌽⍵}¨{4{⍵⌿⍨0=⍺|⍳≢⍵}(0 0,⍵)}↓⍉↑⌽8↑p
s←j,⍨⌽(⍎¨(⊂2 4 6)⌷' '(≠⊆⊢)⊣)¨10↓p
f←{c f t←⍺ ⋄ ((∊(g c↑⊃⍵[f]),⍵[t])(c↓⊃⍵[f])@t f)⍵}
g←⊢⋄⊃¨⊃f/s ⍝ Part 1
g←⌽⋄⊃¨⊃f/s ⍝ Part 2
This was trickier than I anticipated 😵
#adventofcode #apljk #apl #AdventOfCode2022
{x+(*&x=(#=)'x':)'0:"6.txt"}'4 14
/ Run it with either
/ https://codeberg.org/ngn/k
/ or
/ https://johnearnest.github.io/ok/
/ Watch the stream recording at
/ https://www.twitch.tv/videos/1671764330
#k #apljk #apl #adventofcode #arrayprogramming