Skip to content

Commit 2788e47

Browse files
committed
Initial commit (solutions for 1-5)
0 parents  commit 2788e47

22 files changed

+1158
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
yarn-error.log
3+
input.txt

.prettierrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "useTabs": true, "trailingComma": "all" }

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# adventofcode (2019)
2+
3+
my adventofcode solutions and helper tool
4+
5+
`yarn go dayX` - get set up for a challenge
6+
`yarn watch` - start running code on save

package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"scripts": {
3+
"watch": "onchange --kill solutions/\\*\\*/\\*.js -- node runner.js '{{changed}}'",
4+
"prettierwatch": "onchange solutions/\\*\\*/\\*.js -- prettier --write '{{changed}}'",
5+
"go": "node setup.js"
6+
},
7+
"dependencies": {
8+
"clipboardy": "^2.1.0"
9+
},
10+
"devDependencies": {
11+
"onchange": "^6.1.0",
12+
"prettier": "^1.19.1"
13+
}
14+
}

runner.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const vm = require("vm");
4+
const clipboardy = require("clipboardy");
5+
6+
const filename = process.argv[2];
7+
let txtName = filename.replace(/\.[0-9]+\.js$/, ".txt");
8+
const filecont = fs.readFileSync(path.resolve(filename), "utf-8");
9+
const inputcont = fs.readFileSync(path.resolve(txtName), "utf-8");
10+
11+
process.stdout.write("\u001b[2J\u001b[0;0H");
12+
// process.stdout.write("\033c");
13+
14+
console.log(`
15+
${Math.random()} - ${new Date().getTime()}
16+
====================================
17+
${path.basename(filename).replace(/\.js/, "")}
18+
====================================`);
19+
20+
const sandbox = {
21+
input: inputcont.trim(),
22+
output: undefined,
23+
copy: text => (clipboardy.writeSync(text), text),
24+
print: (...v) => console.log(...v),
25+
console: console,
26+
};
27+
28+
vm.createContext(sandbox);
29+
30+
vm.runInContext(filecont, sandbox);
31+
32+
console.log("====================================");
33+
console.log(sandbox.output);

setup.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
let solnName = process.argv[2];
5+
6+
let initialText = `/*
7+
declare let input: string;
8+
declare let output: any;
9+
copy(text: string): text
10+
print(text: string): undefined
11+
*/
12+
`
13+
14+
fs.mkdirSync(path.join(__dirname, "solutions", solnName));
15+
[solnName + ".1.js", solnName + ".2.js", solnName + ".txt"].forEach((v, i) =>
16+
fs.writeFileSync(path.join(__dirname, "solutions", solnName, v), i < 2 ? initialText : "", "utf-8")
17+
);
18+
19+
console.log("done");

solutions/day1/day1.1.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let go = mass => {
2+
return Math.floor(mass / 3) - 2;
3+
};
4+
output = input
5+
.split("\n")
6+
.map(q => +q)
7+
.map(q => go(q))
8+
.reduce((a, b) => a + b, 0);

solutions/day1/day1.2.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let go = mass => {
2+
let res = Math.floor(mass / 3) - 2;
3+
if (res < 0) return 0;
4+
return res + go(res);
5+
};
6+
7+
output = input
8+
.split("\n")
9+
.map(q => +q)
10+
.map(q => go(q))
11+
.reduce((a, b) => a + b, 0);

solutions/day1/day1.txt

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
63455
2+
147371
3+
83071
4+
57460
5+
74392
6+
145303
7+
130181
8+
53102
9+
120073
10+
93111
11+
144471
12+
105327
13+
116466
14+
67222
15+
122845
16+
146097
17+
92014
18+
114428
19+
96796
20+
131140
21+
101481
22+
87953
23+
101415
24+
75739
25+
64263
26+
94257
27+
140426
28+
62387
29+
84464
30+
104547
31+
103581
32+
89121
33+
123301
34+
64993
35+
143555
36+
55246
37+
120986
38+
67596
39+
146173
40+
149707
41+
60285
42+
83517
43+
73782
44+
103464
45+
140506
46+
78400
47+
140672
48+
141638
49+
84470
50+
116879
51+
100701
52+
63976
53+
135748
54+
65021
55+
120086
56+
147249
57+
55441
58+
135315
59+
147426
60+
93676
61+
91384
62+
110918
63+
123368
64+
102430
65+
144807
66+
82761
67+
134357
68+
62990
69+
85171
70+
134886
71+
69166
72+
119744
73+
80648
74+
96752
75+
89379
76+
136178
77+
95175
78+
124306
79+
51990
80+
57564
81+
111347
82+
79317
83+
95357
84+
85765
85+
137827
86+
105014
87+
110742
88+
105014
89+
149330
90+
78437
91+
107908
92+
139044
93+
143304
94+
90614
95+
52119
96+
147113
97+
119815
98+
125634
99+
104335
100+
138295

solutions/day2/day2.1.js

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
declare let input: string;
3+
declare let output: any;
4+
copy(text: string): text
5+
print(text: string): undefined
6+
*/
7+
8+
let inv = input.split`,`;
9+
inv[1] = 12;
10+
inv[2] = 2;
11+
12+
class V {
13+
constructor() {
14+
this.memory = inv.map(v => +v);
15+
this.go = true;
16+
this.index = 0;
17+
}
18+
stop() {
19+
this.go = false;
20+
}
21+
get i() {
22+
return this.index;
23+
}
24+
set i(nv) {
25+
let prevV = this.index;
26+
this.index = nv;
27+
}
28+
get n() {
29+
// *(index++)
30+
return this.getAt(this.i++);
31+
}
32+
get p() {
33+
// *(*(index++))
34+
return this.getAt(this.n);
35+
}
36+
get m() {
37+
return this.memory;
38+
}
39+
boundsCheck(index) {
40+
if (index > this.m.length - 1 || index < 0) {
41+
throw new Error("out of bounds (" + index + " / " + this.m.length + ")");
42+
}
43+
}
44+
getAt(index) {
45+
this.boundsCheck(index);
46+
return this.memory[index];
47+
}
48+
setAt(index, value) {
49+
this.boundsCheck(index);
50+
let prevV = this.memory[index];
51+
this.memory[index] = value;
52+
return prevV;
53+
}
54+
}
55+
let v = new Proxy(new V(), {
56+
get: (obj, prop) =>
57+
(+prop).toString() === prop ? obj.getAt(+prop) : obj[prop],
58+
set: (obj, prop, value) =>
59+
(+prop).toString() === prop
60+
? obj.setAt(+prop, value)
61+
: ((obj[prop] = value), true),
62+
});
63+
64+
let intcodes = {
65+
"1": () => {
66+
let [a, b, ri] = [v.p, v.p, v.n];
67+
v[ri] = a + b;
68+
},
69+
"2": () => {
70+
let [a, b, ri] = [v.p, v.p, v.n];
71+
v[ri] = a * b;
72+
},
73+
"99": () => {
74+
return v.stop();
75+
},
76+
"404": () => {
77+
console.log("uh oh! invalid instruction", v[v.i - 1]);
78+
return v.stop();
79+
},
80+
};
81+
82+
while (v.go) {
83+
let opcode = v.n;
84+
console.log("== opcode ", opcode);
85+
(intcodes["" + opcode] || intcodes["404"])();
86+
}
87+
88+
console.log(v.m.join(","));
89+
90+
output = v[0];

0 commit comments

Comments
 (0)