From 743caf28a89bf3520ed9e20f6aa229a1fa210bd9 Mon Sep 17 00:00:00 2001 From: Jelle van Kraaij Date: Sun, 10 Dec 2023 20:09:55 +0100 Subject: [PATCH] day10 --- day10/main.cpp | 103 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 80 insertions(+), 23 deletions(-) diff --git a/day10/main.cpp b/day10/main.cpp index a76e2ca..ba920dd 100644 --- a/day10/main.cpp +++ b/day10/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -10,15 +11,22 @@ using namespace std; -enum class Tile { - GROUND = '.', - VERTICAL = '|', - HORIZONTAL = '-', - BEND_NE = 'L', - BEND_NW = 'J', - BEND_SW = '7', - BEND_SE = 'F', - START = 'S', +class Tile { + public: + enum TileType { + GROUND = '.', + VERTICAL = '|', + HORIZONTAL = '-', + BEND_NE = 'L', + BEND_NW = 'J', + BEND_SW = '7', + BEND_SE = 'F', + START = 'S', + }; + TileType type; + Tile(TileType type) : type(type) {} + bool isPartOfPath = false; + bool isBend() { return type == Tile::BEND_NE || type == Tile::BEND_NW || type == Tile::BEND_SW || type == Tile::BEND_SE; } }; int main(int argc, char **argv) { @@ -38,13 +46,13 @@ int main(int argc, char **argv) { vector> map; while (getline(inputFile, line)) { vector row; - transform(line.begin(), line.end(), back_inserter(row), [](char c) { return static_cast(c); }); + transform(line.begin(), line.end(), back_inserter(row), [](char c) { return static_cast(c); }); map.push_back(row); } Vec2 startLocation; for (auto it = map.begin(); it != map.end(); ++it) { - auto res = find(it->begin(), it->end(), Tile::START); + auto res = find_if(it->begin(), it->end(), [](Tile t) { return t.type == Tile::START; }); if (res != it->end()) { startLocation[0] = distance(map.begin(), it); startLocation[1] = distance(it->begin(), res); @@ -53,35 +61,36 @@ int main(int argc, char **argv) { } } - static const array>, 4> directions = {{{Vec2{0, 1}, {Tile::HORIZONTAL, Tile::BEND_SW, Tile::BEND_NW}}, - {Vec2{0, -1}, {Tile::HORIZONTAL, Tile::BEND_SE, Tile::BEND_NE}}, - {Vec2{1, 0}, {Tile::VERTICAL, Tile::BEND_NW, Tile::BEND_NE}}, - {Vec2{-1, 0}, {Tile::VERTICAL, Tile::BEND_SW, Tile::BEND_SE}}}}; + static const array>, 4> directions = {{{Vec2{0, 1}, {Tile::HORIZONTAL, Tile::BEND_SW, Tile::BEND_NW}}, + {Vec2{0, -1}, {Tile::HORIZONTAL, Tile::BEND_SE, Tile::BEND_NE}}, + {Vec2{1, 0}, {Tile::VERTICAL, Tile::BEND_NW, Tile::BEND_NE}}, + {Vec2{-1, 0}, {Tile::VERTICAL, Tile::BEND_SW, Tile::BEND_SE}}}}; Vec2 startDirection; for (auto &direction : directions) { Vec2 testLocation = startLocation + direction.first; if (testLocation[0] < 0 || testLocation[0] >= map.size() || testLocation[1] < 0 || testLocation[1] >= map[testLocation[0]].size()) { continue; } - if (find(direction.second.begin(), direction.second.end(), map[testLocation[0]][testLocation[1]]) != direction.second.end()) { + if (find(direction.second.begin(), direction.second.end(), map[testLocation[0]][testLocation[1]].type) != direction.second.end()) { startDirection = direction.first; break; } } - static const unordered_map> tileDirections = {{Tile::BEND_NE, {{-1, 0}, {0, 1}}}, {Tile::BEND_NW, {{0, -1}, {-1, 0}}}, - {Tile::BEND_SW, {{1, 0}, {0, -1}}}, {Tile::BEND_SE, {{0, 1}, {1, 0}}}, - {Tile::HORIZONTAL, {{0, 1}, {0, -1}}}, {Tile::VERTICAL, {{1, 0}, {-1, 0}}}}; + static const unordered_map> tileDirections = { + {Tile::BEND_NE, {{-1, 0}, {0, 1}}}, {Tile::BEND_NW, {{0, -1}, {-1, 0}}}, {Tile::BEND_SW, {{1, 0}, {0, -1}}}, + {Tile::BEND_SE, {{0, 1}, {1, 0}}}, {Tile::HORIZONTAL, {{0, 1}, {0, -1}}}, {Tile::VERTICAL, {{1, 0}, {-1, 0}}}}; Vec2 currentLocation = startLocation + startDirection; Vec2 currentDirection = startDirection; int steps = 0; while (true) { steps++; - if (map[currentLocation[0]][currentLocation[1]] == Tile::START) { + map[currentLocation[0]][currentLocation[1]].isPartOfPath = true; + if (map[currentLocation[0]][currentLocation[1]].type == Tile::START) { break; } - pair direction = tileDirections.at(map[currentLocation[0]][currentLocation[1]]); + pair direction = tileDirections.at(map[currentLocation[0]][currentLocation[1]].type); if (currentDirection == inverse(direction.first)) { currentDirection = direction.second; } else { @@ -90,5 +99,53 @@ int main(int argc, char **argv) { currentLocation = currentLocation + currentDirection; } cout << "Steps: " << steps << endl; - cout << "Result: " << steps / 2 << endl; -} \ No newline at end of file + + static const std::map, Tile::TileType> convertStart = { + {{{0, 1}, {0, -1}}, Tile::HORIZONTAL}, {{{0, -1}, {0, 1}}, Tile::HORIZONTAL}, {{{1, 0}, {-1, 0}}, Tile::VERTICAL}, + {{{-1, 0}, {1, 0}}, Tile::VERTICAL}, {{{0, 1}, {1, 0}}, Tile::BEND_SW}, {{{1, 0}, {0, 1}}, Tile::BEND_SW}, + {{{0, -1}, {1, 0}}, Tile::BEND_SE}, {{{1, 0}, {0, -1}}, Tile::BEND_SE}, {{{0, -1}, {-1, 0}}, Tile::BEND_NE}, + {{{-1, 0}, {0, -1}}, Tile::BEND_NE}, {{{0, 1}, {-1, 0}}, Tile::BEND_NW}, {{{-1, 0}, {0, 1}}, Tile::BEND_NW}, + }; + map[startLocation[0]][startLocation[1]].type = convertStart.at(make_pair(startDirection, inverse(currentDirection))); + + cout << "Start value: " << static_cast(map[startLocation[0]][startLocation[1]].type) << endl; + + enum Direction { UP, DOWN }; + + unordered_map bendDirections = { + {Tile::BEND_NE, UP}, {Tile::BEND_NW, UP}, {Tile::BEND_SW, DOWN}, {Tile::BEND_SE, DOWN}}; + + int total = 0; + for (auto &row : map) { + bool insidePath = false; + bool encounteredBend = false; + Direction lastDir; + for (auto &tile : row) { + if (tile.isPartOfPath) { + if (tile.type == Tile::VERTICAL) + insidePath = !insidePath; + else if (tile.isBend()) { + if (!encounteredBend) { + encounteredBend = true; + lastDir = bendDirections.at(tile.type); + } else { + if (lastDir != bendDirections.at(tile.type)) + insidePath = !insidePath; + encounteredBend = false; + } + } + } + + if (insidePath && !tile.isPartOfPath) { + cout << 'I'; + total++; + } else { + cout << static_cast(tile.type); + } + } + cout << endl; + } + + cout << "Result 1: " << steps / 2 << endl; + cout << "Result 2: " << total << endl; +}