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 pathDay4.js
More file actions
120 lines (103 loc) · 2.65 KB
/
Day4.js
File metadata and controls
120 lines (103 loc) · 2.65 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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]);
minuteAmountMap = fillMinuteAmoutMap(guard, minuteAmountMap, end - start);
guardMap = fillGuardMap(guard, guardMap, start, end);
}
});
function fillMinuteAmoutMap(guard, minuteAmountMap, mins) {
if (minuteAmountMap.has(guard)) {
let val = minuteAmountMap.get(guard) + mins;
minuteAmountMap.set(guard, val);
} else {
minuteAmountMap.set(guard, mins);
}
return minuteAmountMap;
}
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 findMaxGuard(map) {
let max = -232210322319219021;
let guard;
for (m of map) {
if (m[1] > max) {
max = m[1];
guard = m[0];
}
}
return guard;
}
function whichMinute(guardId, map) {
let guardMinutes = map.get(guardId);
let max = -232210322319219021;
let result = 0;
if (map.has(guardId)) {
let innerMap = map.get(guardId);
for (o of innerMap) {
if (o[1] > max) {
max = o[1];
result = o[0];
}
}
}
return result;
}
function multiply(minute, guardId) {
return minute * guardId;
}
let guardId = findMaxGuard(minuteAmountMap);
let minute = whichMinute(guardId, guardMap);
whichMinute(findMaxGuard(minuteAmountMap), guardMap);
console.log('Multip: ' + multiply(guardId,minute));
console.log('Minute: ' + minute);
console.log('Guard: ' + guardId);
//console.log(guardMap);
//console.log(minuteAmountMap);