Skip to content

Commit 925c6ae

Browse files
committed
Javascript code runner
- runs the code for the latest day by default - if passed int, runs that day's code - if passed "all" runs all the days - fixed day 4+6 for crlf issues - ensured \n endings present in all js files
1 parent 990f954 commit 925c6ae

File tree

8 files changed

+19
-11
lines changed

8 files changed

+19
-11
lines changed

js/day01.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ console.log(
1010
a.map((t,k)=>n+s+t==2020&&k>j&&j>i?n*s*t:0)
1111
.reduce((p,v)=>p+v)])
1212
.reduce((p,v)=>[p[0]+v[0],p[1]+v[1]],[0,0]))
13-
.reduce((p,v)=>[p[0]+v[0],p[1]+v[1]],[0,0]));
13+
.reduce((p,v)=>[p[0]+v[0],p[1]+v[1]],[0,0]));

js/day02.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ console.log(
77
.map(x=>[+x[1], +x[2], x[3], x[4].split('')])
88
.map((x, i)=>(i = x[3].reduce((a,v)=>(v == x[2] ? a + 1 : a), 0),
99
[i>=x[0]&&i<=x[1], (x[3][x[0]-1] == x[2]) ^ (x[3][x[1]-1] == x[2])]))
10-
.reduce((a,v)=>[a[0]+v[0],a[1]+v[1]],[0,0]));
10+
.reduce((a,v)=>[a[0]+v[0],a[1]+v[1]],[0,0]));

js/day03.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ console.log(
66
.split("\r\n")
77
.filter(x=>x.length>0)
88
.map(x=>x.split(''))
9-
.reduce((p,c,i)=>[(i%x[1]==0)?(c[p[1]]=='#')+p[0]:p[0],(i%x[1]==0)?(p[1]+x[0])%c.length:p[1]],[0,0])[0],1)));
9+
.reduce((p,c,i)=>[(i%x[1]==0)?(c[p[1]]=='#')+p[0]:p[0],(i%x[1]==0)?(p[1]+x[0])%c.length:p[1]],[0,0])[0],1)));
10+

js/day04.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
console.log(
22
require('fs')
33
.readFileSync('data/day4.txt', 'utf-8')
4-
.split("\n\n")
5-
.map(x=>x.split(/[ \n]/).map(x=>x.split(':')))
4+
.split(/(\r?\n){2}/)
5+
.map(x=>x.split(/[ \n]/).map(x=>x.trim().split(':')))
66
.filter(x=>x.length==8-!x.some(y=>y[0]=="cid"))
77
.reduce(([a,b],x)=>[a+1, b+x.map(([k,v])=>
88
({byr:/19[2-9][0-9]|200[0-2]/,
@@ -12,4 +12,4 @@ console.log(
1212
hcl:/#[0-9a-f]{6}/,
1313
ecl:/amb|blu|brn|gry|grn|hzl|oth/,
1414
pid:/^[0-9]{9}$/,
15-
cid:/.*/})[k]?.test(v)?1:0).reduce((a,b)=>a&b)],[0,0]));
15+
cid:/.*/})[k]?.test(v)?1:0).reduce((a,b)=>a&b)],[0,0]));

js/day05.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
console.log(
22
require("fs")
3-
.readFileSync("data/BigBigDay5.txt", "utf-8")
3+
.readFileSync("data/day5.txt", "utf-8")
44
.trim()
55
.split("\n")
66
.map(x=>parseInt(x.trim().replace(/[FBLR]/g,x=>+"BR".includes(x)),2))
77
.sort((a,b)=>a-b)
8-
.reduce(([p,m],v)=>[v,v-p-2?m:v-1],[]));
8+
.reduce(([p,m],v)=>[v,v-p-2?m:v-1],[]));

js/day06.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
console.log(
22
require("fs")
33
.readFileSync("data/day6.txt", "utf-8")
4-
.split("\n\n")
4+
.split(/(\r?\n){2}/)
55
.map(x=>x.trim().split("\n").map(x=>x.split('')))
66
.map(x=>[x.reduce((a,x)=>new Set([...a,...x]),new Set()),x.map(x=>new Set(x))])
77
.map(([a,b])=>[a.size,b.reduce((a,b)=>new Set([...a].filter(x =>b.has(x))),b[0]).size])
8-
.reduce(([a,b],[c,d])=>[a+c,b+d],[0,0]));
8+
.reduce(([a,b],[c,d])=>[a+c,b+d],[0,0]));

js/day08.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@ console.log(
1313
d,d]).map((x,i,[f])=>i==0?0:i==1?f(x)[0]:x.map((_,i,a,j=JSON)=>
1414
f(x=j.parse(j.stringify(a)),y=x[i][0],x[i][0]=y==0?y:y==1?2:1))
1515
.filter(([_,b])=>!b)[0][0]).slice(1));
16-

js/run.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
console.log((
2+
m=process.hrtime.bigint,t=m(),f=i=>`js/day${i>8?i+1:"0"+(i+1)}.js`,
3+
z=require("fs"),r=(x=Array(25).fill(0).map((_,i)=>[i+1,z.existsSync(f(i))])
4+
.filter(([i,x])=>x).map(([x])=>x),p=process.argv,l=x.length-1,g=p.length,
5+
s="Day number too ",g==2?[x[l]]:g!=3?"Too many args":p[2]=="all"?x:(n=+p[2]
6+
,isNaN(n)?s+"non-numeric":n>l+1?s+"large":n<1?s+"small":[n])),typeof r==
7+
"string"?r:r.forEach(e=>eval(z.readFileSync(f(e-1),"utf-8"))),
8+
(Number(m()-t)*1e-9).toFixed(7)+"s"))

0 commit comments

Comments
 (0)