Skip to content

Commit

Permalink
Tweaking Q solutions some more...
Browse files Browse the repository at this point in the history
  • Loading branch information
mkst committed May 24, 2020
1 parent 863b548 commit db54c12
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 63 deletions.
4 changes: 2 additions & 2 deletions 2015/10.q
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/--- Day 10: Elves Look, Elves Say ---

res:{raze {count[x],first x}each cut[where differ x;x]}\[50;]10 vs "J"$ first read0 `:input/10.txt;
res:{raze (1_deltas i,1),'x i:where differ x}\[50;]10 vs "J"$ first read0 `:input/10.txt
count res 40
/252594
count res 50
/3579328
/3579328
6 changes: 3 additions & 3 deletions 2015/11.q
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ increment:{.Q.a 26 vs 1+26 sv -97+"j"$x}
checkpass:{
$[not any "iol" in x;
$[1 in 1_deltas where 1=deltas "j"$x;
1<sum{ 1 in 1_deltas where x} each .Q.a=\:x;
1<sum{1 in 1_deltas where x} each .Q.a=\:x;
0b];
0b]
}

0N!res:{ increment x }/[{ not checkpass x };] first read0 `:input/11.txt;
0N!res:increment/[{not checkpass x};] first read0 `:input/11.txt
/vzbxxyzz
{ increment x }/[{ not checkpass x };increment res]
increment/[{not checkpass x};increment res]
/vzcaabcc
4 changes: 2 additions & 2 deletions 2015/13.q
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
a:`g`n`h xcols flip `g`h`n!("s i s";" ") 0: { -1_ssr[x;"lose ";"lose -"]} each read0`:input/13.txt

combos:{ (1+count x)#'{raze y,/:'x except/:y}[x;]/[count[x]-1;x] }
arrange:{ {[x;y] sum exec h from a where g in (x;y), n in (x;y) }.'-2#'prev\[count[x]-2;x] }
arrange:{ {sum exec h from a where g in x, n in x} each -2#'prev\[count[x]-2;x] }

max sum flip arrange each combos distinct (raze/)exec (g;n) from a
/618

/ add myself and re-run
max sum flip arrange each combos `me,distinct (raze/)exec (g;n) from a
/601i
/601i
2 changes: 1 addition & 1 deletion 2015/14.q
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ update p:0 from `f;

max sum r:flip 2503#'{(x[`t]#x`s),x[`r]#0i } each f
/2696
{ update p:p+1 from `f where x = max x } each sums r;
{ update p+1 from `f where x = max x } each sums r;
exec max p from f
/1084
4 changes: 2 additions & 2 deletions 2015/15.q
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ combos:{ { $[z=count x;enlist x,y;raze .z.s[;;z]'[x,/:t;y - t:til 1+y]] }[();x;y
ingredients:`ing xkey flip `ing`cap`dur`fla`tex`cal!("s i i i i i";" ")0: except[;":,"] each read0 `:input/15.txt
recipes:flip (0!ingredients)[`ing]!flip combos[100;4]

max { (a~abs a)*prd 4#a:sum { ingredients[x]*y }'[key x;value x] } each recipes
max { (a~abs a)*prd 4#a:sum { y*ingredients x }'[key x;value x] } peach recipes
/222870
max { (a[`cal]=500)*(a~abs a)*prd 4#a:sum { ingredients[x]*y }'[key x;value x] } each recipes
max { (a[`cal]=500)*(a~abs a)*prd 4#a:sum { y*ingredients x }'[key x;value x] } peach recipes
/117936
19 changes: 6 additions & 13 deletions 2015/18.q
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
/ --- Day 18: Like a GIF For Your Yard ---

o:g:"#"=read0 `:input/18.txt; // build grid
g:"#"=read0 `:input/18.txt; // build grid

c:t cross t:til count g; // build coordinates

i:{
s:sum .[g;] each (0 1;1 1;1 0;1 -1;0 -1;-1 -1;-1 0;-1 1)+\:x; / sum of neighbours
$[.[g;x]; / check state of current light
f:{
s:sum .[x;] each (0 1;1 1;1 0;1 -1;0 -1;-1 -1;-1 0;-1 1)+\:y; / sum of neighbours
$[.[x;y]; / check state of current light
s in 2 3;
s = 3]
};

do[100;g:(2#count g)#i each c];

sum raze g
sum raze { (2#count x)#f[x;] peach c }/[100;g]
/814

/ set corner lights on
Expand All @@ -26,10 +24,5 @@ sc:{
x
}

/ reset grid and set corners on
g:sc o;

do[100;g:sc (2#count g)#i each c];

sum raze g
sum raze { sc (2#count x)#f[x;] peach c }/[100;sc g]
/ 924
4 changes: 2 additions & 2 deletions 2015/19.q
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/--- Day 19: Medicine for Rudolph ---

r:r _ first where ""~/:r:read0 `:input/19.txt;
s:flip string ("s s";" ")0:-1_r;
r:r _ first where ""~/:r:read0 `:input/19.txt
s:flip string ("s s";" ")0:-1_r
count distinct raze { {[x;y;z;l] x:@[string x;y;:;z];raze x _/c#(y+c:count[l]-1) }[x;;z;y] each ss[x;y] }[last r;].'s
/518
Expand Down
4 changes: 2 additions & 2 deletions 2015/24.q
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ f:{
f2:{ f[x;1#y;y except first y] };

r:f2[sum[i]%3;] i:desc "J"$read0 `:input/24.txt;
(*/) r first iasc (*/) each r:r where c=min c:count each r
prd r first iasc prd each r:r where c=min c:count each r
/10439961859

r:f2[sum[i]%4;i];
(*/) r first iasc (*/) each r:r where c=min c:count each r
prd r first iasc prd each r:r where c=min c:count each r
/72050269
4 changes: 2 additions & 2 deletions 2017/13.q
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

i:flip "J"$": "vs/:read0 `:input/13.txt
f:@[(1+last l)#0N;l:first i;:;last i]
sum f[w]*w:where 0=(til count g) mod g:{2*x-1}f
sum w*f w:where 0=(til count g) mod g:{2*x-1}f
/1476
(2+)/[{max 0=(x+til count g) mod g};0] / slow, ~4s
(2+)/[{max 0=(x+til count g) mod g};0] / ~4s
/3937334
19 changes: 9 additions & 10 deletions 2017/14.q
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ sum raze h:raze each 0b vs''hash peach (first read0 `:input/14.txt),/:"-",'strin
/8214

v:(`u#enlist 0N 0N)!enlist 0N; / visited, add dummy value to setup key
g:0; / group id

f:{
if[not h[x;y];
f:{[x;id]
if[not h . x; / ignore 'off' bits
:()
];
if[(x;y) in key v;
if[x in key v; / ignore already visited
:()
];
v[(x;y)]:g;
.z.s[x+1;y]; / go right
.z.s[x-1;y]; / go left
.z.s[x;y+1]; / go up
.z.s[x;y-1]; / go down
v[x]:id;
.z.s[x+1 0;id]; / go right
.z.s[x-1 0;id]; / go left
.z.s[x+0 1;id]; / go up
.z.s[x-0 1;id]; / go down
};

til[128]{g+:1;f[x;y]}\:/:til 128;
f'[til[128] cross til 128;til 128*128];
-1+count distinct value v
/1093
19 changes: 6 additions & 13 deletions 2017/15.q
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
/--- Day 15: Dueling Generators ---

f:16807 48271
m:2147483647
c:0;
{
c+:1*(~/) -16#'0b vs'x:(f*x) mod m;
x
}/[40000000;f*i:first (" i";" ") 0: `:input/15.txt];
c
g:{ (first[x]+(~/)mod[r;65536];r:(16807 48271*last x) mod 2147483647) }/[40000000;]
first g (0;) i:first (" j";" ") 0: `:input/15.txt
/650

A:B:()

{ A,:$[0=mod[x:(first[f]*x) mod m;4];x;()];x }/[{ 5000000>count A };first i];
{ B,:$[0=mod[x:(last[f]*x) mod m;8];x;()];x }/[{ 5000000>count B };last i];
{ if[0=mod[x:(x*16807) mod 2147483647;4];A,:x mod 65536];x }/[{ 5000000>count A };first i];
{ if[0=mod[x:(x*48271) mod 2147483647;8];B,:x mod 65536];x }/[{ 5000000>count B };last i];

sum {x~y}'[(-16#'0b vs 'B);(-16#'0b vs'A)]
/336
sum A~'B
/336
18 changes: 8 additions & 10 deletions 2017/24.q
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
/--- Day 24: Electromagnetic Moat ---

bridge:{
w:y~\:2#-1#x;
res:.z.s'[x,/:y where w;y _/:where w],
.z.s'[x,/:y where a;y _/:where a:not[w] and (last last x)=y[;0]],
.z.s'[x,/:reverse each y where a;y _/:where a:not[w] and (last last x)=y[;1]];
$[count res;res;x]
};
f:{[bridge;options]
w1:where options[;0]=lc:last bridge; / options that match
w2:except[;w1] where options[;1]=lc; / options that match in reverse
if[0=count w1,w2;:enlist bridge]; / return if no further options

flat:{ (raze .z.s each x where t),x where not t:0h=type each x };
raze .z.s'[bridge,/:(options w1),reverse each options w2;options _/:w1,w2];
}

max sum each res:flat bridge[0;] "J"$"/"vs'read0 `:input/24.txt
max sum each res:f[0;] "J"$"/"vs'read0 `:input/24.txt
/1940
max sum each res where c=max c:count each res
/1928
/1928
2 changes: 1 addition & 1 deletion 2019/02.q
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ if[`02.q~.z.f;
I:"J"$"," vs first read0 `:input/02.txt;

-1@string first first f[@[I;1 2;:;12 2];0];

/8017076
N:0;V:0;T:19690720;W:1b;

while[T>R:first first f[@[I;1 2;:;(N;V)];0];N+:1];
Expand Down

0 comments on commit db54c12

Please sign in to comment.