Skip to content

Commit f4f44ed

Browse files
committed
Working part 2!
This is pretty bad code all around, oh well!
1 parent e37fa5c commit f4f44ed

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

4/modules/company.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Company {
99
this.parseLog();
1010
this.guard_most_asleep = this.getGuardMostAsleep();
1111
this.time_when_guard_is_asleep_most = this.guard_most_asleep.getMinuteMostLikelyToBeAsleepDuring();
12+
13+
this.most_often_asleep_guard_info = this.getGuardWhoSleptMostDuringAnyParticularMinute();
1214
}
1315

1416
/**
@@ -81,6 +83,28 @@ class Company {
8183
return this.guards[guard_most_asleep.id];
8284
}
8385

86+
getGuardWhoSleptMostDuringAnyParticularMinute() {
87+
let guards = Object.values(this.guards);
88+
89+
let guard_most_asleep = {
90+
id: undefined,
91+
times: -1,
92+
minuteAsleepAt: -1,
93+
};
94+
guards.forEach(guard => {
95+
let duple = guard.getMinuteMostLikelyToBeAsleepDuring(true);
96+
97+
let [minute, times] = duple;
98+
if (times > guard_most_asleep.times) {
99+
guard_most_asleep.times = times;
100+
guard_most_asleep.minuteAsleepAt = minute;
101+
guard_most_asleep.id = guard.id;
102+
}
103+
});
104+
105+
return guard_most_asleep;
106+
}
107+
84108
outputPartOne() {
85109
let guard_most_asleep = this.guard_most_asleep;
86110
let time_when_guard_is_asleep_most = this.time_when_guard_is_asleep_most;
@@ -94,6 +118,19 @@ class Company {
94118
time_when_guard_is_asleep_most}`
95119
);
96120
}
121+
122+
outputPartTwo() {
123+
let most_often_asleep_guard = this.most_often_asleep_guard_info;
124+
let { minuteAsleepAt } = most_often_asleep_guard;
125+
126+
console.log(`Guard most asleep is ${most_often_asleep_guard.id}`);
127+
console.log(`Sleeps the most during minute ${minuteAsleepAt}`);
128+
console.log(
129+
`ID * minute = ${
130+
most_often_asleep_guard.id
131+
} * ${minuteAsleepAt} = ${most_often_asleep_guard.id * minuteAsleepAt}`
132+
);
133+
}
97134
}
98135

99136
module.exports = Company;

4/modules/guard.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Guard {
1818
/**
1919
* @returns {Array}
2020
*/
21-
getMinuteMostLikelyToBeAsleepDuring() {
21+
getMinuteMostLikelyToBeAsleepDuring(return_duple = false) {
2222
let times_asleep = {};
2323
for (let i = 0; i < 60; i++) {
2424
times_asleep[i] = 0;
@@ -42,8 +42,10 @@ class Guard {
4242
}
4343
});
4444

45-
let [time_most_asleep] = sleep_sorted.pop();
46-
return time_most_asleep;
45+
let duple = sleep_sorted.pop();
46+
let time_most_asleep = duple[0];
47+
48+
return return_duple ? duple : time_most_asleep;
4749
}
4850
}
4951

0 commit comments

Comments
 (0)