diff options
| author | Thomas Schmucker <ts@its1.de> | 2023-12-15 09:56:48 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2023-12-15 09:56:48 +0100 |
| commit | e88af2c16d719e998afdd74c3501caf236fe6cf1 (patch) | |
| tree | 08dd51407afe460623ba4436e21bbd4d86748874 /src | |
| parent | 631aadb1508a884e8e6667d8ab22399525200d78 (diff) | |
| download | advent-of-code-e88af2c16d719e998afdd74c3501caf236fe6cf1.tar.gz advent-of-code-e88af2c16d719e998afdd74c3501caf236fe6cf1.tar.bz2 advent-of-code-e88af2c16d719e998afdd74c3501caf236fe6cf1.zip | |
Lösung für Tag 15, Teil 2
Diffstat (limited to 'src')
| -rw-r--r-- | src/day15.cpp | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/src/day15.cpp b/src/day15.cpp index 905110f..607f5f2 100644 --- a/src/day15.cpp +++ b/src/day15.cpp | |||
| @@ -1,5 +1,7 @@ | |||
| 1 | #include <algorithm> | ||
| 1 | #include <fstream> | 2 | #include <fstream> |
| 2 | #include <iostream> | 3 | #include <iostream> |
| 4 | #include <list> | ||
| 3 | #include <sstream> | 5 | #include <sstream> |
| 4 | #include <string> | 6 | #include <string> |
| 5 | #include <vector> | 7 | #include <vector> |
| @@ -46,8 +48,8 @@ calculate_hash(string_view str) | |||
| 46 | void | 48 | void |
| 47 | part1() | 49 | part1() |
| 48 | { | 50 | { |
| 49 | const auto line = read_file("data/day15.txt"); | 51 | const auto line = read_file("data/day15.txt"); |
| 50 | const auto parts = split(line, ','); | 52 | const auto parts = split(line, ','); |
| 51 | unsigned long value = 0; | 53 | unsigned long value = 0; |
| 52 | for ( const auto& part: parts ) { | 54 | for ( const auto& part: parts ) { |
| 53 | value += calculate_hash(part); | 55 | value += calculate_hash(part); |
| @@ -55,8 +57,50 @@ part1() | |||
| 55 | cout << value << endl; | 57 | cout << value << endl; |
| 56 | } | 58 | } |
| 57 | 59 | ||
| 60 | void | ||
| 61 | part2() | ||
| 62 | { | ||
| 63 | vector<list<tuple<string, long>>> boxes(256); | ||
| 64 | |||
| 65 | const auto line = read_file("data/day15.txt"); | ||
| 66 | const auto parts = split(line, ','); | ||
| 67 | for ( const auto& part: parts ) { | ||
| 68 | const auto pos = part.find_first_of("=-"); | ||
| 69 | const auto lens = part.substr(0, pos); | ||
| 70 | auto& box = boxes[calculate_hash(lens)]; | ||
| 71 | |||
| 72 | if ( part[pos] == '=' ) { | ||
| 73 | const auto value = stol(part.substr(pos + 1)); | ||
| 74 | |||
| 75 | auto iter = find_if(box.begin(), box.end(), [&lens](const auto& element) { return get<0>(element) == lens; }); | ||
| 76 | if ( iter == box.end() ) { | ||
| 77 | box.emplace_back(lens, value); | ||
| 78 | } | ||
| 79 | else { | ||
| 80 | *iter = { lens, value }; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | box.remove_if([&lens](const auto& element) { return get<0>(element) == lens; }); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | long value = 0; | ||
| 89 | for ( size_t idx = 0; idx != boxes.size(); ++idx ) { | ||
| 90 | const auto& box = boxes[idx]; | ||
| 91 | |||
| 92 | long lens_number = 1; | ||
| 93 | for ( const auto& lens: box ) { | ||
| 94 | value += static_cast<long>(idx + 1) * lens_number * get<1>(lens); | ||
| 95 | ++lens_number; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | cout << value << endl; | ||
| 99 | } | ||
| 100 | |||
| 58 | int | 101 | int |
| 59 | main() | 102 | main() |
| 60 | { | 103 | { |
| 61 | part1(); | 104 | part1(); |
| 105 | part2(); | ||
| 62 | } | 106 | } |
