Skip to content

Commit 9ae59f6

Browse files
committed
I switched to a slower computer. Here's a faster program runner.
Benefits: - Faster (~200ms overhead vs ~600ms from node->babel->vm) - Shows correct lines on error codes
1 parent 0b96dd9 commit 9ae59f6

File tree

8 files changed

+295
-150
lines changed

8 files changed

+295
-150
lines changed

2021/solutions/_defaults/_defaults.0.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {fhmain} from "../../../src/fheader";
2+
fhmain(__filename);
13
/*
24
input: string, lines: string[], dblines: string[][]
35
copy(text: string) → clipboard
@@ -13,5 +15,7 @@ error(message: string) → thrown error
1315
export {};
1416

1517
const practice = ``;
18+
// input = practice;
19+
input = input.trim();
1620

17-
// input = practice;
21+
// input.

2021/solutions/day6/day6.3.ts

+36-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {fhmain} from "../../../src/fheader";
2+
fhmain(__filename);
13
/*
24
input: string, lines: string[], dblines: string[][]
35
copy(text: string) → clipboard
@@ -12,6 +14,38 @@ error(message: string) → thrown error
1214

1315
export {};
1416

15-
const practice = ``;
17+
const practice = `3,4,3,1,2
18+
`;
19+
// input = practice;
1620

17-
// input = practice;
21+
let fish = input.trim().split(",").map(w => +w);
22+
23+
let counts = new Array(9).fill(0);
24+
25+
for(const fsh of fish) {
26+
counts[fsh]++;
27+
}
28+
29+
function step() {
30+
let nc: number[] = [...counts];
31+
counts.forEach((c, i) => {
32+
if(i == 0) {
33+
nc[i] -= c;
34+
nc[6] += c;
35+
nc[8] += c;
36+
}else{
37+
nc[i] -= c;
38+
nc[i - 1] += c;
39+
}
40+
});
41+
counts = nc;
42+
}
43+
44+
for(let i = 0; i < 256; i++) {
45+
step();
46+
}
47+
48+
49+
counts.dwth(log);
50+
console.log(Array.dwth.bind(counts)(log));
51+
console.log(counts.reduce((t, c) => t + c, 0));

globals.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ declare global{
1414
interface Object {
1515
dwth: <T>(this: T, cb: (v: T) => unknown) => T;
1616
use: <T, U>(this: T, cb: (v: T) => U) => U;
17-
log: <T>(this: T) => T;
1817
}
1918

2019
type Vector<N extends number, T> = (

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"scripts": {
3-
"watch": "onchange --kill */solutions/\\*\\*/\\*.{js,ts} -- node runner.js '{{changed}}'",
3+
"watch": "onchange --kill */solutions/\\*\\*/\\*.{js,ts} -- ./node_modules/.bin/esno '{{changed}}'",
44
"prettierwatch": "onchange */solutions/\\*\\*/\\*.{js,ts,md,json} \\*.{js,md,json} -- prettier --write '{{changed}}'",
55
"go": "node setup.js"
66
},
77
"dependencies": {
8-
"clipboardy": "^2.1.0"
8+
"clipboardy": "^2.1.0",
9+
"esno": "^0.12.1"
910
},
1011
"devDependencies": {
1112
"@babel/core": "^7.7.5",

runner.js

-143
This file was deleted.

src/fheader.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function fhmain(fn: string): void;

src/fheader.js

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import * as path from "path";
2+
import * as fs from "fs";
3+
4+
process.stdout.write("\u001b[2J\u001b[0;0H");
5+
6+
const sessionid = Math.random();
7+
8+
export function fhmain(filename) {
9+
console.log(`${sessionid} - ${new Date().getTime()}
10+
====================================
11+
${path.basename(filename).replace(/\.(?:js|ts)/, "")}
12+
====================================`);
13+
let txt_name = path.dirname(filename) + "/" + path.basename(path.dirname(filename)) + ".txt";
14+
15+
global.input = fs.readFileSync(txt_name, "utf-8");
16+
global.lines = global.input.split("\n");
17+
global.dblines = global.input.split("\n\n");
18+
global.output = undefined;
19+
global.start_time = Date.now();
20+
}
21+
22+
global.highlight = msg => "\x1b[31m"+msg+"\x1b(B\x1b[m";
23+
global.copy = text => {
24+
require("clipboardy").writeSync(text);
25+
return text;
26+
};
27+
global.print = (...a) => console.log(...a);
28+
global.error = (...a) => {throw new Error(highlight(a.join(" ")))},
29+
global.clearScreen = () => process.stdout.write("\u001b[2J\u001b[0;0H");
30+
31+
process.on("exit", () => {
32+
let total_ms = Date.now() - start_time;
33+
console.log("\n\x1b[90mCompleted in "+total_ms+" ms. ("+sessionid+")\x1b(B\x1b[m\x1b[A\x1b[A")
34+
});
35+
process.on("uncaughtException", (e) => {
36+
console.log();
37+
console.log(e.stack.replace(e.message, highlight(e.message)));
38+
process.exit(1);
39+
});
40+
41+
function _defproto(thing, name, cb) {
42+
Object.defineProperty(thing.prototype, name, {
43+
enumerable: false,
44+
value: function(...args) {
45+
return cb(
46+
this,
47+
...args,
48+
);
49+
},
50+
});
51+
}
52+
_defproto(Object, "defproto", _defproto);
53+
54+
Number.defproto("mod", (m, n) => ((m%n)+n)%n);
55+
// Object.defproto("dwth", (me, cb) => (cb(me), me));
56+
_defproto(Object, "dwth", (me, cb) => (cb(me), me));
57+
Object.defproto("use", (me, cb) => cb(me));
58+
global.log = (...a) => {
59+
console.log(...a.map(w => {
60+
if(w instanceof Number) return +w;
61+
if(w instanceof String) return ""+w;
62+
return w;
63+
}));
64+
};
65+
66+
global.vec = (...a) => a.flat();
67+
global.dupe = (a) => [...a];
68+
Array.defproto("op", (a, b, cb) => a.map((v, i) => cb(v, b[i], i, a, b)));
69+
Array.defproto("add", (a, b) => a.op(b, (a, b) => a + b));
70+
Array.defproto("sub", (a, b) => a.op(b, (a, b) => a - b));
71+
Array.defproto("mul", (a, b) => a.op(b, (a, b) => a * b));
72+
Array.defproto("div", (a, b) => a.op(b, (a, b) => a / b));
73+
Array.defproto("mod", (a, b) => a.op(b, (a, b) => a.mod(b)));
74+
Array.prototype.mapt = Array.prototype.map;
75+
for(const [index, name] of ["x", "y", "z", "a"].entries()) {
76+
Object.defineProperty(Array.prototype, name, {
77+
enumerable: false,
78+
get: function() {return this[index]},
79+
set: function(v) {this[index] = v},
80+
});
81+
}
82+
83+
global.cardinals = [[1,0],[-1,0],[0,1],[0,-1]];
84+
global.diagonals = [[-1,-1],[-1,1],[1,-1],[1,1]];
85+
global.adjacents = [...cardinals, ...diagonals];
86+
87+
global.range = (...a) => {
88+
const [start, end, step] = (a.length === 1 ? [0, a[0], 1] : a.length === 2 ? [...a, 1] : a);
89+
return Array.from({length: Math.ceil((end - start) / step)}, (_, i) => i * step + start);
90+
}

0 commit comments

Comments
 (0)