Skip to content

Commit b722ab6

Browse files
committed
not great today. it was really easy but I don't think I properly understood the problem statement before starting
- spent far too long debugging that it said `>9` not `>= 9` and I was trying to combine incr() and flash() but that obviously isn't possible
1 parent 7ad6095 commit b722ab6

File tree

7 files changed

+411
-1
lines changed

7 files changed

+411
-1
lines changed

2021/solutions/day11/day11.1.ts

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
5483143223
19+
2745854711
20+
5264556173
21+
6141336146
22+
6357385478
23+
4167524645
24+
2176841721
25+
6882881134
26+
4846848554
27+
5283751526
28+
`;
29+
// input = practice;
30+
input = input.trim();
31+
32+
const board = makeBoard(0);
33+
34+
input.split("\n").forEach((line, y) => {
35+
line.split("").forEach((c, x) => {
36+
board.set([x, y], +c);
37+
});
38+
});
39+
40+
let flashes = 0;
41+
42+
43+
function flash(pos: Point2D) {
44+
flashes += 1;
45+
board.set(pos, 0);
46+
for(const dir of adjacents) {
47+
const p = pos.add(dir);
48+
49+
if(board.get(p) === 0) {
50+
//
51+
} else {
52+
board.set(p, board.get(p) + 1);
53+
incr(p);
54+
}
55+
}
56+
}
57+
58+
function incr(pos: Point2D) {
59+
const value = board.get(pos);
60+
if(value === 0) {
61+
return;
62+
}
63+
if(value > 9) {
64+
flash(pos);
65+
return;
66+
}
67+
board.set(pos, value);
68+
}
69+
70+
function step() {
71+
board.forEach((v, pos) => {
72+
board.set(pos, board.get(pos) + 1);
73+
});
74+
board.forEach((v, pos) => {
75+
incr(pos);
76+
});
77+
}
78+
79+
board.print().dwth(log);
80+
81+
for(let i = 0; i < 100; i++) {
82+
step();
83+
// board.print().dwth(log);
84+
}
85+
console.log(flashes);
86+
87+
type nobi = number | bigint;
88+
type Board<T> = {
89+
get(pos: Point2D): T;
90+
set(pos: Point2D, t: T): void;
91+
clear(): void;
92+
forEach(visitor: (v: T, pos: Point2D) => void): void;
93+
print(printer?: (v: T, pos: Point2D) => string | nobi): string;
94+
copy(): Board<T>;
95+
};
96+
function makeBoard<T>(fill: T): Board<T> {
97+
// it would be useful if board could center at 0,0 and expand infinitely
98+
let board: T[][] = [];
99+
let limits:
100+
| { min: Point2D, max: Point2D }
101+
| undefined;
102+
let reso: Board<T> = {
103+
clear: () => {
104+
board = [];
105+
},
106+
get: (pos) => {
107+
if (!limits) return fill;
108+
if (
109+
pos.op(limits.min, (a, b) => a < b).some(w => w) ||
110+
pos.op(limits.max, (a, b) => a > b).some(w => w)
111+
) return fill;
112+
if (!board[pos.y]) return fill;
113+
let bval = board[pos.y][pos.x];
114+
return bval === undefined ? fill : bval;
115+
},
116+
set: (pos, v) => {
117+
if (!limits) {
118+
limits = {min: dupe(pos), max: dupe(pos)};
119+
}
120+
limits.min = pos.op(limits.min, (a, b) => Math.min(a, b));
121+
limits.max = pos.op(limits.max, (a, b) => Math.max(a, b));
122+
if (!board[pos.y]) board[pos.y] = [];
123+
board[pos.y][pos.x] = v;
124+
},
125+
forEach: visitor => {
126+
if (!limits) return;
127+
for (let y = limits.min.y; y <= limits.max.y; y++) {
128+
for (let x = limits.min.x; x <= limits.max.x; x++) {
129+
visitor(reso.get([x, y]), [x, y]);
130+
}
131+
}
132+
},
133+
copy: () => {
134+
let nb = makeBoard<T>(fill);
135+
reso.forEach((v, pos) => nb.set(pos, v));
136+
return nb;
137+
},
138+
print: (printer = v => v as any): string => {
139+
// ratelimit print
140+
if (!limits) return "*no board to print*";
141+
let ylength = 0;
142+
for (let y = limits.min.y - 1; y <= limits.max.y + 1; y++) {
143+
ylength = Math.max(y.toString().length, ylength);
144+
}
145+
const resc: string[] = [];
146+
let llen: number = limits.max.x - limits.min.x + 3;
147+
for (let y = limits.min.y - 1; y <= limits.max.y + 1; y++) {
148+
let line = "";
149+
for (let x = limits.min.x - 1; x <= limits.max.x + 1; x++) {
150+
line += printer(reso.get([x, y]), [x, y]);
151+
}
152+
if(line.length > llen) llen = line.length;
153+
resc.push(y.toString().padStart(ylength, " ") + " | " + line + " |");
154+
}
155+
resc.unshift(
156+
" ".repeat(ylength) +
157+
" .-" +
158+
"-".repeat(llen) +
159+
"-. " + (limits.min.y - 1) + " .. " + (limits.max.y + 1),
160+
);
161+
resc.push(
162+
" ".repeat(ylength) +
163+
" '-" +
164+
"-".repeat(llen) +
165+
"-'",
166+
);
167+
return resc.join("\n");
168+
},
169+
};
170+
return reso;
171+
}

2021/solutions/day11/day11.2.ts

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
5483143223
19+
2745854711
20+
5264556173
21+
6141336146
22+
6357385478
23+
4167524645
24+
2176841721
25+
6882881134
26+
4846848554
27+
5283751526
28+
`;
29+
// input = practice;
30+
input = input.trim();
31+
32+
const board = makeBoard(0);
33+
34+
input.split("\n").forEach((line, y) => {
35+
line.split("").forEach((c, x) => {
36+
board.set([x, y], +c);
37+
});
38+
});
39+
40+
let flashes = 0;
41+
42+
43+
function flash(pos: Point2D) {
44+
flashes += 1;
45+
board.set(pos, 0);
46+
for(const dir of adjacents) {
47+
const p = pos.add(dir);
48+
49+
if(board.get(p) === 0) {
50+
//
51+
} else {
52+
board.set(p, board.get(p) + 1);
53+
incr(p);
54+
}
55+
}
56+
}
57+
58+
function incr(pos: Point2D) {
59+
const value = board.get(pos);
60+
if(value === 0) {
61+
return;
62+
}
63+
if(value > 9) {
64+
flash(pos);
65+
return;
66+
}
67+
board.set(pos, value);
68+
}
69+
70+
71+
let stepi = 0;
72+
function step() {
73+
board.forEach((v, pos) => {
74+
board.set(pos, board.get(pos) + 1);
75+
});
76+
flashes = 0;
77+
board.forEach((v, pos) => {
78+
incr(pos);
79+
});
80+
if(flashes === 10 * 10) {
81+
throw new Error("stopped at step " +(stepi + 1)); // expect=788
82+
}
83+
stepi += 1;
84+
}
85+
86+
board.print().dwth(log);
87+
88+
for(let i = 0; i < 1000; i++) {
89+
step();
90+
// board.print().dwth(log);
91+
}
92+
console.log(flashes);
93+
94+
type nobi = number | bigint;
95+
type Board<T> = {
96+
get(pos: Point2D): T;
97+
set(pos: Point2D, t: T): void;
98+
clear(): void;
99+
forEach(visitor: (v: T, pos: Point2D) => void): void;
100+
print(printer?: (v: T, pos: Point2D) => string | nobi): string;
101+
copy(): Board<T>;
102+
};
103+
function makeBoard<T>(fill: T): Board<T> {
104+
// it would be useful if board could center at 0,0 and expand infinitely
105+
let board: T[][] = [];
106+
let limits:
107+
| { min: Point2D, max: Point2D }
108+
| undefined;
109+
let reso: Board<T> = {
110+
clear: () => {
111+
board = [];
112+
},
113+
get: (pos) => {
114+
if (!limits) return fill;
115+
if (
116+
pos.op(limits.min, (a, b) => a < b).some(w => w) ||
117+
pos.op(limits.max, (a, b) => a > b).some(w => w)
118+
) return fill;
119+
if (!board[pos.y]) return fill;
120+
let bval = board[pos.y][pos.x];
121+
return bval === undefined ? fill : bval;
122+
},
123+
set: (pos, v) => {
124+
if (!limits) {
125+
limits = {min: dupe(pos), max: dupe(pos)};
126+
}
127+
limits.min = pos.op(limits.min, (a, b) => Math.min(a, b));
128+
limits.max = pos.op(limits.max, (a, b) => Math.max(a, b));
129+
if (!board[pos.y]) board[pos.y] = [];
130+
board[pos.y][pos.x] = v;
131+
},
132+
forEach: visitor => {
133+
if (!limits) return;
134+
for (let y = limits.min.y; y <= limits.max.y; y++) {
135+
for (let x = limits.min.x; x <= limits.max.x; x++) {
136+
visitor(reso.get([x, y]), [x, y]);
137+
}
138+
}
139+
},
140+
copy: () => {
141+
let nb = makeBoard<T>(fill);
142+
reso.forEach((v, pos) => nb.set(pos, v));
143+
return nb;
144+
},
145+
print: (printer = v => v as any): string => {
146+
// ratelimit print
147+
if (!limits) return "*no board to print*";
148+
let ylength = 0;
149+
for (let y = limits.min.y - 1; y <= limits.max.y + 1; y++) {
150+
ylength = Math.max(y.toString().length, ylength);
151+
}
152+
const resc: string[] = [];
153+
let llen: number = limits.max.x - limits.min.x + 3;
154+
for (let y = limits.min.y - 1; y <= limits.max.y + 1; y++) {
155+
let line = "";
156+
for (let x = limits.min.x - 1; x <= limits.max.x + 1; x++) {
157+
line += printer(reso.get([x, y]), [x, y]);
158+
}
159+
if(line.length > llen) llen = line.length;
160+
resc.push(y.toString().padStart(ylength, " ") + " | " + line + " |");
161+
}
162+
resc.unshift(
163+
" ".repeat(ylength) +
164+
" .-" +
165+
"-".repeat(llen) +
166+
"-. " + (limits.min.y - 1) + " .. " + (limits.max.y + 1),
167+
);
168+
resc.push(
169+
" ".repeat(ylength) +
170+
" '-" +
171+
"-".repeat(llen) +
172+
"-'",
173+
);
174+
return resc.join("\n");
175+
},
176+
};
177+
return reso;
178+
}

2021/solutions/day11/day11.3.ts

Whitespace-only changes.

2021/solutions/day11/day11.4.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
// input = practice;
19+
input = input.trim();
20+
21+
// input.
22+
23+
// hi! i'm glad you're excited to code
24+
// but consider fully reading the problem statement first.
25+
// Sincerely, future you.

0 commit comments

Comments
 (0)