diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-11-15 16:57:33 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-11-15 16:57:33 +0100 |
| commit | 566dbf51f27b6e181826b8de6c1cb98b4a680fe8 (patch) | |
| tree | 6b618efcaf38875172f611f774507c9a873e3edc /2015/src/day14.cpp | |
| parent | 6f75463d197bf7802cca4059e404ed98b1c12062 (diff) | |
| download | advent-of-code-566dbf51f27b6e181826b8de6c1cb98b4a680fe8.tar.gz advent-of-code-566dbf51f27b6e181826b8de6c1cb98b4a680fe8.tar.bz2 advent-of-code-566dbf51f27b6e181826b8de6c1cb98b4a680fe8.zip | |
aoc 2015, days 12, 13 and 14
Diffstat (limited to '2015/src/day14.cpp')
| -rw-r--r-- | 2015/src/day14.cpp | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/2015/src/day14.cpp b/2015/src/day14.cpp new file mode 100644 index 0000000..a877630 --- /dev/null +++ b/2015/src/day14.cpp | |||
| @@ -0,0 +1,112 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <limits> | ||
| 4 | #include <map> | ||
| 5 | #include <sstream> | ||
| 6 | #include <string> | ||
| 7 | #include <tuple> | ||
| 8 | #include <vector> | ||
| 9 | using namespace std; | ||
| 10 | |||
| 11 | using record_type = tuple<string, long, long, long>; | ||
| 12 | |||
| 13 | auto | ||
| 14 | split(const string& line, char sep = ' ') | ||
| 15 | { | ||
| 16 | vector<string> parts; | ||
| 17 | stringstream input{ line }; | ||
| 18 | |||
| 19 | for ( string part; getline(input, part, sep); ) { | ||
| 20 | parts.emplace_back(part); | ||
| 21 | } | ||
| 22 | |||
| 23 | return parts; | ||
| 24 | } | ||
| 25 | |||
| 26 | auto | ||
| 27 | read_file(string_view filename) | ||
| 28 | { | ||
| 29 | fstream input{ filename }; | ||
| 30 | vector<record_type> records; | ||
| 31 | |||
| 32 | for ( string line; getline(input, line); ) { | ||
| 33 | auto parts = split(line); | ||
| 34 | records.emplace_back(parts[0], stol(parts[3]), stol(parts[6]), stol(parts[13])); // NOLINT | ||
| 35 | } | ||
| 36 | return records; | ||
| 37 | } | ||
| 38 | |||
| 39 | static const size_t max_seconds = 2503; | ||
| 40 | |||
| 41 | map<string, vector<long>> | ||
| 42 | calculate_distances(const vector<record_type>& records) | ||
| 43 | { | ||
| 44 | map<string, vector<long>> distances; | ||
| 45 | |||
| 46 | for ( const auto& [name, speed, sec, sec2]: records ) { | ||
| 47 | long distance = 0; | ||
| 48 | for ( size_t second = 0; second != max_seconds; ) { | ||
| 49 | for ( size_t i = 0; second != max_seconds && i != (size_t) sec; ++i ) { | ||
| 50 | distance += speed; | ||
| 51 | ++second; | ||
| 52 | distances[name].emplace_back(distance); | ||
| 53 | } | ||
| 54 | for ( long i = 0; second != max_seconds && i != sec2; ++i ) { | ||
| 55 | ++second; | ||
| 56 | distances[name].emplace_back(distance); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | return distances; | ||
| 61 | } | ||
| 62 | |||
| 63 | void | ||
| 64 | part1(const vector<record_type>& records) | ||
| 65 | { | ||
| 66 | auto distances = calculate_distances(records); | ||
| 67 | long distance = numeric_limits<long>::min(); | ||
| 68 | |||
| 69 | for ( const auto& record: records ) { | ||
| 70 | auto name = get<0>(record); | ||
| 71 | auto final_distance = distances[name].back(); | ||
| 72 | distance = max(distance, final_distance); | ||
| 73 | } | ||
| 74 | |||
| 75 | cout << distance << endl; | ||
| 76 | } | ||
| 77 | |||
| 78 | void | ||
| 79 | part2(const vector<record_type>& records) | ||
| 80 | { | ||
| 81 | auto distances = calculate_distances(records); | ||
| 82 | |||
| 83 | map<string, long> points; | ||
| 84 | for ( size_t second = 0; second != max_seconds; ++second ) { | ||
| 85 | auto max_distance = numeric_limits<long>::min(); | ||
| 86 | for ( const auto& record: records ) { | ||
| 87 | auto name = get<0>(record); | ||
| 88 | max_distance = max(max_distance, distances[name][second]); | ||
| 89 | } | ||
| 90 | |||
| 91 | for ( const auto& record: records ) { | ||
| 92 | auto name = get<0>(record); | ||
| 93 | if ( distances[name][second] == max_distance ) { | ||
| 94 | ++points[name]; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | auto max_points = numeric_limits<long>::min(); | ||
| 100 | for ( const auto& [name, points]: points ) { | ||
| 101 | max_points = max(max_points, points); | ||
| 102 | } | ||
| 103 | cout << max_points << endl; | ||
| 104 | } | ||
| 105 | |||
| 106 | int | ||
| 107 | main() | ||
| 108 | { | ||
| 109 | auto records = read_file("data/day14.txt"); | ||
| 110 | part1(records); | ||
| 111 | part2(records); | ||
| 112 | } | ||
