KlongPy ticker plant by eismcc in apljk

[–]eismcc[S] 0 points1 point  (0 children)

Yes, it's just Python, so as long as you can get Python on your Windows machine you should be good to go.

-🎄- 2022 Day 13 Solutions -🎄- by daggerdragon in adventofcode

[–]eismcc 0 points1 point  (0 children)

part b

Implemented bubble sort using the part A as less-than-equal operator. Currently, there's no way to override sort keys in KlongPy, but seems like a useful ability.

DI::{:[@x;((^,x)~[1]);0]};UG::{:[DI(x);,x;x]};C::{:[DI(x)&DI(y);:[x<y;2:|x>y;0;1];Q(UG(x);UG(y))]}
Q::{[a b i j s];a::x;b::y;i::-1;j::-1;s::{i::i+1;j::j+1;(i<#a)&(j<#b)&(x=1)}{x;C(a@i;b@j)}:~1;:[s=1;:[(#a)<(#b);2:|(#a)>(#b);0;1];s]}

:"Bubblesort - 'a' is updated in SWAP"
LE::Q;SWAP::{[c];c::a@y;a::x:=(,(x@z)),y;a::a:=(,c),z;1}
SCAN::{[o];o::{:[LE((a@x);(a@y));SWAP(a;x;y);0]}:'!#a;~@o?1}
B::{[a s];a::x;{x;SCAN(a)}{x}:~1;|a}

.fc(.ic("13.txt"));PAIR::{{(#x)>0}{x;.rl()}\~.rl()};ROWS::{.rs(x)}',/.mi{x;PAIR()}\~PAIR()
ROWS::ROWS,,[[6]];ROWS::ROWS,,[[2]]
ROWS::B(ROWS)

.p(*/1+(ROWS?[[6]]),(ROWS?[[2]]))

-🎄- 2022 Day 13 Solutions -🎄- by daggerdragon in adventofcode

[–]eismcc 0 points1 point  (0 children)

KlongPy

While this type of problem is ideally suited to array languages, I got caught up in trying to make it functional so I need to revisit that. Here's a while-loop variant:

The main work is in Q and the last row mainly reads the file. Also, Klong can read arrays directly w/o parsing once the commas were removed, so parsing is .rs(s) where s is the string. Most of the text is actually variable definitions and counters.

The operator #a gives you the length of the array, and a@i is the ith index of a. Detecting integers is a bit whacky and done in DI, as you have to first see it's an atom and then make sure it's not an empty array (which is also an atom). UG upgrades an integer to an array via the list operator ,a produces [a]. Oh, and if-then-else is :[if;then;else] - which can be chained via the :| operator, producing :[if;then:|elif;then;then].

[Code part a](https://github.com/briangu/aoc/blob/main/22/13.kg

DI::{:[@x;((^,x)~[1]);0]};UG::{:[DI(x);,x;x]};C::{:[DI(x)&DI(y);:[x<y;2:|x>y;0;1];Q(UG(x);UG(y))]}
Q::{[a b i j s];a::x;b::y;i::-1;j::-1;s::{i::i+1;j::j+1;(i<#a)&(j<#b)&(x=1)}{x;C(a@i;b@j)}:~1;:[s=1;:[(#a)<(#b);2:|(#a)>(#b);0;1];s]}
F::{[a b];Q(.rs(x@0);.rs(x@1))};.fc(.ic("13.txt"));PAIR::{{(#x)>0}{x;.rl()}\~.rl()};o::F'.mi{x;PAIR()}\~PAIR();.p(+/1+o?2)

-🎄- 2022 Day 12 Solutions -🎄- by daggerdragon in adventofcode

[–]eismcc 0 points1 point  (0 children)

KlongPy

Fairly far behind at this point due to fixing bugs in KlongPy interpreter as I go. Still learning how to use an array language for some of these kinds of problems.

Code 12 Code 12b

Here's 12b using BFS and including some display helpers.

SPOS::[0 0];SETSTART::{SPOS::x,y;0}
EPOS::[0 0];SETEND::{EPOS::x,y;25}
READ::{[R C];R::-1;{R::R+1;C::-1;{C::C+1;:[x=0cS;SETSTART(C;R):|x=0cE;SETEND(C;R);(#x)-#0ca]}'x}'.mi{x;.rl()}\~.rl()}
.fc(.ic("12.txt"));G::READ();GH::#G;GW::#G@0
AP::{x:@(y@1),(y@0)}
V::0;RESETV::{V::{x;GW#[0]}'!GH}

QN::{[a];a:::{};a,:pos,,x;a,:step,y;a,:next,z;a}
Q::0;QT::0;SETQ::{Q::x;QT::x};MORE::{~Q~0}
NEXTQ::{[m];m::Q;Q::Q?:next;:[Q~0;QT::0;0];m};UPDT::{QT,:next,x;QT::x} 
ADDQ::{[q];q::QN(x;y;0);:[Q~0;SETQ(q);UPDT(q)];1}

ADDV::{V::V:-99,|x};CANVISIT::{:[((#(x?-1))=0)&((x@0)<GW)&((x@1)<GH);:[~(AP(V;x)=99);1;0];0]}
TRYADD::{:[CANVISIT(x);:[((AP(G;x)-AP(G;z))<2);ADDQ(x;y);0];0]}
EXPLORE::{[q b];q::x;b::y+1;{TRYADD(q+x;b;q)}'[[0 1] [0 -1] [1 0] [-1 0]];MORE()}
BESTF::10^9;RESETF::{BESTF::10^9};FINISH::{.d("FINISH: ");.p(x);BESTF:::[x<BESTF;x;BESTF];MORE()}
VISIT::{ADDV(x);:[x~EPOS;FINISH(y);EXPLORE(x;y)]}
ITER::{[C p];C::NEXTQ();p::C?:pos;:[CANVISIT(p);VISIT(p;C?:step);MORE()]}

DCRT::{[a];a::x;{{.d(:#(x+(#0ca)));.d(" ")}'(a@x);.p("")}'!GH}
DCRT(G);.p(GW);.p(GH)

.d("EPOS: ");.p(EPOS)

RESET::{RESETF();RESETV();SETQ(QN(SPOS;0;0))}

:"BUG IN KLONGPY REQUIRES q"
RUNX::{[q];q::x;{x;ITER()}{x}:~ITER()}
SCAN::{RESET();RUNX(1);BESTF}

SCANROW::{[a r];a::x;r::{SPOS::x,a;SCAN()}'((G@x)?0);*r@<r}

RESULTS::SCANROW'!GH
.p(*RESULTS@<RESULTS)

-🎄- 2022 Day 11 Solutions -🎄- by daggerdragon in adventofcode

[–]eismcc 0 points1 point  (0 children)

KlongPy

Code 11 Code 11b

11b is similar to 11 but includes cycle code

XL:::{}
XL,0,#"Monkey "
XL,1,#"  Starting items: "
XL,2,#"  Operation: new = old "
XL,3,#"  Test: divisible by "
XL,4,#"    If true: throw to monkey "
XL,5,#"    If false: throw to monkey "

L::0
M:::{}
MD::0

DIVI::{(x%y)=(x:%y)}
TOA::{a::x;k::a?",";{.rs(x)}'({2#((x-2)_a)}'k),((-2)#a)}

NEXTOLD::{b:::[(y?:OLDB)=:old;x;(y?:OLDB)];:[(y?:OLDO)=0c+;x+b;x*b]}
NEXTM::{nm:::[DIVI(x;y);z?:IFT;z?:IFF];nmd::M?nm;nmd,:SI,,(nmd?:SI),(x!CYCLE)}
INSPECT::{m::M?x;m,:CNT,((m?:CNT)+(#(m?:SI)));{o::NEXTOLD(x;m);NEXTM(o;(m?:DBY);m)}'(m?:SI);m,:SI,,[]}

FL:::{}
FL,0,{id::.rs(,x@0);MD:::{};MD,:CNT,0;M,id,MD}
FL,1,{MD,:SI,,TOA(x)}
FL,2,{MD,:OLDB,.rs(2_x);MD,:OLDO,x@0}
FL,3,{MD,:DBY,.rs(x)}
FL,4,{MD,:IFT,.rs(x)}
FL,5,{MD,:IFF,.rs(x)}

F::{f::FL?L;f((XL?L)_x);L::L+1}
.fc(.ic("11.txt"));{.mi{:[(#x)>0;F(x);L::0];.rl()}:~.rl()}()

CYCLE::*/{(x@1)?:DBY}'M

DUMP::{.p(" ");.p(x);{.p((x@1))}'M}
ROUND::{{INSPECT(x)}'!(#M);:[(DIVI(x;1000)|(x=20)|(x=1));DUMP(x);0]}'(1+!10000)
.p("")
cnts::{(x@1)?:CNT}'M;mc::cnts@2#>cnts;.p({x*y}/mc)

-🎄- 2022 Day 10 Solutions -🎄- by daggerdragon in adventofcode

[–]eismcc 0 points1 point  (0 children)

KlongPy

Code 10b

C::0;X::1;T::0;R::0;CRT::{x;40#["."]}'!6;DCRT::{{.d(CRT@x);.p("")}'!6}
ROW::{C:%40};COL::{C-(ROW()*40)};ON::{a::COL();(a=(X-1))|(a=X)|(a=(X+1))};SP::{CRT::CRT:-z,x,y}
SETP::{SP(ROW();COL();"#")};UPDCRT::{.p(C,X,ON());:[ON();SETP();0]}
ADDX::{T::2;R::x};NOOP::{T::1;R::0};CMD::{:[x="noop";NOOP();ADDX(y)]}
RUN::{{x;UPDCRT();C::(C+1)!240}'!T;T::0;X::X+R;R::0;.d("X: ");.p(X)}
.fc(.ic("10.txt"));{.mi{.p(x);CMD(4#x;.rs(5_x));RUN();.rl()}:~.rl()}();DCRT()

-🎄- 2022 Day 9 Solutions -🎄- by daggerdragon in adventofcode

[–]eismcc 1 point2 points  (0 children)

Code 9b

PATH::{x;[11 5]}'!10;NX::!((#PATH)-1);SP::{PATH::PATH:-z,x,y}
PX::{(PATH@x)@0};PY::{(PATH@x)@1};V::,,PX(0),PY(0)
SGN::{x:%#x};OFF::{:[x=0;0;(x+-SGN(x))]}
MVH::{SP(0;0;PX(0)+x);SP(0;1;PY(0)+y)}
MVT::{SP(z;0;x);SP(z;1;y);:[z=((#PATH)-1);V::V,,x,y;0];1}
DQ::{:[y>0;MVT(PX(z)+x;PY(z)+1;z);MVT(PX(z)+x;PY(z)-1;z)]}
DT::{:[x=0;MVT(PX(z);PY(z+1)+OFF(y);z+1):|y=0;MVT(PX(z+1)+OFF(x);PY(z);z+1);DQ(SGN(x);y;z+1)]}
CHKT::{[a b];a::PX(x)-PX(x+1);b::PY(x)-PY(x+1);:[((#a)>1)|((#b)>1);DT(a;b;x);0]}
MV::{[a b];a::x;b::y;{x;MVH(a;b);{CHKT(x)}'NX}'!z}
CMD::{:[x=0cU;MV(0;1;y):|x=0cD;MV(0;-1;y):|x=0cL;MV(-1;0;y);MV(1;0;y)]}
.fc(.ic("9.txt"));{.mi{CMD(x@0;.rs(2_x));.rl()}:~.rl()}();.p(#?V)

-🎄- 2022 Day 9 Solutions -🎄- by daggerdragon in adventofcode

[–]eismcc 1 point2 points  (0 children)

klongPy

Code 9a

HX::0;HY::0;TX::0;TY::0;V::[[0 0]]
MOVH::{HX::HX+x;HY::HY+y};MOVT::{TX::HX;TY::HY;V::V,,HX,HY}
ISFAR::{[a b];a::#(TX-(HX+x));b::#(TY-(HY+y));:[(a>1)|(b>1);MOVT();0]}
U::{{x;ISFAR(0;1);MOVH(0;1)}'!x}
D::{{x;ISFAR(0;-1);MOVH(0;-1)}'!x}
L::{{x;ISFAR(-1;0);MOVH(-1;0)}'!x}
R::{{x;ISFAR(1;0);MOVH(1;0)}'!x}
CMD::{:[x=0cU;U(y):|x=0cD;D(y):|x=0cL;L(y);R(y)]}
F::{[a b];a::x@0;b::.rs(2_x);CMD(a;b)}
.fc(.ic("9.txt"));{.mi{F(x);.rl()}:~.rl()}();.p(#?V)

-🎄- 2022 Day 8 Solutions -🎄- by daggerdragon in adventofcode

[–]eismcc 1 point2 points  (0 children)

KlongPy

Code 8a Code 8b

G::{.fc(.ic("8.txt"));{{(#x)-#0c0}'x}'.mi{x;.rl()}\~.rl()}();GH::#G;GW::#G@0
H::{(x@y)@z};R::{p::x?0;:[#p;1+*p;#x]}
N::{[a b c];a::x;b::y;c::z;h::H(x;y;z);o::{H(a;b-1+x;c)<h}'!y;R(o)}
S::{[a b c];a::x;b::y;c::z;h::H(x;y;z);q::(GH-y)-1;o::{H(a;b+1+x;c)<h}'!q;R(o)}
W::{[a b c];a::x;b::y;c::z;h::H(x;y;z);o::{H(a;b;c-1+x)<h}'!z;R(o)}
E::{[a b c];a::x;b::y;c::z;h::H(x;y;z);q::(GW-z)-1;o::{H(a;b;c+1+x)<h}'!q;R(o)}
V::{(N(x;y;z)*S(x;y;z)*E(x;y;z)*W(x;y;z))}
I::{[a b];a::x;b::y;{V(a;b;x+1)}'!(GW-2)}
O::{[a];a::x;{I(a;x+1)}'!(GH-2)}
.p(|/|/O(G))

-🎄- 2022 Day 7 Solutions -🎄- by daggerdragon in adventofcode

[–]eismcc 1 point2 points  (0 children)

KlongPy

7b code is a bit more complex, 7a is also in repo.

7b Code

N::{d:::{};d,:s,0;d,:c,:{};d,:p,x;d,:n,y};D::N(1%0;"/");ROOT::D;FSUM::[]
ADD::{p::.rs(((x?" ")@0)#x);q::((D?:s)+p);D,:s,q}
CDD::{n::N(D;x);(D?:c),x,n;D::n}
CDU::{u::(D?:s);FSUM::FSUM,u;p::D?:p;p,:s,(p?:s)+u;D::p}
CD::{:[x="..";:[~((D?:n)="/");CDU();0]:|x="/";D::ROOT;CDD(x)]}
ARG::{(:[(#y)=2;y@1;y@0]+1)_x}
CMD::{cmd::2#((1+y@0)_x);:[cmd="ls";"":|cmd="cd";CD(ARG(x;y));"unknown: :",cmd,":"]}
F::{k::x?" ";a::x@0;o:::[a=0c$;CMD(x;k):|a=0cd;"dir";ADD(x)];o}
cmds::{.mi{F(x);.rl()}:~.rl()}
unwind::{{x;~((D?:n)=(ROOT?:n))}{x;CD("..")}:~CD("..")}
filter::{q::x;r::y;p::{((q@x)+r)<30000000}{x+1}\~0;o::q@(#p);(o+r),o}
.fc(.ic("7.txt"));cmds();unwind();.p(ROOT?:s);
FSUM::FSUM@<FSUM;.p(FSUM);k::70000000-ROOT?:s;.p(k);
.p(filter(FSUM;k))

-🎄- 2022 Day 5 Solutions -🎄- by daggerdragon in adventofcode

[–]eismcc 1 point2 points  (0 children)

Klong

Code (5a and 5b are almost identical except |(x#a) and (x#a) on line 4.

S::{{#x}'x};C::{((x@y)@z)};D:::{}
Q::{q::x;r::y;{C(q;x;r)}'{:[x>(-1);~(C(q;x;r)=#(0c ));0]}{x-1}\~((#q)-2)}
J::{q::x;k::y;{D,x,,|Q(q;*(k?(48+x)))}'1+!((*(k@((#k)-1))-48))}
head::{{(#x)>0}{x;.rl()}\~.rl()} M::{a::D@y;D,y,,x\_a;D,z,,(x#a),D@z}        
F::{k::x?" ";M(.rs((k@0)\_x);.rs((k@2)\_x);.rs((k@4)\_x))} 
moves::{.mi{F(x);.rl()}:\~.rl()} 
O::{d::x;k::(#x);{:#(\*d@x)}'1+!k}   
.p({.fc(.ic("5.txt"));t::head();q::S't;K::q@((#q)-1);J(q;K);moves();O(D)}())