From f9c90b42ded405569e5ea4f0588e0e6d7e764726 Mon Sep 17 00:00:00 2001 From: David Vereb Date: Fri, 6 Dec 2024 10:07:14 -0500 Subject: [PATCH] Day 6 Part 1. --- 2024/6/Makefile | 2 + 2024/6/data.txt | 130 ++++++++++++++++++++++++++++++++ 2024/6/data_test.txt | 10 +++ 2024/6/main.cpp | 175 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 317 insertions(+) create mode 100644 2024/6/Makefile create mode 100644 2024/6/data.txt create mode 100644 2024/6/data_test.txt create mode 100644 2024/6/main.cpp diff --git a/2024/6/Makefile b/2024/6/Makefile new file mode 100644 index 0000000..638e325 --- /dev/null +++ b/2024/6/Makefile @@ -0,0 +1,2 @@ +a.out: main.cpp + clang++ -std=c++2b -g -O0 main.cpp diff --git a/2024/6/data.txt b/2024/6/data.txt new file mode 100644 index 0000000..fed9be4 --- /dev/null +++ b/2024/6/data.txt @@ -0,0 +1,130 @@ +.......................#....#....................#....#..##.##.........................................#...........#...........#.. +.............#........................................#....................#............#.......................#............#.... +.........#.............#..#....#..........................#.......#.#.......#...............................................##.... +..........#....#....................................................#....................#...........#....#....................... +.............#...#....................#...........#.........#...........................#............#............................ +..........................................................................................#.....#.......#...................##.... +...............................................#.#......#..............#...#...................##...........#....#..#...#......... +..#.................#......#........#......................#.......#...............................#.............................. +.........................#...............#.....................................#....#..#..#............................#.......... +......................#.................................................................#..........#..............#...#......#.... +............................#.....##..........#.....................#..........#..#.......#....................................... +......#...###...........#.................................................................#....................................... +......................#.#..#................#..........#................#...........#............#..............#...#.........#... +....#.........................#..#...............................................#....#.............#............................. +..#............#...#............#..........#...............#.....#....##.........................#........#....................... +..................#............#..............#..........................................................#..#..#........#......... +.......#...#............#..........................................................#..............................#.............#. +..#................#....................#.#...#.................................#.........#....................#.........#........ +...................................#..#..........................#........#.....#.........................................#....... +...........##.................##............#................#...#..................................##...#........................ +..............#..................#....................................................................#........................... +.#...#....#..........##..................................#.............................................................#......#... +......#.....#................................#...............................................................#......#............. +..........#.............................................#............................##....#.........................#............ +..........#......#.................#...#........#...........#.#..........#........#....................................#.......... +.#..................#.#............................#.......................#.........#.......#.................................... +.....................#....#..................#..................................................##..#................#............ +......#......................#..#..............##.......#..................................................#..............#....... +..........................................................................#........#...#..............#.................#......... +....................##....#.......................#........#...................#.............##.......#........................... +.........#............#..................................................................................#........................ +........................................................................#................#........................#.............#. +............#.......#.................................#............#.............................................................. +.#.............#...............................................#....#.........#....#..................................#.#......... +.......................................#.....................................................................................#.... +........#........................................#...............#........................................................#....... +...............................#.........#........#...........#.......................................................#.........#. +.....................##...#....................................................#............#..................#..#..........##... +................#..........#...#............................................................#...##................................ +...................#........#...............................#.................#.........#......................................... +.................................................................................................................#................ +............................#......................................................#.....................................#......#. +......#................#............................#............................................................................. +..#...........#.....#.........#....#................#............................................#................................ +..................................#.......#.......#...........#.........#................#.....................................#.# +...............................................#............................................#.#.....................#.......##.... +..............................#...............#..............................#...#...#.............................#...........#.. +.......#...........#..........................................................#........#..#.........#.....#.....#...#............. +......................................................#.#............#.........#.#...........#........##.....#.................... +...........................................................................#..........#.........................#.........#....... +....#.........................#.....................................................................................#...#......#.. +...#.......#............................................................#...............................#....................##.#. +.................................#.....#................................................................#......................... +.....................................#........................#....................................#.............................. +..............#.......................................................................#.......#....................#..........#... +................................#..........................................#.................................#.....#.............. +...........#.......................................................#...............#.....................#..............#......#.. +..................................#..........................#.................................................................#.. +.............##......#....................................................#......#...............................................# +#.......#...................#..........#...................#.................#..................................................#. +.......#..........#.....##...........#.......................#............#..............................#.............#.......... +.............#...................##......................................................................................#........ +#.............................#...................................................................#............................... +.#...................................#....##..................................................................................#... +.................................#.#..................#..#..#..........................#.......#.................................. +..................#....#....#....................................................#....................................#........#.. +..........................................#...#.....#...........#.........#............#............#..........................#.. +...............................................................................#..............................................#... +.............................#......#....................#....#....................................................#.#.#.......... +................##......#........#........#...........#..........................#......................#..............#.......... +.....................#.....#..#.............................................................................................#..... +.......#..#...........#................................................................#.......................................... +#.......................................................................................................#...#..................... +.#..........................................................#.....................................................#............... +#.....#...#...............#..#......#........#..#...................#...................#.................#....................... +..#.................................................................................#.#......................#.................... +.......................#.............#..........#.......................#...................................#..................... +.................#......#......#...#.#.#.............................#.......................................................#.... +..#...........................................................................................................#................... +.................................#..................#.....#........#.........................................................#.... +.............#......................#...............................................#...#.............................#...#....#.. +.#............................#..........................#.#...........................#..........................#.#............. +.............................#.........#.................##............................#...................#...................... +.........#......#....#.........#..............................................................................................#..# +.....................#......#.................#..#..........................#..................#..............................#.#. +........................................................#......................#............#.......#......#...................... +........................................#....##.#........#......#..............................#..........#..........#.##......... +.............#.......#.......#......................#..........................................#.........#............#...#...#.#. +..................#........................................................#...............................#.....#.....#.......... +.........#.................#........#..............^..............#.................##......#...................................#. +....#...................#.#.................................#...#.................................................#............... +.................#.............................................#................#.....#......#.......#...................#........ +...............#...............................................................................#..#...............#............... +........................................##..........................................................................##............ +............................................#...................#........#...#...#....#.........#...................#............. +.....#...#...................#....................................#......#...........#...............#............................ +........................#..................#.....................#.................................................#......##...... +...#.................#....................#..................................#.#.........................#.......#................ +....................#............#..................................#...#......#....................#...............#............. +.....#...............#.......###........#............#......................#.......#....#........................................ +....................#........#...........#..................................#..#................#.............#................... +.....#..................#...................#.#..................................#.......#..#..................................... +..#..#.......#........................................#.................................................................#......... +.......#............#...#....#.......................#.#...................................#.........................#..#.#....... +..............#......................................................................................................#............ +............#....................#........................#.........#...#......................................................... +...................................#..............................#.........................................................#..... +......##...........#.#........................#.........................................................................#......... +..........................#.....................................................#..............................#..............#... +.........................#...#.....................#..#................#................................................#....#.... +...#.................................#...#.......#...............................................................#................ +..................................#....................................#...........................##..#.#.....................#.. +.#.............#.....#...............................................................#...............#...............#..#......... +.........#.............................#........................#.............#......................#........#............#...... +...................................#..#...#.....................................#................................................. +........................................##..........#..............................................#.....................#........ +......#...........................................#....#.....................#...................#..#.........#................... +.#....#.....#........##..................................#......#................................................#....#..##....... +..........#....................................................................##.........................#...........#........... +...........#.#......#.......#..................#............#...............................................#...#..............#.. +.................................................................#.......#.........#.....#........#....##....#..........#......... +.............................#......................................................#.....#...#...#....#...........#..........#.#. +.....................#........#.........................#......................................#................#...............#. +.........................................................................#...................................................#.... +..............#...#..#................#....................................#.......#....#.#...............#.........#............. +.......#......#.#............................................#.......#............................................................ +##......................................................#..#.................................##...................#..#...........# +...........#.#...#........#............#..............................#........................#.........................#..#..... +................................................##.........................................#.................#.................... +............................#....................................................#...#........................#.#......#........#. diff --git a/2024/6/data_test.txt b/2024/6/data_test.txt new file mode 100644 index 0000000..a4eb402 --- /dev/null +++ b/2024/6/data_test.txt @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... diff --git a/2024/6/main.cpp b/2024/6/main.cpp new file mode 100644 index 0000000..eb824ed --- /dev/null +++ b/2024/6/main.cpp @@ -0,0 +1,175 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct Position { + // NOTE(dev): Could do enum empty/reached/blocked, but I'm not interested. + bool blocked = false; + bool reached = false; +}; + +enum Direction { + UP = 0, + RIGHT, + DOWN, + LEFT +}; + +struct Player { + std::pair location; + Direction direction = Direction::UP; +}; + +int main() +{ + const std::string filename = "data.txt"; + std::ifstream ifs(filename); + if(!ifs.is_open()) + { + std::cerr << "Missing " << filename << "." << std::endl; + return -1; + } + + unsigned long total = 0; + unsigned long total_pt2 = 0; + + std::map, Position> level; + Player player; + + int max_x = 0; + int max_y = 0; + bool first_row = true; + int y = 0; + + auto DebugPrint = [&]() + { + std::cout << std::endl; + for(auto y = 0; y < max_y; ++y) + { + for(auto x = 0; x < max_x; ++x) + { + if(player.location.first == x && player.location.second == y) + { + switch(player.direction) + { + case Direction::UP: + std::cout << "^"; + break; + case Direction::RIGHT: + std::cout << ">"; + break; + case Direction::DOWN: + std::cout << "v"; + break; + case Direction::LEFT: + std::cout << "<"; + break; + }; + } + else + { + const auto &pos = level[{x, y}]; + if(pos.reached) + std::cout << "X"; + else if(pos.blocked) + std::cout << "#"; + else + std::cout << "."; + } + } + std::cout << std::endl; + } + }; + + // Parse Input + for(std::string line; std::getline(ifs, line); ) + { + if(line == "") + continue; + + int x = 0; + for(const char &value : line) + { + Position pos; + if(value == '#') + pos.blocked = true; + if(value == '^') // always up? + { + pos.reached = true; + player.location = {x, y}; + } + + level[{x, y}] = pos; + + ++x; + } + if(first_row) + { + max_x = x; + first_row = false; + } + ++y; + } + max_y = y; + + // DEBUG: + DebugPrint(); + + std::cout << std::endl; + + // while(player.location.first >= 0 && player.location.first < max_x && + // player.location.second >= 0 && player.location.second < max_y) + while(true) + { + std::pair next_location = player.location; + switch(player.direction) + { + case Direction::UP: + --next_location.second; + break; + case Direction::RIGHT: + ++next_location.first; + break; + case Direction::DOWN: + ++next_location.second; + break; + case Direction::LEFT: + --next_location.first; + break; + }; + + if(!level.contains(next_location)) + { + player.location = next_location; + break; + } + if(level[next_location].blocked) + player.direction = static_cast(((int(player.direction)) + 1) % 4); + else + { + level[next_location].reached = true; + player.location = next_location; + // DebugPrint(); + // usleep(20000); + } + } + + DebugPrint(); + + for(auto y = 0; y < max_y; ++y) + for(auto x = 0; x < max_x; ++x) + if(level[{x,y}].reached) + ++total; + + std::cout << std::endl; + std::cout << " Total: " << total << std::endl; + std::cout << "PT2 Total: " << total_pt2 << std::endl; + + return 0; +}