Skip to content

Commit 6393121

Browse files
committed
had to do this one late. part 1: 7:13, part 2: 9:00. took a while to debug an error copilot made in part 1 (it didn't do the math.min/math.max). That delta time to part 2 would have gotten me a spot on the leaderboard had I done part 1 faster. copilot made that - I just typed function drawLine() and it filled it out for me and I just had to change a const to a let and update the board.set() call. it even supports non perfectly-diagonal lines. fancy.
glad I didn't actually qualify for a leaderboard position today because I'd've been mad that I couldn't do it on time
1 parent a990aa8 commit 6393121

File tree

8 files changed

+852
-0
lines changed

8 files changed

+852
-0
lines changed

2021/solutions/day5/day5.1.ts

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
input: string, lines: string[], dblines: string[][]
3+
copy(text: string) → clipboard
4+
error(message: string) → thrown error
5+
-5..mod(3) → @mod(-5, 3)
6+
*/
7+
8+
// Cardinals:
9+
// [[1,0],[-1,0],[0,1],[0,-1]]
10+
// +Diagonals:
11+
// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]]
12+
13+
export {};
14+
15+
const practice = `
16+
0,9 -> 5,9
17+
8,0 -> 0,8
18+
9,4 -> 3,4
19+
2,2 -> 2,1
20+
7,0 -> 7,4
21+
6,4 -> 2,0
22+
0,9 -> 2,9
23+
3,4 -> 1,4
24+
0,0 -> 8,8
25+
5,5 -> 8,2
26+
`;
27+
// input = practice;
28+
29+
const board = makeBoard(0);
30+
input.trim().split("\n").forEach(line => {
31+
let [start, end] = line.split(" -> ");
32+
let [sx, sy] = start.split(",").map(Number);
33+
let [ex, ey] = end.split(",").map(Number);
34+
if(!(sx == ex || sy == ey)) return;//throw new Error("can only move in one direction");
35+
36+
console.log(line);
37+
for(let y = Math.min(sy, ey); y <= Math.max(sy, ey); y++) {
38+
for(let x = Math.min(sx,ex); x <= Math.max(sx, ex); x++) {
39+
board.set([x, y], board.get([x, y]) + 1);
40+
}
41+
}
42+
});
43+
44+
board.print(v => v === 0 ? "." : "" + v).dwth(log);
45+
46+
let res = 0;
47+
board.forEach(v => {
48+
if(v >= 2) res += 1;
49+
});
50+
res.dwth(log);
51+
52+
53+
54+
55+
type nobi = number | bigint;
56+
type Board<T> = {
57+
get(pos: Point2D): T;
58+
set(pos: Point2D, t: T): void;
59+
clear(): void;
60+
forEach(visitor: (v: T, pos: Point2D) => void): void;
61+
print(printer?: (v: T, pos: Point2D) => string | nobi): string;
62+
copy(): Board<T>;
63+
};
64+
function makeBoard<T>(fill: T): Board<T> {
65+
// it would be useful if board could center at 0,0 and expand infinitely
66+
let board: T[][] = [];
67+
let limits:
68+
| { min: Point2D, max: Point2D }
69+
| undefined;
70+
let reso: Board<T> = {
71+
clear: () => {
72+
board = [];
73+
},
74+
get: (pos) => {
75+
if (!limits) return fill;
76+
if (
77+
pos.op(limits.min, (a, b) => a < b).some(w => w) ||
78+
pos.op(limits.max, (a, b) => a > b).some(w => w)
79+
) return fill;
80+
if (!board[pos.y]) return fill;
81+
let bval = board[pos.y][pos.x];
82+
return bval === undefined ? fill : bval;
83+
},
84+
set: (pos, v) => {
85+
if (!limits) {
86+
limits = {min: dupe(pos), max: dupe(pos)};
87+
}
88+
limits.min = pos.op(limits.min, (a, b) => Math.min(a, b));
89+
limits.max = pos.op(limits.max, (a, b) => Math.max(a, b));
90+
if (!board[pos.y]) board[pos.y] = [];
91+
board[pos.y][pos.x] = v;
92+
},
93+
forEach: visitor => {
94+
if (!limits) return;
95+
for (let y = limits.min.y; y <= limits.max.y; y++) {
96+
for (let x = limits.min.x; x <= limits.max.x; x++) {
97+
visitor(reso.get([x, y]), [x, y]);
98+
}
99+
}
100+
},
101+
copy: () => {
102+
let nb = makeBoard<T>(fill);
103+
reso.forEach((v, pos) => nb.set(pos, v));
104+
return nb;
105+
},
106+
print: (printer = v => v as any): string => {
107+
// ratelimit print
108+
if (!limits) return "*no board to print*";
109+
let ylength = 0;
110+
for (let y = limits.min.y - 1; y <= limits.max.y + 1; y++) {
111+
ylength = Math.max(y.toString().length, ylength);
112+
}
113+
const resc: string[] = [];
114+
let llen: number = limits.max.x - limits.min.x + 3;
115+
for (let y = limits.min.y - 1; y <= limits.max.y + 1; y++) {
116+
let line = "";
117+
for (let x = limits.min.x - 1; x <= limits.max.x + 1; x++) {
118+
line += printer(reso.get([x, y]), [x, y]);
119+
}
120+
if(line.length > llen) llen = line.length;
121+
resc.push(y.toString().padStart(ylength, " ") + " | " + line + " |");
122+
}
123+
resc.unshift(
124+
" ".repeat(ylength) +
125+
" .-" +
126+
"-".repeat(llen) +
127+
"-. " + (limits.min.y - 1) + " .. " + (limits.max.y + 1),
128+
);
129+
resc.push(
130+
" ".repeat(ylength) +
131+
" '-" +
132+
"-".repeat(llen) +
133+
"-'",
134+
);
135+
return resc.join("\n");
136+
},
137+
};
138+
return reso;
139+
}

2021/solutions/day5/day5.2.ts

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
input: string, lines: string[], dblines: string[][]
3+
copy(text: string) → clipboard
4+
error(message: string) → thrown error
5+
-5..mod(3) → @mod(-5, 3)
6+
*/
7+
8+
// Cardinals:
9+
// [[1,0],[-1,0],[0,1],[0,-1]]
10+
// +Diagonals:
11+
// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]]
12+
13+
export {};
14+
15+
const practice = `
16+
0,9 -> 5,9
17+
8,0 -> 0,8
18+
9,4 -> 3,4
19+
2,2 -> 2,1
20+
7,0 -> 7,4
21+
6,4 -> 2,0
22+
0,9 -> 2,9
23+
3,4 -> 1,4
24+
0,0 -> 8,8
25+
5,5 -> 8,2
26+
`;
27+
// input = practice;
28+
29+
function drawLine(start: Point2D, end: Point2D, board: Board<number>) {
30+
let [x1, y1] = start;
31+
let [x2, y2] = end;
32+
const dx = Math.abs(x2 - x1);
33+
const dy = Math.abs(y2 - y1);
34+
const sx = x1 < x2 ? 1 : -1;
35+
const sy = y1 < y2 ? 1 : -1;
36+
let err = dx - dy;
37+
38+
while (true) {
39+
board.set([x1, y1], board.get([x1, y1]) + 1);
40+
if (x1 === x2 && y1 === y2) break;
41+
const e2 = 2 * err;
42+
if (e2 > -dy) {
43+
err -= dy;
44+
x1 += sx;
45+
}
46+
if (e2 < dx) {
47+
err += dx;
48+
y1 += sy;
49+
}
50+
}
51+
}
52+
53+
const board = makeBoard(0);
54+
input.trim().split("\n").forEach(line => {
55+
let [start, end] = line.split(" -> ");
56+
let [sx, sy] = start.split(",").map(Number);
57+
let [ex, ey] = end.split(",").map(Number);
58+
// if(!(sx == ex || sy == ey)) return;//throw new Error("can only move in one direction");
59+
60+
// console.log(line);
61+
drawLine([sx, sy], [ex, ey], board);
62+
});
63+
64+
// board.print(v => v === 0 ? "." : "" + v).dwth(log);
65+
66+
let res = 0;
67+
board.forEach(v => {
68+
if(v >= 2) res += 1;
69+
});
70+
res.dwth(log);
71+
72+
73+
74+
75+
type nobi = number | bigint;
76+
type Board<T> = {
77+
get(pos: Point2D): T;
78+
set(pos: Point2D, t: T): void;
79+
clear(): void;
80+
forEach(visitor: (v: T, pos: Point2D) => void): void;
81+
print(printer?: (v: T, pos: Point2D) => string | nobi): string;
82+
copy(): Board<T>;
83+
};
84+
function makeBoard<T>(fill: T): Board<T> {
85+
// it would be useful if board could center at 0,0 and expand infinitely
86+
let board: T[][] = [];
87+
let limits:
88+
| { min: Point2D, max: Point2D }
89+
| undefined;
90+
let reso: Board<T> = {
91+
clear: () => {
92+
board = [];
93+
},
94+
get: (pos) => {
95+
if (!limits) return fill;
96+
if (
97+
pos.op(limits.min, (a, b) => a < b).some(w => w) ||
98+
pos.op(limits.max, (a, b) => a > b).some(w => w)
99+
) return fill;
100+
if (!board[pos.y]) return fill;
101+
let bval = board[pos.y][pos.x];
102+
return bval === undefined ? fill : bval;
103+
},
104+
set: (pos, v) => {
105+
if (!limits) {
106+
limits = {min: dupe(pos), max: dupe(pos)};
107+
}
108+
limits.min = pos.op(limits.min, (a, b) => Math.min(a, b));
109+
limits.max = pos.op(limits.max, (a, b) => Math.max(a, b));
110+
if (!board[pos.y]) board[pos.y] = [];
111+
board[pos.y][pos.x] = v;
112+
},
113+
forEach: visitor => {
114+
if (!limits) return;
115+
for (let y = limits.min.y; y <= limits.max.y; y++) {
116+
for (let x = limits.min.x; x <= limits.max.x; x++) {
117+
visitor(reso.get([x, y]), [x, y]);
118+
}
119+
}
120+
},
121+
copy: () => {
122+
let nb = makeBoard<T>(fill);
123+
reso.forEach((v, pos) => nb.set(pos, v));
124+
return nb;
125+
},
126+
print: (printer = v => v as any): string => {
127+
// ratelimit print
128+
if (!limits) return "*no board to print*";
129+
let ylength = 0;
130+
for (let y = limits.min.y - 1; y <= limits.max.y + 1; y++) {
131+
ylength = Math.max(y.toString().length, ylength);
132+
}
133+
const resc: string[] = [];
134+
let llen: number = limits.max.x - limits.min.x + 3;
135+
for (let y = limits.min.y - 1; y <= limits.max.y + 1; y++) {
136+
let line = "";
137+
for (let x = limits.min.x - 1; x <= limits.max.x + 1; x++) {
138+
line += printer(reso.get([x, y]), [x, y]);
139+
}
140+
if(line.length > llen) llen = line.length;
141+
resc.push(y.toString().padStart(ylength, " ") + " | " + line + " |");
142+
}
143+
resc.unshift(
144+
" ".repeat(ylength) +
145+
" .-" +
146+
"-".repeat(llen) +
147+
"-. " + (limits.min.y - 1) + " .. " + (limits.max.y + 1),
148+
);
149+
resc.push(
150+
" ".repeat(ylength) +
151+
" '-" +
152+
"-".repeat(llen) +
153+
"-'",
154+
);
155+
return resc.join("\n");
156+
},
157+
};
158+
return reso;
159+
}

2021/solutions/day5/day5.3.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
input: string, lines: string[], dblines: string[][]
3+
copy(text: string) → clipboard
4+
error(message: string) → thrown error
5+
-5..mod(3) → @mod(-5, 3)
6+
*/
7+
8+
// Cardinals:
9+
// [[1,0],[-1,0],[0,1],[0,-1]]
10+
// +Diagonals:
11+
// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]]
12+
13+
export {};
14+
15+
const practice = ``;
16+
17+
// input = practice;

2021/solutions/day5/day5.4.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
input: string, lines: string[], dblines: string[][]
3+
copy(text: string) → clipboard
4+
error(message: string) → thrown error
5+
-5..mod(3) → @mod(-5, 3)
6+
*/
7+
8+
// Cardinals:
9+
// [[1,0],[-1,0],[0,1],[0,-1]]
10+
// +Diagonals:
11+
// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]]
12+
13+
export {};
14+
15+
const practice = ``;
16+
17+
// input = practice;

2021/solutions/day5/day5.5.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
input: string, lines: string[], dblines: string[][]
3+
copy(text: string) → clipboard
4+
error(message: string) → thrown error
5+
-5..mod(3) → @mod(-5, 3)
6+
*/
7+
8+
// Cardinals:
9+
// [[1,0],[-1,0],[0,1],[0,-1]]
10+
// +Diagonals:
11+
// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]]
12+
13+
export {};
14+
15+
const practice = ``;
16+
17+
// input = practice;

0 commit comments

Comments
 (0)