Skip to content

Commit 08fa410

Browse files
committed
not super great. understood the problem fully but attempted at first with a method I knew would be less debuggable but hoped would work anyway. it didn't, so I had to rewrite it in the way that I knew would be needed for part 2 and was actually visually debuggable.
- probably should have gone with the visual way from the start. I knew trying to do it with a Set() I would run into problems and I would have to make changes for part 2. so I should have just done the board from the start. - also I have to fix my downloader. wasn't expecting node-fetch to decide it couldn't be imported with require() because it didn't feel like it. how have web things adapted so easily to `import` but node stuff is just completely broken and not compatible
1 parent 70afed7 commit 08fa410

File tree

6 files changed

+1213
-0
lines changed

6 files changed

+1213
-0
lines changed

2021/solutions/day13/day13.1.ts

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
import {fhmain} from "../../../src/fheader";
2+
fhmain(__filename);
3+
/*
4+
input: string, lines: string[], dblines: string[][]
5+
copy(text: string) → clipboard
6+
error(message: string) → thrown error
7+
-5..mod(3) → @mod(-5, 3)
8+
*/
9+
10+
// Cardinals:
11+
// [[1,0],[-1,0],[0,1],[0,-1]]
12+
// +Diagonals:
13+
// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]]
14+
15+
export {};
16+
17+
const practice = `
18+
6,10
19+
0,14
20+
9,10
21+
0,3
22+
10,4
23+
4,11
24+
6,0
25+
6,12
26+
4,1
27+
0,13
28+
10,12
29+
3,4
30+
3,0
31+
8,4
32+
1,10
33+
2,14
34+
8,10
35+
9,0
36+
37+
fold along y=7
38+
fold along x=5
39+
`;
40+
// input = practice;
41+
input = input.trim();
42+
43+
44+
45+
46+
const [dots, folds] = input.split("\n\n");
47+
48+
const dts = dots.split("\n").map(v => v.split(",").map(Number));
49+
50+
let board = makeBoard(".");
51+
52+
for(const dot of dts) {
53+
board.set(dot, "#");
54+
}
55+
56+
57+
const firstfold = folds.split("\n")[0].split(" ")[2].split("=");
58+
59+
const res = new Set();
60+
61+
const m = firstfold[0] === "x" ? 0 : 1;
62+
const n = 1 - m;
63+
64+
function applyfold(fold) {
65+
const newboard = makeBoard(".");
66+
67+
const m = firstfold[0] === "x" ? 0 : 1;
68+
const q = m === 0 ? [+firstfold[1], 0] : [0, +firstfold[1]];
69+
console.log(q);
70+
71+
board.forEach((tile, pos) => {
72+
if(tile === "#") {
73+
if(pos[m] > +fold[1]) {
74+
newboard.set(
75+
pos.sub(q)
76+
.dwth(v => v[m] = -v[m])
77+
.add(q),
78+
"#",
79+
);
80+
}else{
81+
newboard.set(pos, "#");
82+
}
83+
}
84+
});
85+
86+
board = newboard;
87+
}
88+
89+
board.print().dwth(log);
90+
applyfold(firstfold);
91+
board.print().dwth(log);
92+
93+
let count = 0;
94+
board.forEach((itm) => {
95+
if(itm === "#") {
96+
count++;
97+
}
98+
});
99+
count.dwth(log); // expect=602
100+
101+
102+
103+
104+
105+
106+
type nobi = number | bigint;
107+
type Board<T> = {
108+
get(pos: Point2D): T;
109+
set(pos: Point2D, t: T): void;
110+
clear(): void;
111+
forEach(visitor: (v: T, pos: Point2D) => void): void;
112+
print(printer?: (v: T, pos: Point2D) => string | nobi): string;
113+
copy(): Board<T>;
114+
};
115+
function makeBoard<T>(fill: T): Board<T> {
116+
// it would be useful if board could center at 0,0 and expand infinitely
117+
let board: T[][] = [];
118+
let limits:
119+
| { min: Point2D, max: Point2D }
120+
| undefined;
121+
let reso: Board<T> = {
122+
clear: () => {
123+
board = [];
124+
},
125+
get: (pos) => {
126+
if (!limits) return fill;
127+
if (
128+
pos.op(limits.min, (a, b) => a < b).some(w => w) ||
129+
pos.op(limits.max, (a, b) => a > b).some(w => w)
130+
) return fill;
131+
if (!board[pos.y]) return fill;
132+
let bval = board[pos.y][pos.x];
133+
return bval === undefined ? fill : bval;
134+
},
135+
set: (pos, v) => {
136+
if (!limits) {
137+
limits = {min: dupe(pos), max: dupe(pos)};
138+
}
139+
limits.min = pos.op(limits.min, (a, b) => Math.min(a, b));
140+
limits.max = pos.op(limits.max, (a, b) => Math.max(a, b));
141+
if (!board[pos.y]) board[pos.y] = [];
142+
board[pos.y][pos.x] = v;
143+
},
144+
forEach: visitor => {
145+
if (!limits) return;
146+
for (let y = limits.min.y; y <= limits.max.y; y++) {
147+
for (let x = limits.min.x; x <= limits.max.x; x++) {
148+
visitor(reso.get([x, y]), [x, y]);
149+
}
150+
}
151+
},
152+
copy: () => {
153+
let nb = makeBoard<T>(fill);
154+
reso.forEach((v, pos) => nb.set(pos, v));
155+
return nb;
156+
},
157+
print: (printer = v => v as any): string => {
158+
// ratelimit print
159+
if (!limits) return "*no board to print*";
160+
let ylength = 0;
161+
for (let y = limits.min.y - 1; y <= limits.max.y + 1; y++) {
162+
ylength = Math.max(y.toString().length, ylength);
163+
}
164+
const resc: string[] = [];
165+
let llen: number = limits.max.x - limits.min.x + 3;
166+
for (let y = limits.min.y - 1; y <= limits.max.y + 1; y++) {
167+
let line = "";
168+
for (let x = limits.min.x - 1; x <= limits.max.x + 1; x++) {
169+
line += printer(reso.get([x, y]), [x, y]);
170+
}
171+
if(line.length > llen) llen = line.length;
172+
resc.push(y.toString().padStart(ylength, " ") + " | " + line + " |");
173+
}
174+
resc.unshift(
175+
" ".repeat(ylength) +
176+
" .-" +
177+
"-".repeat(llen) +
178+
"-. " + (limits.min.y - 1) + " .. " + (limits.max.y + 1),
179+
);
180+
resc.push(
181+
" ".repeat(ylength) +
182+
" '-" +
183+
"-".repeat(llen) +
184+
"-'",
185+
);
186+
return resc.join("\n");
187+
},
188+
};
189+
return reso;
190+
}

2021/solutions/day13/day13.2.ts

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
import {fhmain} from "../../../src/fheader";
2+
fhmain(__filename);
3+
/*
4+
input: string, lines: string[], dblines: string[][]
5+
copy(text: string) → clipboard
6+
error(message: string) → thrown error
7+
-5..mod(3) → @mod(-5, 3)
8+
*/
9+
10+
// Cardinals:
11+
// [[1,0],[-1,0],[0,1],[0,-1]]
12+
// +Diagonals:
13+
// [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]]
14+
15+
export {};
16+
17+
const practice = `
18+
6,10
19+
0,14
20+
9,10
21+
0,3
22+
10,4
23+
4,11
24+
6,0
25+
6,12
26+
4,1
27+
0,13
28+
10,12
29+
3,4
30+
3,0
31+
8,4
32+
1,10
33+
2,14
34+
8,10
35+
9,0
36+
37+
fold along y=7
38+
fold along x=5
39+
`;
40+
// input = practice;
41+
input = input.trim();
42+
43+
44+
45+
46+
const [dots, folds] = input.split("\n\n");
47+
48+
const dts = dots.split("\n").map(v => v.split(",").map(Number));
49+
50+
let board = makeBoard(".");
51+
52+
for(const dot of dts) {
53+
board.set(dot, "#");
54+
}
55+
56+
const firstfold = folds.split("\n").map(fold => fold.split(" ")[2].split("="));
57+
58+
const res = new Set();
59+
60+
function applyfold(fold) {
61+
const newboard = makeBoard(".");
62+
63+
const m = fold[0] === "x" ? 0 : 1;
64+
const q = m === 0 ? [+fold[1], 0] : [0, +fold[1]];
65+
console.log(fold, m, q, +firstfold[1], firstfold[1]);
66+
67+
board.forEach((tile, pos) => {
68+
if(tile === "#") {
69+
if(pos[m] > +fold[1]) {
70+
newboard.set(
71+
pos.sub(q)
72+
.dwth(v => v[m] = -v[m])
73+
.add(q),
74+
"#",
75+
);
76+
}else{
77+
newboard.set(pos, "#");
78+
}
79+
}
80+
});
81+
82+
board = newboard;
83+
}
84+
85+
// board.print().dwth(log);
86+
for(const fold of firstfold) {
87+
applyfold(fold);
88+
}
89+
board.print().dwth(log);
90+
91+
let count = 0;
92+
board.forEach((itm) => {
93+
if(itm === "#") {
94+
count++;
95+
}
96+
});
97+
count.dwth(log);
98+
// expect=
99+
/*
100+
.-------------------------------------------. -1 .. 6
101+
-1 | ......................................... |
102+
0 | ..##...##..####...##.#..#.####..##..#..#. |
103+
1 | .#..#.#..#.#.......#.#..#....#.#..#.#.#.. |
104+
2 | .#....#..#.###.....#.####...#..#....##... |
105+
3 | .#....####.#.......#.#..#..#...#....#.#.. |
106+
4 | .#..#.#..#.#....#..#.#..#.#....#..#.#.#.. |
107+
5 | ..##..#..#.#.....##..#..#.####..##..#..#. |
108+
6 | ......................................... |
109+
'-------------------------------------------'
110+
*/
111+
112+
113+
114+
115+
116+
type nobi = number | bigint;
117+
type Board<T> = {
118+
get(pos: Point2D): T;
119+
set(pos: Point2D, t: T): void;
120+
clear(): void;
121+
forEach(visitor: (v: T, pos: Point2D) => void): void;
122+
print(printer?: (v: T, pos: Point2D) => string | nobi): string;
123+
copy(): Board<T>;
124+
};
125+
function makeBoard<T>(fill: T): Board<T> {
126+
// it would be useful if board could center at 0,0 and expand infinitely
127+
let board: T[][] = [];
128+
let limits:
129+
| { min: Point2D, max: Point2D }
130+
| undefined;
131+
let reso: Board<T> = {
132+
clear: () => {
133+
board = [];
134+
},
135+
get: (pos) => {
136+
if (!limits) return fill;
137+
if (
138+
pos.op(limits.min, (a, b) => a < b).some(w => w) ||
139+
pos.op(limits.max, (a, b) => a > b).some(w => w)
140+
) return fill;
141+
if (!board[pos.y]) return fill;
142+
let bval = board[pos.y][pos.x];
143+
return bval === undefined ? fill : bval;
144+
},
145+
set: (pos, v) => {
146+
if (!limits) {
147+
limits = {min: dupe(pos), max: dupe(pos)};
148+
}
149+
limits.min = pos.op(limits.min, (a, b) => Math.min(a, b));
150+
limits.max = pos.op(limits.max, (a, b) => Math.max(a, b));
151+
if (!board[pos.y]) board[pos.y] = [];
152+
board[pos.y][pos.x] = v;
153+
},
154+
forEach: visitor => {
155+
if (!limits) return;
156+
for (let y = limits.min.y; y <= limits.max.y; y++) {
157+
for (let x = limits.min.x; x <= limits.max.x; x++) {
158+
visitor(reso.get([x, y]), [x, y]);
159+
}
160+
}
161+
},
162+
copy: () => {
163+
let nb = makeBoard<T>(fill);
164+
reso.forEach((v, pos) => nb.set(pos, v));
165+
return nb;
166+
},
167+
print: (printer = v => v as any): string => {
168+
// ratelimit print
169+
if (!limits) return "*no board to print*";
170+
let ylength = 0;
171+
for (let y = limits.min.y - 1; y <= limits.max.y + 1; y++) {
172+
ylength = Math.max(y.toString().length, ylength);
173+
}
174+
const resc: string[] = [];
175+
let llen: number = limits.max.x - limits.min.x + 3;
176+
for (let y = limits.min.y - 1; y <= limits.max.y + 1; y++) {
177+
let line = "";
178+
for (let x = limits.min.x - 1; x <= limits.max.x + 1; x++) {
179+
line += printer(reso.get([x, y]), [x, y]);
180+
}
181+
if(line.length > llen) llen = line.length;
182+
resc.push(y.toString().padStart(ylength, " ") + " | " + line + " |");
183+
}
184+
resc.unshift(
185+
" ".repeat(ylength) +
186+
" .-" +
187+
"-".repeat(llen) +
188+
"-. " + (limits.min.y - 1) + " .. " + (limits.max.y + 1),
189+
);
190+
resc.push(
191+
" ".repeat(ylength) +
192+
" '-" +
193+
"-".repeat(llen) +
194+
"-'",
195+
);
196+
return resc.join("\n");
197+
},
198+
};
199+
return reso;
200+
}

0 commit comments

Comments
 (0)