From 7949fb91c550a7a39093b7cc58ef73d2e0cfffb1 Mon Sep 17 00:00:00 2001 From: Mars Ultor Date: Mon, 1 Dec 2025 21:13:47 -0600 Subject: [PATCH] day 1 done --- src/day-1-solution.cpp | 79 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 5 deletions(-) diff --git a/src/day-1-solution.cpp b/src/day-1-solution.cpp index 1117a34..11ec6b3 100644 --- a/src/day-1-solution.cpp +++ b/src/day-1-solution.cpp @@ -1,4 +1,14 @@ +#include "spdlog/common.h" +#include "spdlog/spdlog.h" +#include +#include +#include #include +#include +#include +#include +#include +#include #include #include @@ -6,13 +16,72 @@ enum Direction { LEFT=-1, RIGHT=1 }; -int main (int argc, char *argv[]) { +uint_fast32_t solution_part_one(std::vector> moves){ + constexpr thread_local static int default_start = int {50}; + + int position = {default_start}; + uint_fast32_t count_zero_cross = {0}; + for (std::vector>::iterator i = moves.begin(); ifirst) * i->second; + position %=100; + position = position<0 ? position +100 : position; + spdlog::debug("Current position: {}", position); + if(position==0)++count_zero_cross; + } + return count_zero_cross; +} + + +uint_fast32_t solution_part_two(std::vector> moves){ + constexpr thread_local static int default_start = int {50}; + + int position = {default_start}; + uint_fast32_t count_zero_cross = {0}; + for (std::vector>::iterator i = moves.begin(); isecond; steps++) { + position = (position + i->first + 100) % 100; + if (position==0) count_zero_cross++; + } + spdlog::debug("Current position: {}", position); + } + + return count_zero_cross; +} + + +int main (int argc, char *argv[]) { + spdlog::set_level(spdlog::level::level_enum::info); + std::vector> moves_to_submit = std::vector> {}; + std::ifstream input = std::ifstream{"testcases/day-1.in", std::ios::ios_base::binary}; + + { + std::string current_line = std::string {}; + while (std::getline(input, current_line)) { + char direction_char = current_line.at(0); + int magnitude = int {}; + std::from_chars(current_line.data()+1, current_line.data()+current_line.size(), magnitude); + Direction chosen; + switch (direction_char) { + case 'R': + chosen = Direction::RIGHT; + break; + case 'L': + chosen = Direction::LEFT; + break; + default: + spdlog::critical("parsing error done on line: {}", current_line); + } + moves_to_submit.push_back(std::make_pair(chosen, magnitude)); + spdlog::debug("added chosen: {}, magnitude: {}", static_cast(chosen), magnitude); + } + } + + uint_fast32_t part_one_solution = solution_part_one(moves_to_submit); + spdlog::info("Part One Solution {}", part_one_solution); + uint_fast32_t part_two_solution = solution_part_two(moves_to_submit); + spdlog::info("Part Two Solution {}", part_two_solution); return 0; } -int solution(std::vector> moves){ - constexpr thread_local static int default_start = int {50}; - return default_start; -}