Solve 2024 day 6 part 1

This commit is contained in:
SebastianStork 2024-12-22 23:25:53 +01:00
parent 86f28a8c38
commit 18eecfad74
4 changed files with 229 additions and 0 deletions

7
2024/day-06/day-06.pro Normal file
View file

@ -0,0 +1,7 @@
TEMPLATE = app
CONFIG += console c++17
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.cpp

130
2024/day-06/input Normal file
View file

@ -0,0 +1,130 @@
............#...........................................#.......#.............#...........#.......................................
..........#....................#.........................................#............#............#.....##...................#...
.#.........#..........#.....#......##...............................................................#..................#..........
...........#.#...................#..................#.......................#........#............#...............................
...#..........................#............................#..................#..................#..........##..................#.
..............................................................................#...................................#..........#.#..
.#......##........#.........#...........#...............................#...................#........#..#............#.........##.
.....................................#...................................................................#........................
......................#......................#.............................................#......................................
....................................................#...........#........................#...................#......#.#.........##
..................#..#..........#...........................#.#..........#.......##....................#.#....#..#................
............#...#..........................#.............................................................................#........
...............................#.............#.........#..........................................................................
.....#......#........#.#...............#.........................#.......#.......#.........................#......................
...#....#........................##.................#..#...............................#..........#...............................
..........#......#..............................................................#...................#..........#.........#.#......
#.............#.........#.................................#....................#....................#......................#......
....#.............................................#..................#.........#...#................#........#....#..........#....
.......#......................................#.......#..........................................................#................
...#..#..............#.........#......#...........................................#..............................###.....#........
......................................................#........#..............................#..................#................
.......................................#.....#.........................................................................#..........
...........................#...........#....................................#.........#...........................................
....#........#...................................................................#...................................#............
....#..#.................................................#........................#...............................................
............................................................##..#......................#.......#...........#..#........#..........
........#.........................................................................................................................
............#......#.........#...............##...............................#......#......................#......#..............
.#......#...............................................#.#........................#....#.....#..#................##..............
.........#.....#.............#.........#.............................#.................#.........##.......#.......................
...#..............................................................................#....................#..........................
.#...#.....#..............#.................................................#..............................................#......
....................................................#...#..................................................................#......
..............#................#.......................................................................#.........#................
...........#..............................#.....#.........#..#............................#.......................................
.........................#............#..................................#...#.....#......#.......................................
..........#......................#...#....#......#.....................#..................................#.....#........#........
.............#...#........#.........................................#..#........................................................#.
.....#...............#......#.........##.....#.......................#..............#....................................#...#....
.....................#..........................................................................................#.............#...
.................#.............#..............##...................................................#..............................
..................................#.......#.......................................#...........#...................................
...#..............#............................#.#................................................................................
..........#.......#...........................#...........................#..............................#....................#...
.....#...#...#.................#..........................................................#............#.....................#.#..
...#...#......#.............#...............................................................................#...##................
..........#.....#.......#............#........................#..........................................#.............#..........
..................................................#.................#.........................#.#.......#.........................
.............................#...............................#.#............#................#...................#...............#
..#...........#........................#............................................#...........#............#....................
.......................#...#...........................#............#..#..#............#..#..............................#........
.......#...........#...............#.............................................................................#................
...........................#................................#....#....#...#..................#...................#.#..........##..
...#.#.......................................................................................#.....#..........#..........#........
....#......#.......................................................#.......#.#............................#.......................
.#.................#...#........#...#...........................#...#.................^......#............#.......................
...........#................##..........................................................................#.........................
...........................................................#....#............................#........#...........................
.#......#....#..#.......#....................#...............................................#........#...........................
......................##..............##.......................#...#...............#........................#................#....
#..................................................................................................#..........#...................
.....................#...................................................................................#............#.....#.....
..........................##.#............................................#........#.#.......#.#....#...#.........................
....#...............#.................................................................................#..#...........##...........
.................................................................#.#.....#.........#...............#..............................
.......................................................#.......#.........#............#....#..........#.....................#.....
#..............................................#..#..................#....................#.......................#....#....#.....
................#...............#..............................................................#....#...........................#.
.#...............#..#....................#...........................................#............................................
................................#...........................................#.........................#..#........................
.......#......#........................................#....................................................#.....................
.#......................#........................#...............#......................#...........#.............................
........#.......#..................#.......................#.........#.....................................................#.....#
..................#.....................#...#..........................................................#.............#............
....#........................#......#...#.....................#....##.........#...................................................
......................#....................#....................................................#..........#..................#...
.......#..........#.....................#.................................................#.......................................
....................#.......................................................#....##...................................#...........
................................#.............................................#.....#..............#..............................
..#.....#.......#..................................#........................#.....................................................
...##........#.#........#.........................................#..............#......................................#.........
........................................................................................#.........................................
.......................#......#...........................................................................#.......................
.........#...................#.............................................................#.........#............................
..........................#.................................................#................#...................................#
...............................#......................................#.........................#....#......#................#....
..........#.......................................................................................................................
....#......#.#.................#.#.......#.........#...#................#...............#..............#.....................#....
....#.........#....#................#..........#........#...................#.............#...............#..........#...#........
................#...............#.....#.....#...........#....#....................#.....#...................#.....................
.......................#.......................#...........................................................#.....................#
#.................................................................................................#...............................
.........................................##..#........#............................#............#........................#........
....................#...............#............#................................................................................
...#................#............#..............................................................#.#.................###...........
......#...........................#.......#....#...............#....................................................#.....#.......
..#.......##.....................#................#.....................................................#.#......................#
.................................................................#.................................##.............................
....#..............................................#................................................................#....#........
................#................................................#..............................................#...#...........#.
...................................##...............................................................................#.............
............#.................#..#.........................#..........................#.......#...................................
.......##........#.#...#......................................#..............#...............................#.............#......
............#.............................................##..............................#............................#..........
.................................................#..........................#.........#...#...............................#.......
..#............#......................#.....................................#.....##.........................#....................
...........#......#............#...................#.......................#..##........................................#.........
.................................##......#.........#.......#..................##...............#..................................
........#.........#...##.........#.#......#.........#.......#.....#.......................#.......................................
..................................#..............#................#.....#..............##...............#.........................
................#..........................................#..#.......#....................................#......................
..................................................#...........#..........................#.................#......................
.........#....................................................#..........#........................................#...............
..........................#..............................................#.................................................#......
...#....................................#.......................#.#.......#............##........#........................#.......
...#..................................#.....#...........................................................#...#........#............
................#...........#.........................................................#...............#....................#....#.
.................#...........#..............................#..........#........#............................#..........#.........
.........#.............................................#..........#....#.................#...................#...#................
......#....................#.................#.......#..............#............................#..#.......................#...#.
#.....#..........................#.....#...................................#...##.....#.....#...........#................#......#.
.##................#.........#.........#...#.#....................................................................................
.............................................................#..#...............#..#...............#.....#....#.............#..#..
.............#.........#.......#....#.........#...#......#........#.............................#.................................
#.....................................#.......#..................#..................#............#...#......#.........#...........
.......##............#.............#..............#.....#........#.................#..#..........#......................#..#......
......#......#.............#....#...............#.......#....................##.................................................##
...............#........#........#........................#.#........#.............#...................................#...##.....
................#........................#.......#......#..........................#................#............#...#....#.......
#.................#.....................#..........#.....................#...................##..........................#........

82
2024/day-06/main.cpp Normal file
View file

@ -0,0 +1,82 @@
#include <iostream>
#include <vector>
using namespace std;
void readInput(vector<string> &mappedArea, pair<int, int> &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<string> &mappedArea,
pair<int, int> &currentPosition,
int &currentOrientation,
int &distinctPositionCount)
{
pair<int, int> 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<string> mappedArea;
pair<int, int> 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;
}

10
2024/day-06/short-input Normal file
View file

@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...