Skip to content

Commit f37186d

Browse files
committed
2022.14
1 parent 21b9118 commit f37186d

File tree

3 files changed

+218
-0
lines changed

3 files changed

+218
-0
lines changed

2022/14.q

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// --- Day 14: Regolith Reservoir ---
2+
3+
i:raze {(-1+count c)#2#'next\[c:" -> " vs x]} each read0 `:input/14.txt
4+
5+
range:{x + til 1 + y - x}
6+
7+
f:{
8+
xx:reverse each "J"$"," vs'x; // flip x/y
9+
l:first xx;
10+
r:last xx;
11+
:$[l[0]=r[0];
12+
l[0],/:range . asc (l 1;r 1);
13+
(range . asc (l 0;r 0)),\:r[1]
14+
];
15+
}
16+
17+
ii:asc distinct raze f each i
18+
19+
maxy: max ii[;0]
20+
21+
// add flooring
22+
ii,:(maxy+2),/:250 + til 500 // should base this on 2*maxy?
23+
24+
cave:{[x;y]
25+
x[y]:"#";
26+
:x;
27+
}/[(`u#enlist[0N 0N])!enlist"X";ii]
28+
29+
pf:{[pt2;x]
30+
if[1 = count x;:x]; // break iteration
31+
32+
$[pt2;
33+
if[cave[0 500]="o";:0b]; // reached the top (part2)
34+
if[maxy = x 0;:0b] // reached the floor (part1)
35+
];
36+
37+
$[(cave 1 0+x) = " "; // empty space?
38+
:1 0+x; // keep going down
39+
(cave 1 -1+x) = " "; // empty space on diagonal left?
40+
:1 -1+x; // go diagonal left
41+
(cave 1 1+x) = " "; // empty space on diagonal right?
42+
:1 1+x; // go diagonal right
43+
[cave[x]:"o";:1b] // place some sand and we're done
44+
];
45+
};
46+
47+
{pf[0b]/[0 500]}/[{x};1];
48+
sum"o"=value cave
49+
/719
50+
51+
{pf[1b]/[0 500]}/[{x};1];
52+
sum"o"=value cave
53+
/23390

0 commit comments

Comments
 (0)