renamed 2 digit file names
This commit is contained in:
parent
018011dd27
commit
b4d985a55a
44 changed files with 0 additions and 0 deletions
1134
2018/04/input.txt
Normal file
1134
2018/04/input.txt
Normal file
File diff suppressed because it is too large
Load diff
43
2018/04/part1.js
Normal file
43
2018/04/part1.js
Normal file
|
@ -0,0 +1,43 @@
|
|||
const fs = require('fs');
|
||||
|
||||
let input = fs.readFileSync('input.txt', 'utf-8').split('\n').sort((a, b) => val(a).getTime() - val(b).getTime());
|
||||
|
||||
function val(a) {
|
||||
let time = new Date(a.match(/\[(.*)\]/)[1]);
|
||||
return time;
|
||||
}
|
||||
|
||||
let guards = {};
|
||||
let id;
|
||||
let start;
|
||||
let shifts = []
|
||||
for (let line of input) {
|
||||
let match = line.match(/#(\d+)/);
|
||||
if (match) {
|
||||
id = match[1];
|
||||
if (!guards[id])
|
||||
guards[id] = {};
|
||||
shifts.push(id);
|
||||
} else {
|
||||
if (line.includes('falls')) {
|
||||
start = val(line).getMinutes();
|
||||
} else if (line.includes('wakes')) {
|
||||
let end = val(line).getMinutes();
|
||||
for (time = start; time < end; time++) {
|
||||
guards[id][time] = (guards[id][time] || 0) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function sleps(guard) {
|
||||
try {
|
||||
return Object.values(guard[1]).reduce((a, b) => a + b) / shifts.filter(v => v == guard[0]).length;
|
||||
} catch (err) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
id = Object.entries(guards).sort((a, b) => sleps(b) - sleps(a))[0][0];
|
||||
let minute = Number(Object.entries(guards[id]).sort((a, b) => b[1] - a[1])[0][0]);
|
||||
|
||||
console.log(`#${id} * minute ${minute} = ${id * minute}`)
|
46
2018/04/part2.js
Normal file
46
2018/04/part2.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
const fs = require('fs');
|
||||
|
||||
let input = fs.readFileSync('input.txt', 'utf-8').split('\n').sort((a, b) => val(a).getTime() - val(b).getTime());
|
||||
|
||||
function val(a) {
|
||||
let time = new Date(a.match(/\[(.*)\]/)[1]);
|
||||
return time;
|
||||
}
|
||||
|
||||
let guards = {};
|
||||
let id;
|
||||
let start;
|
||||
let shifts = []
|
||||
for (let line of input) {
|
||||
let match = line.match(/#(\d+)/);
|
||||
if (match) {
|
||||
id = match[1];
|
||||
if (!guards[id])
|
||||
guards[id] = {};
|
||||
shifts.push(id);
|
||||
} else {
|
||||
if (line.includes('falls')) {
|
||||
start = val(line).getMinutes();
|
||||
} else if (line.includes('wakes')) {
|
||||
let end = val(line).getMinutes();
|
||||
for (time = start; time < end; time++) {
|
||||
guards[id][time] = (guards[id][time] || 0) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let theid;
|
||||
let themost = 0;
|
||||
let theminute;
|
||||
for (id in guards) {
|
||||
let thismost = Object.entries(guards[id]).sort((a, b) => b[1] - a[1])[0];
|
||||
if (!thismost) { continue }
|
||||
if (thismost[1] > themost) {
|
||||
theid = id;
|
||||
themost = thismost[1];
|
||||
theminute = Number(thismost[0])
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`#${theid} * minute ${theminute} = ${theid * theminute}, ${themost} times`)
|
Loading…
Add table
Add a link
Reference in a new issue