more days

This commit is contained in:
Tim Stallard 2018-12-06 00:10:48 +00:00
parent 1a085be4db
commit edd783b3e4
11 changed files with 2460 additions and 0 deletions

3
03/input-test.txt Normal file
View File

@ -0,0 +1,3 @@
#1 @ 1,3: 4x4
#2 @ 3,1: 4x4
#3 @ 5,5: 2x2

1233
03/input.txt Normal file

File diff suppressed because it is too large Load Diff

18
03/part1.js Normal file
View File

@ -0,0 +1,18 @@
var input = require("fs").readFileSync("input.txt").toString().split("\n").filter(a=>a);
var sqs = input.map((a)=>(a.match(/#\d+ @ (\d+),(\d+): (\d+)x(\d+)/)).map(a=>(parseInt(a))).filter(a=>(a>=0)));
var xa = Math.min.apply(null, sqs.map(a=>(a[0])));
var ya = Math.min.apply(null, sqs.map(a=>(a[1])));
var xb = Math.max.apply(null, sqs.map(a=>(a[0]+a[2])));
var yb = Math.max.apply(null, sqs.map(a=>(a[1]+a[3])));
var count = 0;
for(var x = xa; x <= xb; x++){
for(var y = ya; y <= yb; y++){
if(sqs.filter(a=>( (a[0] <= x) && ((a[0] + a[2]) > x) && (a[1] <= y) && ((a[1]) + a[3]) > y)).length >= 2){
count++;
}
}
}
console.log(count);

30
03/part2.js Normal file
View File

@ -0,0 +1,30 @@
var input = require("fs").readFileSync("input.txt").toString().split("\n").filter(a=>a);
var sqs = input.map((a)=>(a.match(/#\d+ @ (\d+),(\d+): (\d+)x(\d+)/)).map(a=>(parseInt(a))).filter(a=>(a>=0)));
function overlapa(a, b){
xa1 = a[0]
xa2 = a[0] + a[2] - 1;
xb1 = b[0];
xb2 = b[0] + b[2] - 1;
ya1 = a[1]
ya2 = a[1] + a[3] - 1;
yb1 = b[1];
yb2 = b[1] + b[3] - 1;
return (
(((xa1 >= xb1) && (xa1 <= xb2)) || ((xa2 >= xb1) && (xa2 <= xb2)))
&&
(((ya1 >= yb1) && (ya1 <= yb2)) || ((ya2 >= yb1) && (ya2 <= yb2)))
)
}
function overlap(a,b){
return overlapa(a,b) || overlapa(b,a);
}
for(var i in sqs){
if(sqs.filter(sq=>(overlap(sq, sqs[i]))).length == 1){
console.log(i + 1);
}
}

17
04/input-test.txt Normal file
View File

@ -0,0 +1,17 @@
[1518-11-01 00:00] Guard #10 begins shift
[1518-11-01 00:05] falls asleep
[1518-11-01 00:25] wakes up
[1518-11-01 00:30] falls asleep
[1518-11-01 00:55] wakes up
[1518-11-01 23:58] Guard #99 begins shift
[1518-11-02 00:40] falls asleep
[1518-11-02 00:50] wakes up
[1518-11-03 00:05] Guard #10 begins shift
[1518-11-03 00:24] falls asleep
[1518-11-03 00:29] wakes up
[1518-11-04 00:02] Guard #99 begins shift
[1518-11-04 00:36] falls asleep
[1518-11-04 00:46] wakes up
[1518-11-05 00:03] Guard #99 begins shift
[1518-11-05 00:45] falls asleep
[1518-11-05 00:55] wakes up

1017
04/input.txt Normal file

File diff suppressed because it is too large Load Diff

59
04/part1.js Normal file
View File

@ -0,0 +1,59 @@
var input = require("fs").readFileSync("input.txt").toString().split("\n").filter(a=>a);
var timeline = [];
var events = input.sort((a,b)=>{
var pa = a.match(/\[(\d+-\d+-\d+ (\d+):(\d+))\] (.*)/);
var pb = b.match(/\[(\d+-\d+-\d+ (\d+):(\d+))\] (.*)/);
return new Date(pa[1]).getTime() - new Date(pb[1]).getTime();
})
console.log(events);
var guards = [];
var lastGuard = -1;
for(var event of events){
var p = event.match(/\[(\d+-\d+-\d+ (\d+):(\d+))\] (.*)/);
var time = (parseInt(p[2])*60) + parseInt(p[3])
if(p[4].includes("begins shift")){
lastGuard = parseInt(p[4].match(/\#(\d+)/)[1]);
}
else{
if(!guards[lastGuard]){
guards[lastGuard] = [];
}
guards[lastGuard].push(time);
}
}
var guardLengths = guards.map(g=>{
var total = 0;
for(var i = 0; i < g.length; i+=2){
if(g[i+1]<g[i]){
total += 60*12;
}
total += g[i+1] - g[i];
}
return total;
});
console.log(guards);
console.log(guardLengths);
var max = Math.max.apply(null, guardLengths.filter(a=>a));
var bestGuard = guardLengths.indexOf(max);
console.log(bestGuard, guards[bestGuard]);
var mins = [];
var times = guards[bestGuard];
for(var i = 0; i < times.length; i+=2){
for(var t = times[i]; t != times[i+1]; t=(t+1)%(24*60)){
console.log(t);
if(!mins[t]){
mins[t] = 0;
}
mins[t]++;
}
}
console.log(mins);
var bestMin = mins.indexOf(Math.max.apply(null, mins.filter(a=>a)))
console.log(Math.max.apply(null, mins.filter(a=>a)))
console.log(bestMin);
console.log(bestMin * bestGuard);

46
04/part2.js Normal file
View File

@ -0,0 +1,46 @@
var input = require("fs").readFileSync("input.txt").toString().split("\n").filter(a=>a);
var timeline = [];
var events = input.sort((a,b)=>{
var pa = a.match(/\[(\d+-\d+-\d+ (\d+):(\d+))\] (.*)/);
var pb = b.match(/\[(\d+-\d+-\d+ (\d+):(\d+))\] (.*)/);
return new Date(pa[1]).getTime() - new Date(pb[1]).getTime();
})
console.log(events);
var guards = [];
var lastGuard = -1;
for(var event of events){
var p = event.match(/\[(\d+-\d+-\d+ (\d+):(\d+))\] (.*)/);
var time = (parseInt(p[2])*60) + parseInt(p[3])
if(p[4].includes("begins shift")){
lastGuard = parseInt(p[4].match(/\#(\d+)/)[1]);
}
else{
if(!guards[lastGuard]){
guards[lastGuard] = [];
}
guards[lastGuard].push(time);
}
}
var guardMins = guards.map(times=>{
var mins = [];
for(var i = 0; i < times.length; i+=2){
for(var t = times[i]; t != times[i+1]; t=(t+1)%(24*60)){
console.log(t);
if(!mins[t]){
mins[t] = 0;
}
mins[t]++;
}
}
return mins;
});
console.log(guards);
var mostInMin = Math.max.apply(null, guardMins.reduce((a,b)=>(a.concat(b))).filter(a=>a));
console.log(mostInMin);
var bestGuard = guardMins.indexOf(guardMins.filter(a=>(a.indexOf(mostInMin) != -1))[0]);
var bestMin = guardMins[bestGuard].indexOf(mostInMin);
console.log(bestGuard, bestMin, bestGuard * bestMin);

1
05/input.txt Normal file

File diff suppressed because one or more lines are too long

15
05/part1.js Normal file
View File

@ -0,0 +1,15 @@
var input = require("fs").readFileSync("input.txt").toString();
var replaced = input.split("").filter(a=>(a!="\n")).reduce((a,b)=>{
var c = a.pop();
if(c){
if(!((b!=c) && (b.toLowerCase()==c.toLowerCase()))){
a.push(c);
a.push(b);
}
}
else{
a.push(b);
}
return a;
}, []).join("");
console.log(replaced.length);

21
05/part2.js Normal file
View File

@ -0,0 +1,21 @@
var input = require("fs").readFileSync("input.txt").toString().replace("\n", "");
replace = str=>(str.split("").filter(a=>(a!="\n")).reduce((a,b)=>{
var c = a.pop();
if(c){
if(!((b!=c) && (b.toLowerCase()==c.toLowerCase()))){
a.push(c);
a.push(b);
}
}
else{
a.push(b);
}
return a;
}, []).join(""));
var strs = [];
for(var i = 0; i < 26; i++){
strs.push(input.split("").filter((a)=>(a.toLowerCase().charCodeAt(0) != (97 + i))).join(""));
}
var lengths = strs.map(a=>(replace(a))).map(a=>(a.length));
console.log(Math.min.apply(null, lengths));