Skip to content

Commit

Permalink
further tweaks to Q solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
mkst committed Jun 4, 2020
1 parent db54c12 commit 9e6f24b
Show file tree
Hide file tree
Showing 24 changed files with 144 additions and 426 deletions.
4 changes: 2 additions & 2 deletions 2015/01.q
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

last s:sums -1 1"("=first read0 `:input/01.txt
/232
1+first where -1=s
/1783
1+s?-1
/1783
8 changes: 4 additions & 4 deletions 2015/02.q
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/--- Day 2: I Was Told There Would Be No Math ---

d:asc each "J"$ "x" vs'read0 `:input/02.txt
sum sum each (2*d[;2]*d[;0];3*d[;0]*d[;1];2*d[;1]*d[;2])
t:flip `w`h`l!flip asc each "J"$ "x" vs'read0 `:input/02.txt
exec sum (2*w*l) + (3*w*h) + 2*h*l from t
/1598415
sum sum each (d[;0]*d[;1]*d[;2];2*d[;0]+d[;1])
/3812909
exec sum (w*h*l) + (2*w+h) from t
/3812909
6 changes: 3 additions & 3 deletions 2015/03.q
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/--- Day 3: Perfectly Spherical Houses in a Vacuum ---

count distinct sums D:!["^v<>";(0 1;0 -1;-1 0;1 0)] first read0 `:input/03.txt
count distinct sums d:(0 1;0 -1;-1 0;1 0)"^v<>"?first read0 `:input/03.txt
/2572
count distinct (sums D where not o),sums D where o:count[D]#10b
/2631
count distinct raze sums 0N 2#d
/2631
4 changes: 2 additions & 2 deletions 2015/04.q
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/--- Day 4: The Ideal Stocking Stuffer ---

f:{not (16>a 2)&0 0~2#a:"j"$md5 x,string y }[r:first read0 `:input/04.txt;]
g:{not 0x000000~3#md5 x,string y }[r;]
f:{not (16>a 2)&0 0~2#a:"j"$md5 x,string y}[r:first read0 `:input/04.txt;]
g:{sum 3#md5 x,string y}[r;]
(1+)/[f;0]
/254575
(1+)/[g;0]
Expand Down
6 changes: 3 additions & 3 deletions 2015/05.q
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

sw:{neg[x-1]_flip next\[x-1;y]} / sliding window

sum { (0 in deltas "j"$x)&(0N~first raze x ss/:("ab";"cd";"pq";"xy")) & 2<sum x in "aeiou" } each w:read0 `:input/05.txt
sum {(any x=prev x) and (0=count raze x ss/:("ab";"cd";"pq";"xy")) and 2<sum x in "aeiou"} each w:read0 `:input/05.txt
/238
sum{(0<sum("..",x)=x,"__")&1<last[w]-first w:where 1<sum {x~/:\:x}sw[2;x]} each w
/69
sum {(any x=2 xprev x) and 1<sum 1_deltas where 1<sum {x~\:/:x}sw[2;x]} each w
/69
36 changes: 36 additions & 0 deletions 2015/07.q
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/--- Day 7: Some Assembly Required ---

.a.wires:(`u#enlist `)!enlist 0N

.a.get:{ $[all x in .Q.n;"J"$x;.a.wires `$x] }
.a.not:{ $[null r:.a.get x;0N;0b sv (48#0b),not -16#0b vs r] }
.a.and:{ $[any null r:.a.get each (x;y);0N;0b sv all 0b vs'r] }
.a.or:{ $[any null r:.a.get each (x;y);0N;0b sv any 0b vs'r] }
.a.ls:{ "j"$(2 xexp .a.get y) * .a.get x }
.a.rs:{ .a.get[x] div (2 xexp .a.get y) }

f:{
if[not null .a.wires y;:()];
.a.wires[y]:$[1=count x;
.a.get x 0;
"NOT"~x 0;
.a.not x 1;
"AND"~x 1;
.a.and . x 0 2;
"OR"~x 1;
.a.or . x 0 2;
"LSHIFT"~x 1;
.a.ls . x 0 2;
.a.rs . x 0 2
]
};

i:{(" "vs x;`$y)}.'" -> "vs/:read0 `:input/07.txt

while[null .a.wires`a;f .'i];a:.a.wires`a;a
/16076

delete from `.a.wires;
i[where i[;1]=`b;0]:enlist enlist string a
while[null .a.wires`a;f .'i];a:.a.wires`a;a
/2797
6 changes: 3 additions & 3 deletions 2015/08.q
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/--- Day 8: Matchsticks ---

sum { count[x]-count 2_ssr[ssr[ssr[x;"\\x[0-9a-f][0-9a-f]";"_"];"\\\\";"_"];"\\\"";"_"] } each r:read0 `:input/08.txt
sum { count[x]-count 2_ ssr[ssr[x;"\\x[0-9a-f][0-9a-f]";"_"];"\\?";"_"] } each r:read0 `:input/08.txt
/1350
sum { count[except[.Q.s x;"\r\n"]]-count x} each r
/2085
sum { count[.Q.s1 x]-count x} each r
/2085
2 changes: 1 addition & 1 deletion 2015/10.q
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/--- Day 10: Elves Look, Elves Say ---

res:{raze (1_deltas i,1),'x i:where differ x}\[50;]10 vs "J"$ first read0 `:input/10.txt
res:{raze (1_deltas i,count x),'x i:where differ x}\[50;]10 vs "J"$ first read0 `:input/10.txt
count res 40
/252594
count res 50
Expand Down
8 changes: 3 additions & 5 deletions 2015/14.q
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/--- Day 14: Reindeer Olympics ---

f:flip `n`s`t`r!("s i i i";" ")0: `:input/14.txt
update p:0 from `f;
t:flip `n`s`t`r!("s j j j";" ")0: `:input/14.txt
max sum r:flip 2503#'{(x[`t]#x`s),x[`r]#0i } each f
max last f:flip value exec sums 2503#((t#s),r#0) by n from t
/2696
{ update p+1 from `f where x = max x } each sums r;
exec max p from f
max sum each flip f=max flip f
/1084
20 changes: 9 additions & 11 deletions 2015/17.q
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
/--- Day 17: No Such Thing as Too Much ---

TARGET:150
B:(count[i]#.Q.a)!i:desc "J"$ read0 `:input/17.txt

buckets:(count[i]#.Q.a)!i:desc "J"$ read0 `:input/17.txt

f:{[current;available;indexes]
// target reached, return
if[TARGET=sum buckets indexes;:enlist indexes];
// filter available
available:available where buckets[available] <= min buckets[indexes];
f:{
// success
if[x=s:sum B y;:enlist y];
// discover possibilities
w:where (z > last y) and x >= B[z] + s;
// recurse
raze .z.s'[c w;available _/:w;indexes,/:available w:where 0<=c:current-/:buckets[available]]
raze .z.s'[x;y,/:z w;z _/:w]
}

count c:distinct asc each f[TARGET;key buckets;()]
count r:f[150;"";key B]
/4372
min count each c
min count each r
/4
18 changes: 8 additions & 10 deletions 2015/24.q
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
/ --- Day 24: It Hangs in the Balance ---
/--- Day 24: It Hangs in the Balance ---

f:{
// success
if[x=s:sum y;:enlist y];
// discover possibilities
p:z where z <= x - s;
// optimisation to avoid recalculations
p:p where all p</:y;
w:where (z<last y) and x >= z + s;
// recurse
raze .z.s'[x;y,/:p;z except/:p]
raze .z.s'[x;y,/:z w;z _/:w]
};

f2:{ f[x;1#y;y except first y] };
f2:{ f[x;1#y;1_ y] };

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

r:f2[sum[i]%4;i];
prd r first iasc prd each r:r where c=min c:count each r
r:f2[sum[i] div 4;i];
min prd each r:r where c=min c:count each r
/72050269
Loading

0 comments on commit 9e6f24b

Please sign in to comment.