diff options
Diffstat (limited to '2015/src/day13.cpp')
| -rw-r--r-- | 2015/src/day13.cpp | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/2015/src/day13.cpp b/2015/src/day13.cpp new file mode 100644 index 0000000..7256b81 --- /dev/null +++ b/2015/src/day13.cpp | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <fstream> | ||
| 3 | #include <iostream> | ||
| 4 | #include <iterator> | ||
| 5 | #include <limits> | ||
| 6 | #include <map> | ||
| 7 | #include <set> | ||
| 8 | #include <sstream> | ||
| 9 | #include <string> | ||
| 10 | #include <tuple> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | using namespace std; | ||
| 14 | |||
| 15 | auto | ||
| 16 | split(const string& line, char sep = ' ') | ||
| 17 | { | ||
| 18 | vector<string> parts; | ||
| 19 | stringstream input{ line }; | ||
| 20 | |||
| 21 | for ( string part; getline(input, part, sep); ) { | ||
| 22 | parts.emplace_back(part); | ||
| 23 | } | ||
| 24 | |||
| 25 | return parts; | ||
| 26 | } | ||
| 27 | |||
| 28 | auto | ||
| 29 | read_file(string_view filename) | ||
| 30 | { | ||
| 31 | fstream input{ filename }; | ||
| 32 | set<string> names; | ||
| 33 | map<tuple<string, string>, long> table; | ||
| 34 | |||
| 35 | for ( string line; getline(input, line); ) { | ||
| 36 | if ( line.ends_with(".") ) { | ||
| 37 | line.pop_back(); | ||
| 38 | } | ||
| 39 | |||
| 40 | auto parts = split(line); | ||
| 41 | |||
| 42 | auto start = parts[0]; | ||
| 43 | auto units = parts[2] == "gain" ? stol(parts[3]) : -stol(parts[3]); | ||
| 44 | auto end = parts[10]; // NOLINT | ||
| 45 | |||
| 46 | table[{ start, end }] = units; | ||
| 47 | |||
| 48 | names.insert(start); | ||
| 49 | } | ||
| 50 | return table; | ||
| 51 | } | ||
| 52 | |||
| 53 | void | ||
| 54 | part1(map<tuple<string, string>, long>& table) | ||
| 55 | { | ||
| 56 | set<string> unique_names; | ||
| 57 | for ( const auto& item: table ) { | ||
| 58 | unique_names.insert(get<0>(item.first)); | ||
| 59 | } | ||
| 60 | vector<string> names{ begin(unique_names), end(unique_names) }; | ||
| 61 | |||
| 62 | long happiness_sofar = numeric_limits<long>::min(); | ||
| 63 | do { | ||
| 64 | long happiness = 0; | ||
| 65 | for ( size_t idx = 0; idx != names.size(); ++idx ) { | ||
| 66 | auto next_idx = (idx + 1) % names.size(); | ||
| 67 | |||
| 68 | happiness += table[{ names[idx], names[next_idx] }]; | ||
| 69 | happiness += table[{ names[next_idx], names[idx] }]; | ||
| 70 | } | ||
| 71 | |||
| 72 | happiness_sofar = max(happiness_sofar, happiness); | ||
| 73 | } while ( next_permutation(begin(names), end(names)) ); | ||
| 74 | cout << happiness_sofar << endl; | ||
| 75 | } | ||
| 76 | |||
| 77 | void | ||
| 78 | part2(map<tuple<string, string>, long>& table) | ||
| 79 | { | ||
| 80 | set<string> unique_names; | ||
| 81 | for ( const auto& item: table ) { | ||
| 82 | unique_names.insert(get<0>(item.first)); | ||
| 83 | } | ||
| 84 | vector<string> names{ begin(unique_names), end(unique_names) }; | ||
| 85 | names.emplace_back("Hartkode"); | ||
| 86 | |||
| 87 | long happiness_sofar = numeric_limits<long>::min(); | ||
| 88 | do { | ||
| 89 | long happiness = 0; | ||
| 90 | for ( size_t idx = 0; idx != names.size(); ++idx ) { | ||
| 91 | auto next_idx = (idx + 1) % names.size(); | ||
| 92 | |||
| 93 | happiness += table[{ names[idx], names[next_idx] }]; | ||
| 94 | happiness += table[{ names[next_idx], names[idx] }]; | ||
| 95 | } | ||
| 96 | |||
| 97 | happiness_sofar = max(happiness_sofar, happiness); | ||
| 98 | } while ( next_permutation(begin(names), end(names)) ); | ||
| 99 | cout << happiness_sofar << endl; | ||
| 100 | } | ||
| 101 | |||
| 102 | int | ||
| 103 | main() | ||
| 104 | { | ||
| 105 | auto table = read_file("data/day13.txt"); | ||
| 106 | part1(table); | ||
| 107 | part2(table); | ||
| 108 | } | ||
