diff options
| author | Thomas Schmucker <ts@its1.de> | 2023-12-18 19:12:58 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2023-12-18 19:12:58 +0100 |
| commit | e67bd318726418f37d1541939ac3e97667995d8d (patch) | |
| tree | d43c2a4198bbbacb1dbc6d25f7ebe4790df45545 /src/day18.cpp | |
| parent | d6a96063a7ff7910a88c52ae1e9a3ea444d1bbc4 (diff) | |
| download | advent-of-code-e67bd318726418f37d1541939ac3e97667995d8d.tar.gz advent-of-code-e67bd318726418f37d1541939ac3e97667995d8d.tar.bz2 advent-of-code-e67bd318726418f37d1541939ac3e97667995d8d.zip | |
Lösungen für Tag 18
Diffstat (limited to 'src/day18.cpp')
| -rw-r--r-- | src/day18.cpp | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/day18.cpp b/src/day18.cpp new file mode 100644 index 0000000..3037739 --- /dev/null +++ b/src/day18.cpp | |||
| @@ -0,0 +1,115 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <map> | ||
| 4 | #include <sstream> | ||
| 5 | #include <string> | ||
| 6 | #include <vector> | ||
| 7 | using namespace std; | ||
| 8 | |||
| 9 | using puzzle_t = vector<pair<long, long>>; | ||
| 10 | |||
| 11 | puzzle_t | ||
| 12 | read_file_part1(string_view filename, long& border_steps) | ||
| 13 | { | ||
| 14 | static map<char, pair<int, int>> movement = { | ||
| 15 | { 'U', { 0, -1 } }, // up | ||
| 16 | { 'D', { 0, 1 } }, // down | ||
| 17 | { 'L', { -1, 0 } }, // left | ||
| 18 | { 'R', { 1, 0 } }, // right | ||
| 19 | }; | ||
| 20 | |||
| 21 | border_steps = 0; | ||
| 22 | |||
| 23 | fstream input{ filename }; | ||
| 24 | puzzle_t data; | ||
| 25 | |||
| 26 | data.emplace_back(0, 0); | ||
| 27 | |||
| 28 | for ( string line; getline(input, line); ) { | ||
| 29 | char dir{}; | ||
| 30 | long steps{}; | ||
| 31 | stringstream sstrm{ line }; | ||
| 32 | |||
| 33 | if ( sstrm >> dir >> steps ) { | ||
| 34 | data.emplace_back(get<0>(data.back()) + get<0>(movement[dir]) * steps, | ||
| 35 | get<1>(data.back()) + get<1>(movement[dir]) * steps); | ||
| 36 | |||
| 37 | border_steps += steps; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | return data; | ||
| 42 | } | ||
| 43 | |||
| 44 | puzzle_t | ||
| 45 | read_file_part2(string_view filename, long& border_steps) | ||
| 46 | { | ||
| 47 | static map<char, pair<int, int>> movement = { | ||
| 48 | { '0', { 1, 0 } }, // right | ||
| 49 | { '1', { 0, 1 } }, // down | ||
| 50 | { '2', { -1, 0 } }, // left | ||
| 51 | { '3', { 0, -1 } }, // up | ||
| 52 | }; | ||
| 53 | |||
| 54 | border_steps = 0; | ||
| 55 | |||
| 56 | fstream input{ filename }; | ||
| 57 | puzzle_t data; | ||
| 58 | |||
| 59 | data.emplace_back(0, 0); | ||
| 60 | |||
| 61 | for ( string line; getline(input, line); ) { | ||
| 62 | char dir{}; | ||
| 63 | long steps{}; | ||
| 64 | string hexcode; | ||
| 65 | stringstream sstrm{ line }; | ||
| 66 | |||
| 67 | if ( sstrm >> dir >> steps >> hexcode ) { | ||
| 68 | steps = stol(hexcode.substr(2, 5), nullptr, 16); | ||
| 69 | dir = hexcode[7]; | ||
| 70 | |||
| 71 | data.emplace_back(get<0>(data.back()) + get<0>(movement[dir]) * steps, | ||
| 72 | get<1>(data.back()) + get<1>(movement[dir]) * steps); | ||
| 73 | |||
| 74 | border_steps += steps; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | return data; | ||
| 79 | } | ||
| 80 | |||
| 81 | long | ||
| 82 | shoelace(const puzzle_t& puzzle, const long border_steps) | ||
| 83 | { | ||
| 84 | long area = 0; | ||
| 85 | for ( size_t i = 1; i < puzzle.size(); ++i ) { | ||
| 86 | const auto [x1, y1] = puzzle[i - 1]; | ||
| 87 | const auto [x2, y2] = puzzle[i]; | ||
| 88 | |||
| 89 | area += (x1 * y2 - y1 * x2); | ||
| 90 | } | ||
| 91 | return (border_steps + area) / 2 + 1; | ||
| 92 | } | ||
| 93 | |||
| 94 | void | ||
| 95 | part1() | ||
| 96 | { | ||
| 97 | long border_steps = 0; | ||
| 98 | auto puzzle = read_file_part1("data/day18.txt", border_steps); | ||
| 99 | cout << shoelace(puzzle, border_steps) << endl; | ||
| 100 | } | ||
| 101 | |||
| 102 | void | ||
| 103 | part2() | ||
| 104 | { | ||
| 105 | long border_steps = 0; | ||
| 106 | auto puzzle = read_file_part2("data/day18.txt", border_steps); | ||
| 107 | cout << shoelace(puzzle, border_steps) << endl; | ||
| 108 | } | ||
| 109 | |||
| 110 | int | ||
| 111 | main() | ||
| 112 | { | ||
| 113 | part1(); | ||
| 114 | part2(); | ||
| 115 | } | ||
