2018 d4 p1 js

This commit is contained in:
Tristan 2018-12-04 21:54:33 +00:00
parent fef7786f8c
commit 634b22067f
3 changed files with 1184 additions and 6 deletions

1134
2018/4/input.txt Normal file

File diff suppressed because it is too large Load diff

43
2018/4/part1.js Normal file
View 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}`)

View file

@ -1,6 +1,6 @@
# Advent of Code Solutions # Advent of Code Solutions
It\'s a fun challenge from [here](https://adventofcode.com). It's a fun challenge from [here](https://adventofcode.com).
# Completed Challenges # Completed Challenges
@ -15,6 +15,7 @@ It\'s a fun challenge from [here](https://adventofcode.com).
* [1](https://adventofcode.com/2018/day/1) js * * * [1](https://adventofcode.com/2018/day/1) js * *
* [2](https://adventofcode.com/2018/day/2) py * * * [2](https://adventofcode.com/2018/day/2) py * *
* [3](https://adventofcode.com/2018/day/3) js * * * [3](https://adventofcode.com/2018/day/3) js * *
* [4](https://adventofcode.com/2018/day/4) js *
# Languages Used # Languages Used
* Python * Python