diff options
| author | Thomas Schmucker <ts@its1.de> | 2023-12-10 10:41:42 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2023-12-10 10:41:42 +0100 |
| commit | 1165d51cc10b5795e1ec78f3eb5c9b56bfb31d18 (patch) | |
| tree | 33e0702a7281338670b2ef19f7e21d8c02885829 /src | |
| parent | 40023c113c17ff0e8b5b7dfffef7c24d6632d63a (diff) | |
| download | advent-of-code-1165d51cc10b5795e1ec78f3eb5c9b56bfb31d18.tar.gz advent-of-code-1165d51cc10b5795e1ec78f3eb5c9b56bfb31d18.tar.bz2 advent-of-code-1165d51cc10b5795e1ec78f3eb5c9b56bfb31d18.zip | |
Lösung für Tag 10, Teil 1
Diffstat (limited to 'src')
| -rw-r--r-- | src/day10.cpp | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/src/day10.cpp b/src/day10.cpp new file mode 100644 index 0000000..b9d6355 --- /dev/null +++ b/src/day10.cpp | |||
| @@ -0,0 +1,144 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <sstream> | ||
| 4 | #include <string> | ||
| 5 | #include <vector> | ||
| 6 | using namespace std; | ||
| 7 | |||
| 8 | vector<string> | ||
| 9 | read_file(string_view filename) | ||
| 10 | { | ||
| 11 | fstream input{ filename }; | ||
| 12 | vector<string> data; | ||
| 13 | |||
| 14 | for ( string line; getline(input, line); ) { | ||
| 15 | data.emplace_back(line); | ||
| 16 | } | ||
| 17 | |||
| 18 | return data; | ||
| 19 | } | ||
| 20 | |||
| 21 | void | ||
| 22 | part1() | ||
| 23 | { | ||
| 24 | typedef tuple<size_t, size_t> pos_t; | ||
| 25 | |||
| 26 | const auto puzzle = read_file("data/day10.txt"); | ||
| 27 | |||
| 28 | const auto find_start_pos = [&]() -> pos_t { | ||
| 29 | for ( size_t y = 0; y != puzzle.size(); ++y ) { // NOLINT | ||
| 30 | auto x = puzzle[y].find('S'); // NOLINT | ||
| 31 | if ( x != string::npos ) { | ||
| 32 | return { x, y }; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | return { 0, 0 }; | ||
| 37 | }; | ||
| 38 | |||
| 39 | const auto predict_direction = [&](pos_t& current, char& direction) -> bool { | ||
| 40 | auto x = get<0>(current); // NOLINT | ||
| 41 | auto y = get<1>(current); // NOLINT | ||
| 42 | |||
| 43 | if ( direction == 'S' ) { | ||
| 44 | ++y; | ||
| 45 | if ( y >= puzzle.size() ) { | ||
| 46 | return false; | ||
| 47 | } | ||
| 48 | auto tile = puzzle[y][x]; | ||
| 49 | if ( tile != 'J' && tile != '|' && tile != 'L' ) { | ||
| 50 | return false; | ||
| 51 | } | ||
| 52 | if ( tile == 'J' ) { | ||
| 53 | direction = 'W'; | ||
| 54 | } | ||
| 55 | else if ( tile == 'L' ) { | ||
| 56 | direction = 'E'; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | else if ( direction == 'N' ) { | ||
| 60 | --y; | ||
| 61 | if ( y >= puzzle.size() ) { | ||
| 62 | return false; | ||
| 63 | } | ||
| 64 | auto tile = puzzle[y][x]; | ||
| 65 | if ( tile != '7' && tile != '|' && tile != 'F' ) { | ||
| 66 | return false; | ||
| 67 | } | ||
| 68 | if ( tile == '7' ) { | ||
| 69 | direction = 'W'; | ||
| 70 | } | ||
| 71 | else if ( tile == 'F' ) { | ||
| 72 | direction = 'E'; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | else if ( direction == 'E' ) { | ||
| 76 | ++x; | ||
| 77 | if ( x >= puzzle[y].size() ) { | ||
| 78 | return false; | ||
| 79 | } | ||
| 80 | auto tile = puzzle[y][x]; | ||
| 81 | if ( tile != 'J' && tile != '-' && tile != '7' ) { | ||
| 82 | return false; | ||
| 83 | } | ||
| 84 | if ( tile == 'J' ) { | ||
| 85 | direction = 'N'; | ||
| 86 | } | ||
| 87 | else if ( tile == '7' ) { | ||
| 88 | direction = 'S'; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | else if ( direction == 'W' ) { | ||
| 92 | --x; | ||
| 93 | if ( x >= puzzle[y].size() ) { | ||
| 94 | return false; | ||
| 95 | } | ||
| 96 | auto tile = puzzle[y][x]; | ||
| 97 | if ( tile != 'L' && tile != '-' && tile != 'F' ) { | ||
| 98 | return false; | ||
| 99 | } | ||
| 100 | if ( tile == 'L' ) { | ||
| 101 | direction = 'N'; | ||
| 102 | } | ||
| 103 | else if ( tile == 'F' ) { | ||
| 104 | direction = 'S'; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | else { | ||
| 108 | cerr << "invalid direction " << direction << ")!" << endl; | ||
| 109 | return false; | ||
| 110 | } | ||
| 111 | |||
| 112 | current = { x, y }; | ||
| 113 | return true; | ||
| 114 | }; | ||
| 115 | |||
| 116 | const auto start_pos = find_start_pos(); | ||
| 117 | |||
| 118 | auto max_steps = 0; | ||
| 119 | |||
| 120 | for ( const auto direction: { 'N', 'S', 'E', 'W' } ) { | ||
| 121 | auto current_direction = direction; | ||
| 122 | auto current_pos = start_pos; | ||
| 123 | |||
| 124 | auto steps = 0; | ||
| 125 | for ( ;; ) { | ||
| 126 | ++steps; | ||
| 127 | |||
| 128 | if ( !predict_direction(current_pos, current_direction) ) { | ||
| 129 | break; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | cout << "Richtung: " << direction << " - Schritte: " << steps << endl; | ||
| 133 | |||
| 134 | max_steps = max(max_steps, steps); | ||
| 135 | } | ||
| 136 | |||
| 137 | cout << max_steps / 2 << endl; | ||
| 138 | } | ||
| 139 | |||
| 140 | int | ||
| 141 | main() | ||
| 142 | { | ||
| 143 | part1(); | ||
| 144 | } | ||
