diff --git a/2024/day-06/day-06.pro b/2024/day-06/day-06.pro new file mode 100644 index 0000000..595bdaa --- /dev/null +++ b/2024/day-06/day-06.pro @@ -0,0 +1,7 @@ +TEMPLATE = app +CONFIG += console c++17 +CONFIG -= app_bundle +CONFIG -= qt + +SOURCES += \ + main.cpp diff --git a/2024/day-06/input b/2024/day-06/input new file mode 100644 index 0000000..d04f30e --- /dev/null +++ b/2024/day-06/input @@ -0,0 +1,130 @@ +............#...........................................#.......#.............#...........#....................................... +..........#....................#.........................................#............#............#.....##...................#... +.#.........#..........#.....#......##...............................................................#..................#.......... +...........#.#...................#..................#.......................#........#............#............................... +...#..........................#............................#..................#..................#..........##..................#. +..............................................................................#...................................#..........#.#.. +.#......##........#.........#...........#...............................#...................#........#..#............#.........##. +.....................................#...................................................................#........................ +......................#......................#.............................................#...................................... +....................................................#...........#........................#...................#......#.#.........## +..................#..#..........#...........................#.#..........#.......##....................#.#....#..#................ +............#...#..........................#.............................................................................#........ +...............................#.............#.........#.......................................................................... +.....#......#........#.#...............#.........................#.......#.......#.........................#...................... +...#....#........................##.................#..#...............................#..........#............................... +..........#......#..............................................................#...................#..........#.........#.#...... +#.............#.........#.................................#....................#....................#......................#...... +....#.............................................#..................#.........#...#................#........#....#..........#.... +.......#......................................#.......#..........................................................#................ +...#..#..............#.........#......#...........................................#..............................###.....#........ +......................................................#........#..............................#..................#................ +.......................................#.....#.........................................................................#.......... +...........................#...........#....................................#.........#........................................... +....#........#...................................................................#...................................#............ +....#..#.................................................#........................#............................................... +............................................................##..#......................#.......#...........#..#........#.......... +........#......................................................................................................................... +............#......#.........#...............##...............................#......#......................#......#.............. +.#......#...............................................#.#........................#....#.....#..#................##.............. +.........#.....#.............#.........#.............................#.................#.........##.......#....................... +...#..............................................................................#....................#.......................... +.#...#.....#..............#.................................................#..............................................#...... +....................................................#...#..................................................................#...... +..............#................#.......................................................................#.........#................ +...........#..............................#.....#.........#..#............................#....................................... +.........................#............#..................................#...#.....#......#....................................... +..........#......................#...#....#......#.....................#..................................#.....#........#........ +.............#...#........#.........................................#..#........................................................#. +.....#...............#......#.........##.....#.......................#..............#....................................#...#.... +.....................#..........................................................................................#.............#... +.................#.............#..............##...................................................#.............................. +..................................#.......#.......................................#...........#................................... +...#..............#............................#.#................................................................................ +..........#.......#...........................#...........................#..............................#....................#... +.....#...#...#.................#..........................................................#............#.....................#.#.. +...#...#......#.............#...............................................................................#...##................ +..........#.....#.......#............#........................#..........................................#.............#.......... +..................................................#.................#.........................#.#.......#......................... +.............................#...............................#.#............#................#...................#...............# +..#...........#........................#............................................#...........#............#.................... +.......................#...#...........................#............#..#..#............#..#..............................#........ +.......#...........#...............#.............................................................................#................ +...........................#................................#....#....#...#..................#...................#.#..........##.. +...#.#.......................................................................................#.....#..........#..........#........ +....#......#.......................................................#.......#.#............................#....................... +.#.................#...#........#...#...........................#...#.................^......#............#....................... +...........#................##..........................................................................#......................... +...........................................................#....#............................#........#........................... +.#......#....#..#.......#....................#...............................................#........#........................... +......................##..............##.......................#...#...............#........................#................#.... +#..................................................................................................#..........#................... +.....................#...................................................................................#............#.....#..... +..........................##.#............................................#........#.#.......#.#....#...#......................... +....#...............#.................................................................................#..#...........##........... +.................................................................#.#.....#.........#...............#.............................. +.......................................................#.......#.........#............#....#..........#.....................#..... +#..............................................#..#..................#....................#.......................#....#....#..... +................#...............#..............................................................#....#...........................#. +.#...............#..#....................#...........................................#............................................ +................................#...........................................#.........................#..#........................ +.......#......#........................................#....................................................#..................... +.#......................#........................#...............#......................#...........#............................. +........#.......#..................#.......................#.........#.....................................................#.....# +..................#.....................#...#..........................................................#.............#............ +....#........................#......#...#.....................#....##.........#................................................... +......................#....................#....................................................#..........#..................#... +.......#..........#.....................#.................................................#....................................... +....................#.......................................................#....##...................................#........... +................................#.............................................#.....#..............#.............................. +..#.....#.......#..................................#........................#..................................................... +...##........#.#........#.........................................#..............#......................................#......... +........................................................................................#......................................... +.......................#......#...........................................................................#....................... +.........#...................#.............................................................#.........#............................ +..........................#.................................................#................#...................................# +...............................#......................................#.........................#....#......#................#.... +..........#....................................................................................................................... +....#......#.#.................#.#.......#.........#...#................#...............#..............#.....................#.... +....#.........#....#................#..........#........#...................#.............#...............#..........#...#........ +................#...............#.....#.....#...........#....#....................#.....#...................#..................... +.......................#.......................#...........................................................#.....................# +#.................................................................................................#............................... +.........................................##..#........#............................#............#........................#........ +....................#...............#............#................................................................................ +...#................#............#..............................................................#.#.................###........... +......#...........................#.......#....#...............#....................................................#.....#....... +..#.......##.....................#................#.....................................................#.#......................# +.................................................................#.................................##............................. +....#..............................................#................................................................#....#........ +................#................................................#..............................................#...#...........#. +...................................##...............................................................................#............. +............#.................#..#.........................#..........................#.......#................................... +.......##........#.#...#......................................#..............#...............................#.............#...... +............#.............................................##..............................#............................#.......... +.................................................#..........................#.........#...#...............................#....... +..#............#......................#.....................................#.....##.........................#.................... +...........#......#............#...................#.......................#..##........................................#......... +.................................##......#.........#.......#..................##...............#.................................. +........#.........#...##.........#.#......#.........#.......#.....#.......................#....................................... +..................................#..............#................#.....#..............##...............#......................... +................#..........................................#..#.......#....................................#...................... +..................................................#...........#..........................#.................#...................... +.........#....................................................#..........#........................................#............... +..........................#..............................................#.................................................#...... +...#....................................#.......................#.#.......#............##........#........................#....... +...#..................................#.....#...........................................................#...#........#............ +................#...........#.........................................................#...............#....................#....#. +.................#...........#..............................#..........#........#............................#..........#......... +.........#.............................................#..........#....#.................#...................#...#................ +......#....................#.................#.......#..............#............................#..#.......................#...#. +#.....#..........................#.....#...................................#...##.....#.....#...........#................#......#. +.##................#.........#.........#...#.#.................................................................................... +.............................................................#..#...............#..#...............#.....#....#.............#..#.. +.............#.........#.......#....#.........#...#......#........#.............................#................................. +#.....................................#.......#..................#..................#............#...#......#.........#........... +.......##............#.............#..............#.....#........#.................#..#..........#......................#..#...... +......#......#.............#....#...............#.......#....................##.................................................## +...............#........#........#........................#.#........#.............#...................................#...##..... +................#........................#.......#......#..........................#................#............#...#....#....... +#.................#.....................#..........#.....................#...................##..........................#........ diff --git a/2024/day-06/main.cpp b/2024/day-06/main.cpp new file mode 100644 index 0000000..19fc854 --- /dev/null +++ b/2024/day-06/main.cpp @@ -0,0 +1,82 @@ +#include +#include + +using namespace std; + +void readInput(vector &mappedArea, pair &startingPosition) +{ + string line; + for (size_t rowIndex = 0; getline(cin, line); rowIndex++) { + for (size_t colIndex = 0; colIndex < line.size(); colIndex++) { + if (line[colIndex] == '^') { + startingPosition = {rowIndex, colIndex}; + } + } + mappedArea.push_back(line); + } +} + +bool takeStep(vector &mappedArea, + pair ¤tPosition, + int ¤tOrientation, + int &distinctPositionCount) +{ + pair nextPosition = currentPosition; + + switch (currentOrientation) { + case 0: + nextPosition.first--; + break; + case 1: + nextPosition.second++; + break; + case 2: + nextPosition.first++; + break; + case 3: + nextPosition.second--; + break; + default: + break; + } + + if ((nextPosition.first >= mappedArea.size() || nextPosition.first < 0) + || (nextPosition.second >= mappedArea.front().size() || nextPosition.second < 0)) { + mappedArea[currentPosition.first][currentPosition.second] = '#'; + return false; + } + + if (mappedArea[nextPosition.first][nextPosition.second] == '#') { + currentOrientation = ++currentOrientation % 4; + return takeStep(mappedArea, currentPosition, currentOrientation, distinctPositionCount); + } + + distinctPositionCount += !(mappedArea[nextPosition.first][nextPosition.second] == 'X'); + + mappedArea[currentPosition.first][currentPosition.second] = 'X'; + mappedArea[nextPosition.first][nextPosition.second] = '^'; + + currentPosition = nextPosition; + return true; +} + +int main() +{ + vector mappedArea; + pair currentPosition; + readInput(mappedArea, currentPosition); + + int currentOrientation = 0; + int distinctPositionCount = 1; + while (takeStep(mappedArea, currentPosition, currentOrientation, distinctPositionCount)) + ; + + for (auto row : mappedArea) { + cout << row << endl; + } + cout << endl; + + // Part one + cout << "How many distinct positions will the guard visit before leaving the mapped area? " + << distinctPositionCount << endl; +} diff --git a/2024/day-06/short-input b/2024/day-06/short-input new file mode 100644 index 0000000..a4eb402 --- /dev/null +++ b/2024/day-06/short-input @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#...