day 1 done
This commit is contained in:
@@ -1,4 +1,14 @@
|
|||||||
|
#include "spdlog/common.h"
|
||||||
|
#include "spdlog/spdlog.h"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <charconv>
|
||||||
|
#include <cstdint>
|
||||||
#include <expected>
|
#include <expected>
|
||||||
|
#include <fstream>
|
||||||
|
#include <ios>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <string>
|
||||||
|
#include <sys/types.h>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -6,13 +16,72 @@ enum Direction {
|
|||||||
LEFT=-1, RIGHT=1
|
LEFT=-1, RIGHT=1
|
||||||
};
|
};
|
||||||
|
|
||||||
int main (int argc, char *argv[]) {
|
uint_fast32_t solution_part_one(std::vector<std::pair<Direction, int>> moves){
|
||||||
|
constexpr thread_local static int default_start = int {50};
|
||||||
|
|
||||||
|
int position = {default_start};
|
||||||
|
uint_fast32_t count_zero_cross = {0};
|
||||||
|
for (std::vector<std::pair<Direction, int>>::iterator i = moves.begin(); i<moves.end(); i++) {
|
||||||
|
position+=(i->first) * 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<std::pair<Direction, int>> moves){
|
||||||
|
constexpr thread_local static int default_start = int {50};
|
||||||
|
|
||||||
|
int position = {default_start};
|
||||||
|
uint_fast32_t count_zero_cross = {0};
|
||||||
|
for (std::vector<std::pair<Direction, int>>::iterator i = moves.begin(); i<moves.end(); i++) {
|
||||||
|
for (int steps =0; steps<i->second; 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<std::pair<Direction, int>> moves_to_submit = std::vector<std::pair<Direction, int>> {};
|
||||||
|
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<int>(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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int solution(std::vector<std::pair<Direction, int>> moves){
|
|
||||||
constexpr thread_local static int default_start = int {50};
|
|
||||||
|
|
||||||
return default_start;
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user