aboutsummaryrefslogtreecommitdiff
path: root/src/2018/day4/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-04-08 23:29:00 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-04-08 23:29:00 +0800
commite177901c23480b5e42c153b68fb8be32ce4a4a6d (patch)
tree594cb45d28fccd92988a2c64322021df490da0e7 /src/2018/day4/aoc.cpp
parent133b01f692c98d12a82e4b102ea0ae5fc2c558ad (diff)
downloadadvent-of-code-e177901c23480b5e42c153b68fb8be32ce4a4a6d.tar.gz
advent-of-code-e177901c23480b5e42c153b68fb8be32ce4a4a6d.zip
2018 day4
Diffstat (limited to 'src/2018/day4/aoc.cpp')
-rw-r--r--src/2018/day4/aoc.cpp77
1 files changed, 42 insertions, 35 deletions
diff --git a/src/2018/day4/aoc.cpp b/src/2018/day4/aoc.cpp
index 9b9283d..811d5b5 100644
--- a/src/2018/day4/aoc.cpp
+++ b/src/2018/day4/aoc.cpp
@@ -5,41 +5,39 @@
namespace aoc2018 {
-int mostlikely(guard* g) {
- // int minute[60] = {0};
- // for (int i = 0; i < 60; i++) {
- // int ms = g->offtime[i];
- // int j = i;
- // while (ms-- > 0) {
- // minute[j++] += 1;
- // }
- // }
- // int most{INT32_MIN};
- // int highest{INT32_MIN};
+std::pair<int, int> mostlikely(guard* g) {
+ int minute[60] = {0};
+ for (int i = 0; i < 60; i++) {
+ for (auto ms : g->offtime[i]) {
+ for (int j = 0; j < ms; j++) {
+ minute[i + j] += 1;
+ }
+ }
+ }
+ int most{INT32_MIN};
+ int highest{INT32_MIN};
for (int i = 0; i < 60; i++) {
// printf("%02d ", minute[i]);
- printf("%02d ", g->offtime[i]);
- // if (minute[i] > highest) {
- // most = i;
- // highest = minute[i];
- // }
- if ((i + 1) % 10 == 0) {
- printf("\n");
+ if (minute[i] > highest) {
+ most = i;
+ highest = minute[i];
}
+ // if ((i + 1) % 10 == 0) {
+ // printf("\n");
+ //}
}
- // printf("%d\n", most);
- return 0;
+ return {most, highest};
}
int totaloff(guard* g) {
int d{0};
for (int i = 0; i < 60; i++) {
- d += g->offtime[i];
+ std::for_each(g->offtime[i].begin(), g->offtime[i].end(), [&d](int x) { d += x; });
}
return d;
}
-int day4(line_view file) {
+std::pair<int, int> day4(line_view file) {
std::vector<guard> gs;
per_line(file, [&gs](line_view lv) {
gs.emplace_back(lv);
@@ -59,10 +57,10 @@ int day4(line_view file) {
}
return false;
});
- std::for_each(gs.begin(), gs.end(), [](const guard& g) {
- printf("%02d-%02d %02d:%02d #%d %s\n", g.timestamp.month, g.timestamp.day, g.timestamp.hour, g.timestamp.minute,
- g.id, g.status == guard::on ? "on" : "off");
- });
+ // std::for_each(gs.begin(), gs.end(), [](const guard& g) {
+ // printf("%02d-%02d %02d:%02d #%d %s\n", g.timestamp.month, g.timestamp.day, g.timestamp.hour, g.timestamp.minute,
+ // g.id, g.status == guard::on ? "on" : "off");
+ // });
std::unordered_map<int, guard*> hs;
guard* p{nullptr};
@@ -81,11 +79,11 @@ int day4(line_view file) {
ontime = g.timestamp.hour > 0 ? 0 : g.timestamp.minute;
} else {
if (g.status == guard::off) {
- p->ontime[ontime] += g.timestamp.minute - ontime;
+ p->ontime[ontime].push_back(g.timestamp.minute - ontime);
offtime = g.timestamp.minute;
}
if (g.status == guard::on) {
- p->offtime[offtime] += g.timestamp.minute - offtime;
+ p->offtime[offtime].push_back(g.timestamp.minute - offtime);
ontime = g.timestamp.minute;
}
}
@@ -96,6 +94,12 @@ int day4(line_view file) {
guard* g = nullptr;
} mostoff;
+ struct {
+ int minute = 0;
+ int times = 0;
+ guard* g = nullptr;
+ } mostfrequent;
+
for (auto& kv : hs) {
int off = totaloff(kv.second);
// printf("%d: %d\n", kv.second->id, off);
@@ -103,15 +107,18 @@ int day4(line_view file) {
mostoff.g = kv.second;
mostoff.off = off;
}
- }
-
- int minute = mostlikely(mostoff.g);
- printf("\n%d: %d at %d\n", mostoff.g->id, mostoff.off, minute);
- for (auto& kv : hs) {
- delete kv.second;
+ auto p = mostlikely(kv.second);
+ if (mostfrequent.g == nullptr || mostfrequent.times < p.second) {
+ mostfrequent.g = kv.second;
+ mostfrequent.minute = p.first;
+ mostfrequent.times = p.second;
+ }
}
- return 0;
+
+ int minute = mostlikely(mostoff.g).first;
+ // printf("\n%d: %d at %d\n", mostoff.g->id, mostoff.off, minute);
+ return {mostoff.g->id * minute, mostfrequent.g->id * mostfrequent.minute};
}
} // namespace aoc2018