diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-04-08 23:29:00 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-04-08 23:29:00 +0800 |
commit | e177901c23480b5e42c153b68fb8be32ce4a4a6d (patch) | |
tree | 594cb45d28fccd92988a2c64322021df490da0e7 | |
parent | 133b01f692c98d12a82e4b102ea0ae5fc2c558ad (diff) | |
download | advent-of-code-e177901c23480b5e42c153b68fb8be32ce4a4a6d.tar.gz advent-of-code-e177901c23480b5e42c153b68fb8be32ce4a4a6d.zip |
2018 day4
-rw-r--r-- | src/2018/day4/README.md | 5 | ||||
-rw-r--r-- | src/2018/day4/aoc.cpp | 77 | ||||
-rw-r--r-- | src/2018/day4/aoc.h | 7 | ||||
-rw-r--r-- | test/test_2018.cpp | 4 |
4 files changed, 54 insertions, 39 deletions
diff --git a/src/2018/day4/README.md b/src/2018/day4/README.md index d66f901..d6c40a9 100644 --- a/src/2018/day4/README.md +++ b/src/2018/day4/README.md @@ -48,4 +48,9 @@ While this example listed the entries in chronological order, your entries are i What is the ID of the guard you chose multiplied by the minute you chose? (In the above example, the answer would be 10 * 24 = 240.) +--- Part Two --- +Strategy 2: Of all guards, which guard is most frequently asleep on the same minute? +In the example above, Guard #99 spent minute 45 asleep more than any other guard or minute - three times in total. (In all other cases, any guard spent any minute asleep at most twice.) + +What is the ID of the guard you chose multiplied by the minute you chose? (In the above example, the answer would be 99 * 45 = 4455.) 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 diff --git a/src/2018/day4/aoc.h b/src/2018/day4/aoc.h index 563f0c0..f2a392d 100644 --- a/src/2018/day4/aoc.h +++ b/src/2018/day4/aoc.h @@ -1,5 +1,6 @@ #pragma once #include "common.h" +#include <vector> namespace aoc2018 { @@ -16,8 +17,8 @@ struct guard { off, } status = off; - int offtime[60] = {0}; - int ontime[60] = {0}; + std::vector<int> offtime[60]; + std::vector<int> ontime[60]; void get_number(const char** pp, int* d) { const char* p = *pp; @@ -47,6 +48,6 @@ struct guard { }; }; -int day4(line_view); +std::pair<int, int> day4(line_view); } // namespace aoc2018 diff --git a/test/test_2018.cpp b/test/test_2018.cpp index a14f437..ee4c755 100644 --- a/test/test_2018.cpp +++ b/test/test_2018.cpp @@ -32,5 +32,7 @@ TEST_CASE("No Matter How You Slice It", "[2018]") { TEST_CASE("Repose Record", "[2018]") { line_view lv = load_file("../src/2018/day4/input"); - REQUIRE(0 == aoc2018::day4(lv)); + auto p = aoc2018::day4(lv); + REQUIRE(14346 == p.first); + REQUIRE(5705 == p.second); } |