This repository was archived by the owner on Mar 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay4b.js
More file actions
84 lines (74 loc) · 1.85 KB
/
Day4b.js
File metadata and controls
84 lines (74 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const fetch = require("node-fetch");
const fs = require('fs');
const filePath = "/Users/yoanadimova/Workspace/adventofcode2018/input4.txt";
const newFile = fs.readFileSync(filePath);
const data = newFile.toString('utf8').split('\n');
let minuteAmountMap = new Map();
let guardMap = new Map();
let guard = 0;
let start = 0;
let end = 0;
data.sort();
data.forEach(function(value, index) {
let input = value.split(" ");
let date = input[0].replace('[','').split('-');
let minutes = input[1].replace(']','').split(':');
if (input[3].includes('#')) {
guard = input[3].replace('#','');
start = 0;
end = 0;
} else if(input[2] == 'falls') {
start = parseInt(minutes[1]);
} else if (input[2] == 'wakes'){
end = parseInt(minutes[1]);
guardMap = fillGuardMap(guard, guardMap, start, end);
}
});
function fillGuardMap(guard, guardMap, start, end) {
if (guardMap.has(guard)) {
let innerMap;
if (guardMap.has(guard)) {
innerMap = guardMap.get(guard);
}
for (let minute = start; minute < end; minute++) {
if (innerMap.has(minute)) {
let val = innerMap.get(minute) + 1;
//console.log(guard,minute,val);
innerMap.set(minute, val);
} else {
innerMap.set(minute, 1);
}
}
}
else {
guardMap.set(guard, new Map());
let innerMap = guardMap.get(guard);
for (let minute = start; minute < end; minute++) {
if (innerMap.has(minute)) {
let val = innerMap.get(minute) + 1;
innerMap.set(minute, val);
} else {
innerMap.set(minute, 1);
}
}
}
return guardMap;
}
function whichMostFrequentlyAsleep(map) {
let max = 0;
let id = '';
let min = 0;
for (m of map) {
let innerMap = map.get(m[0]);
for (inn of innerMap) {
if (inn[1] > max) {
max = inn[1];
id = m[0];
min = inn[0];
}
}
}
return id * min;
}
let mostFrequentlyAsleep = whichMostFrequentlyAsleep(guardMap);
console.log(mostFrequentlyAsleep);