update
This commit is contained in:
parent
38004618a6
commit
35d509754f
@ -46,18 +46,20 @@ add_executable(AdventOfCode2023 main.cpp)
|
|||||||
|
|
||||||
add_subdirectory(pb_1)
|
add_subdirectory(pb_1)
|
||||||
|
|
||||||
set_target_properties(AdventOfCode2023 pb_1 PROPERTIES
|
add_subdirectory(pb_2)
|
||||||
|
|
||||||
|
set_target_properties(AdventOfCode2023 pb_1 pb_2 PROPERTIES
|
||||||
CXX_STANDARD 20
|
CXX_STANDARD 20
|
||||||
CXX_STANDARD_REQUIRED ON
|
CXX_STANDARD_REQUIRED ON
|
||||||
CXX_EXTENSIONS OFF
|
CXX_EXTENSIONS OFF
|
||||||
INTERPROCEDURAL_OPTIMIZATION ON
|
# INTERPROCEDURAL_OPTIMIZATION ON
|
||||||
UNITY_BUILD ON
|
# UNITY_BUILD ON
|
||||||
)
|
)
|
||||||
|
|
||||||
target_compile_definitions(AdventOfCode2023 PUBLIC $<$<AND:$<CONFIG:Debug>,$<STREQUAL:$<CXX_COMPILER_ID>,GNU>>:_GLIBCXX_DEBUG>)
|
#target_compile_definitions(AdventOfCode2023 PUBLIC $<$<AND:$<CONFIG:Debug>,$<STREQUAL:$<CXX_COMPILER_ID>,GNU>>:_GLIBCXX_DEBUG>)
|
||||||
|
|
||||||
target_compile_options(AdventOfCode2023 PUBLIC ${COMPILE_FLAGS})
|
#target_compile_options(AdventOfCode2023 PUBLIC ${COMPILE_FLAGS})
|
||||||
|
|
||||||
target_link_options(AdventOfCode2023 PUBLIC ${LINKER_OPTIONS})
|
#target_link_options(AdventOfCode2023 PUBLIC ${LINKER_OPTIONS})
|
||||||
|
|
||||||
target_link_libraries(AdventOfCode2023 PUBLIC pb_1 ${LINKER_FLAGS})
|
target_link_libraries(AdventOfCode2023 PUBLIC pb_1 pb_2 ${LINKER_FLAGS})
|
||||||
|
100
inputs/pb2.txt
Normal file
100
inputs/pb2.txt
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
Game 1: 2 blue, 3 red; 3 green, 3 blue, 6 red; 4 blue, 6 red; 2 green, 2 blue, 9 red; 2 red, 4 blue
|
||||||
|
Game 2: 4 red, 1 green; 3 red; 13 green, 5 red, 3 blue; 3 green, 2 red; 3 blue, 5 red, 3 green; 2 red, 3 blue, 12 green
|
||||||
|
Game 3: 4 red, 1 green, 1 blue; 1 red, 1 blue; 6 red, 1 green; 6 red, 3 blue, 1 green; 4 red
|
||||||
|
Game 4: 4 blue, 12 red, 4 green; 6 green, 3 blue, 19 red; 3 blue, 2 red, 2 green
|
||||||
|
Game 5: 1 red, 5 blue, 16 green; 1 red, 6 green, 3 blue; 2 red, 12 blue; 17 blue, 3 green; 7 green, 2 red, 6 blue
|
||||||
|
Game 6: 3 green, 1 blue, 5 red; 5 green, 5 red; 2 green, 2 blue, 3 red; 5 green, 2 red; 3 green, 6 red, 3 blue; 5 green, 4 red
|
||||||
|
Game 7: 15 blue, 1 red, 6 green; 4 blue, 7 green, 2 red; 14 blue, 5 green, 2 red
|
||||||
|
Game 8: 6 blue, 3 green, 10 red; 2 blue, 1 green, 5 red; 6 blue, 3 green, 12 red; 11 red, 1 green, 1 blue; 5 blue, 14 red, 3 green; 3 red
|
||||||
|
Game 9: 15 red, 3 blue; 1 blue, 16 red; 1 red, 3 blue; 1 blue, 1 green, 9 red
|
||||||
|
Game 10: 1 red; 1 blue, 7 green; 1 green, 5 blue; 3 blue, 3 green; 1 green
|
||||||
|
Game 11: 19 blue, 13 green; 19 blue, 2 green; 10 blue, 3 red, 12 green; 11 blue, 1 red, 6 green
|
||||||
|
Game 12: 7 green, 5 blue; 6 green, 3 red, 6 blue; 2 red, 5 blue, 15 green; 2 red, 1 blue, 1 green; 4 red, 4 green, 2 blue; 3 blue, 6 green
|
||||||
|
Game 13: 9 red, 2 blue, 2 green; 1 blue, 2 red, 15 green; 9 green, 2 blue, 9 red; 5 blue, 8 green, 5 red; 2 blue, 11 green, 5 red
|
||||||
|
Game 14: 9 blue, 1 red; 10 blue, 4 green, 3 red; 2 red, 6 blue; 4 green, 2 blue, 1 red; 5 green, 2 red, 11 blue; 12 blue, 2 red, 1 green
|
||||||
|
Game 15: 9 blue, 7 green, 12 red; 9 red, 17 green, 8 blue; 6 red, 4 blue, 4 green; 5 red, 17 green
|
||||||
|
Game 16: 5 green, 4 red; 3 blue, 3 red, 14 green; 6 red, 5 blue, 12 green
|
||||||
|
Game 17: 8 blue, 5 green, 2 red; 6 red, 6 blue; 9 red; 5 blue, 2 green, 8 red; 13 red, 4 blue, 4 green; 9 blue, 3 green, 5 red
|
||||||
|
Game 18: 8 green, 1 red, 2 blue; 4 green, 4 red, 1 blue; 6 blue, 2 red
|
||||||
|
Game 19: 3 green, 9 blue; 4 blue, 10 red; 6 red, 3 green, 3 blue; 6 red, 4 green, 9 blue
|
||||||
|
Game 20: 11 green, 3 blue; 6 green; 3 green, 6 blue; 1 red, 5 green; 6 blue, 7 green
|
||||||
|
Game 21: 1 green, 1 blue, 12 red; 6 red, 2 blue; 5 green, 4 red, 2 blue; 11 red, 8 green, 1 blue
|
||||||
|
Game 22: 10 red; 1 red, 13 green, 9 blue; 6 blue, 12 red, 12 green; 10 red, 8 blue, 11 green; 2 green, 1 red, 3 blue; 7 red, 1 blue, 8 green
|
||||||
|
Game 23: 11 red, 15 blue; 10 blue, 16 red, 1 green; 14 blue, 5 red; 1 green, 9 red, 9 blue; 1 red, 7 blue, 3 green; 6 red, 2 green, 3 blue
|
||||||
|
Game 24: 6 blue, 11 red; 16 green, 2 red, 1 blue; 8 red, 7 blue; 14 blue, 9 green, 9 red; 13 green, 4 red, 8 blue; 2 red, 7 blue, 1 green
|
||||||
|
Game 25: 2 green, 12 blue, 1 red; 10 blue, 5 red, 5 green; 2 blue, 9 red, 3 green; 5 blue, 4 red, 2 green
|
||||||
|
Game 26: 7 blue, 6 red, 1 green; 2 blue, 3 green, 12 red; 2 blue, 6 red, 5 green
|
||||||
|
Game 27: 2 green, 3 red; 4 green; 2 red, 1 blue, 1 green; 2 red, 1 green, 2 blue
|
||||||
|
Game 28: 11 blue, 1 red, 5 green; 2 blue, 2 red, 4 green; 10 blue, 4 red, 1 green
|
||||||
|
Game 29: 6 blue, 17 red, 1 green; 8 blue, 4 red; 14 blue, 1 red, 3 green
|
||||||
|
Game 30: 2 blue, 4 green; 7 green, 1 blue, 1 red; 1 blue, 8 green
|
||||||
|
Game 31: 15 blue, 9 green, 2 red; 5 green, 4 blue, 1 red; 1 green, 15 red, 7 blue; 5 red, 2 blue
|
||||||
|
Game 32: 1 blue, 5 red, 3 green; 3 green, 8 red, 1 blue; 5 green, 1 red; 4 green, 3 blue, 15 red; 2 green, 1 blue; 4 blue, 15 red, 4 green
|
||||||
|
Game 33: 3 red, 10 blue; 4 red, 9 blue; 1 green, 10 blue
|
||||||
|
Game 34: 3 blue, 1 green, 9 red; 4 green, 2 red, 9 blue; 7 blue, 3 red; 6 blue, 13 red; 4 green, 13 blue, 9 red
|
||||||
|
Game 35: 14 red, 1 green; 1 red, 2 green, 4 blue; 3 blue, 10 red, 6 green; 5 blue, 6 red, 7 green; 7 blue, 5 red
|
||||||
|
Game 36: 2 blue, 8 red, 9 green; 9 green, 3 red, 10 blue; 6 red, 8 blue, 1 green; 6 green, 8 red, 4 blue
|
||||||
|
Game 37: 10 green, 3 red, 6 blue; 2 blue, 9 red, 5 green; 13 green, 9 red, 10 blue; 2 blue, 4 green, 9 red
|
||||||
|
Game 38: 4 red, 14 blue, 12 green; 6 red, 12 green, 18 blue; 6 green, 1 blue, 1 red
|
||||||
|
Game 39: 5 red, 1 blue, 3 green; 1 blue, 3 green, 8 red; 15 red, 1 blue, 5 green; 3 green, 5 red; 1 blue, 14 red; 3 green, 1 blue, 12 red
|
||||||
|
Game 40: 8 green, 4 blue; 5 blue, 7 red, 8 green; 5 blue, 8 green; 6 green, 3 red, 12 blue; 14 blue, 7 green, 2 red; 1 green, 7 red, 5 blue
|
||||||
|
Game 41: 7 red, 10 green; 10 red, 6 green; 9 red, 7 green, 1 blue; 3 red, 1 blue
|
||||||
|
Game 42: 3 green, 2 blue, 13 red; 1 blue, 3 red; 11 green, 16 red; 3 green, 1 blue, 16 red; 5 red, 8 green
|
||||||
|
Game 43: 12 blue, 9 red; 16 blue, 2 red, 7 green; 4 red, 1 blue, 11 green; 15 blue, 4 red, 9 green
|
||||||
|
Game 44: 17 green, 5 blue, 2 red; 9 green, 11 blue, 1 red; 20 green, 3 blue, 8 red; 2 red, 13 green, 9 blue; 15 green, 12 blue; 4 blue, 7 green, 9 red
|
||||||
|
Game 45: 5 green; 5 green, 1 red; 3 green, 2 blue; 1 green, 1 blue, 1 red
|
||||||
|
Game 46: 10 red, 11 green; 16 green, 8 blue, 12 red; 9 green, 9 blue
|
||||||
|
Game 47: 20 green, 17 red, 1 blue; 16 red, 2 blue, 11 green; 3 blue, 19 red, 1 green; 3 blue, 17 red, 17 green; 12 green, 2 blue, 7 red
|
||||||
|
Game 48: 1 red, 4 blue, 6 green; 19 green, 1 red, 1 blue; 16 green, 3 blue, 1 red; 3 blue, 17 green; 4 blue, 12 green
|
||||||
|
Game 49: 13 green, 2 blue, 1 red; 1 green, 8 red, 2 blue; 11 red, 11 green, 3 blue; 7 red, 8 green, 4 blue
|
||||||
|
Game 50: 11 blue, 1 red, 2 green; 1 green, 10 blue; 1 blue; 6 blue; 1 green, 2 blue
|
||||||
|
Game 51: 3 red, 3 green, 1 blue; 3 green, 3 red; 10 green, 4 red; 3 red, 2 green
|
||||||
|
Game 52: 1 red, 4 blue; 1 green, 11 blue; 1 green, 3 red, 6 blue; 4 red, 1 green, 4 blue; 9 blue, 1 green; 10 blue, 1 green
|
||||||
|
Game 53: 2 blue, 4 green, 1 red; 8 blue, 4 red, 7 green; 9 red, 7 blue, 6 green; 3 red, 7 green, 1 blue; 2 red, 9 blue, 5 green; 1 green, 7 red, 10 blue
|
||||||
|
Game 54: 1 red, 1 blue, 5 green; 2 red, 1 green, 2 blue; 3 green, 3 blue, 2 red; 4 red; 12 red, 5 green, 2 blue
|
||||||
|
Game 55: 2 red, 11 blue; 16 green, 7 red, 16 blue; 4 blue, 11 green, 7 red; 8 green, 18 blue, 8 red
|
||||||
|
Game 56: 2 blue, 2 green, 1 red; 1 red, 1 green; 1 red; 4 green; 1 blue; 1 blue, 7 green
|
||||||
|
Game 57: 4 blue, 3 green; 16 green, 2 red, 5 blue; 1 red, 13 green, 2 blue; 3 blue, 12 green, 2 red; 2 red, 5 blue, 4 green; 10 green, 2 blue
|
||||||
|
Game 58: 3 blue, 8 green; 4 green, 3 blue; 7 green, 5 blue, 5 red; 8 green; 3 red, 6 blue, 9 green; 2 red, 10 green, 4 blue
|
||||||
|
Game 59: 7 blue, 6 green, 5 red; 7 red, 2 blue; 5 red, 11 green, 14 blue; 8 green, 17 red
|
||||||
|
Game 60: 3 green, 8 blue, 2 red; 4 green, 7 blue, 6 red; 13 blue, 8 green, 2 red; 10 red, 6 blue, 5 green; 11 green, 3 blue, 4 red; 9 red, 5 green, 9 blue
|
||||||
|
Game 61: 4 red, 18 blue, 13 green; 9 green, 5 red, 3 blue; 4 green, 3 blue, 4 red; 8 red, 4 green, 7 blue; 8 red, 4 blue, 6 green; 10 green, 5 red, 14 blue
|
||||||
|
Game 62: 12 red, 14 blue, 9 green; 9 blue, 6 red, 4 green; 2 red, 5 blue; 1 red, 12 blue
|
||||||
|
Game 63: 11 blue, 13 red, 11 green; 4 blue, 9 green; 8 blue, 9 red; 7 red, 11 green, 7 blue
|
||||||
|
Game 64: 10 blue, 8 red, 12 green; 10 red, 12 blue, 9 green; 3 green, 17 red; 12 green, 15 blue, 16 red; 6 green, 15 blue, 1 red; 9 red, 6 blue, 10 green
|
||||||
|
Game 65: 7 red, 7 blue; 3 blue, 1 red, 1 green; 3 red, 8 blue
|
||||||
|
Game 66: 1 blue, 3 red; 10 green, 5 blue; 4 green; 3 red, 11 green; 3 blue, 15 green, 3 red
|
||||||
|
Game 67: 1 red; 2 blue, 2 green, 1 red; 6 green, 1 blue
|
||||||
|
Game 68: 7 red, 4 blue; 4 blue, 6 red, 7 green; 2 green, 19 red, 11 blue; 11 green, 9 red
|
||||||
|
Game 69: 4 blue, 3 green, 1 red; 7 blue, 1 red, 3 green; 5 blue, 1 green; 2 blue, 10 green, 2 red; 2 red, 6 green, 5 blue; 1 red, 4 green, 2 blue
|
||||||
|
Game 70: 9 blue, 7 red, 6 green; 19 blue, 4 red, 5 green; 6 blue, 7 red, 4 green; 3 blue, 4 red, 2 green
|
||||||
|
Game 71: 6 green, 12 blue, 4 red; 11 red, 10 green, 11 blue; 3 red, 14 blue, 13 green; 4 blue, 3 green
|
||||||
|
Game 72: 2 green, 1 blue, 9 red; 10 red, 3 green, 1 blue; 11 red, 2 green; 2 green, 1 blue, 5 red; 1 red, 1 blue, 3 green; 13 red, 4 blue, 1 green
|
||||||
|
Game 73: 11 green, 6 blue; 7 green, 6 blue, 7 red; 12 green, 8 blue, 11 red; 4 red, 2 blue, 9 green; 4 green, 7 blue, 2 red
|
||||||
|
Game 74: 3 blue, 7 red; 3 blue, 5 green, 2 red; 5 red, 1 green, 3 blue; 8 green, 2 blue, 11 red; 3 blue, 8 green, 10 red
|
||||||
|
Game 75: 2 green; 5 blue; 1 blue, 1 red; 1 red, 9 blue, 2 green; 2 blue, 2 green
|
||||||
|
Game 76: 12 blue, 13 green; 5 red, 11 blue, 9 green; 12 green, 6 red
|
||||||
|
Game 77: 1 blue, 15 green, 12 red; 15 green, 5 blue; 14 green, 3 blue, 8 red
|
||||||
|
Game 78: 11 green, 8 blue, 1 red; 9 green, 8 blue, 1 red; 13 green, 5 red, 6 blue; 5 red, 7 green, 20 blue; 10 blue, 5 red
|
||||||
|
Game 79: 3 blue; 6 blue, 5 red; 4 red, 1 green, 4 blue; 7 blue, 6 red; 7 red, 1 blue; 1 red, 1 blue, 1 green
|
||||||
|
Game 80: 11 green, 3 red, 8 blue; 2 red, 15 green, 2 blue; 5 green, 8 blue, 2 red; 8 blue, 14 green; 2 blue, 13 green
|
||||||
|
Game 81: 9 red, 4 green; 7 green, 4 red; 2 red, 4 blue, 6 green; 6 red, 4 blue, 9 green; 1 green, 3 red; 6 green, 1 blue, 8 red
|
||||||
|
Game 82: 5 blue, 3 red, 3 green; 5 red; 2 red, 3 green, 8 blue
|
||||||
|
Game 83: 10 green, 1 red, 1 blue; 3 red, 1 green, 1 blue; 4 red, 10 green
|
||||||
|
Game 84: 16 red, 2 green, 6 blue; 6 red, 3 green, 8 blue; 3 green, 10 red, 5 blue; 4 blue, 3 green; 15 red
|
||||||
|
Game 85: 3 green, 2 red; 5 green, 4 blue; 5 green, 8 red, 3 blue
|
||||||
|
Game 86: 7 green, 16 blue, 7 red; 1 green, 12 red, 2 blue; 15 green, 16 blue, 7 red
|
||||||
|
Game 87: 1 red, 6 green, 5 blue; 2 green, 1 blue; 2 green, 1 red, 1 blue; 5 green, 4 blue
|
||||||
|
Game 88: 3 green, 3 red, 4 blue; 1 red, 1 green; 6 blue, 9 red, 1 green; 1 green, 11 red, 3 blue; 7 red, 6 blue
|
||||||
|
Game 89: 2 blue, 3 red, 4 green; 5 red, 7 blue, 14 green; 8 blue, 5 red, 16 green; 2 blue, 5 red, 7 green; 5 green, 9 blue, 1 red
|
||||||
|
Game 90: 1 blue, 3 red, 7 green; 11 green, 4 red, 1 blue; 1 red, 1 blue, 6 green; 2 blue, 2 green; 8 green, 2 blue; 3 red, 2 blue, 4 green
|
||||||
|
Game 91: 6 blue, 4 red, 1 green; 8 red, 3 blue, 3 green; 1 green, 2 blue, 5 red; 1 blue, 3 green
|
||||||
|
Game 92: 8 green, 1 red, 5 blue; 2 green, 7 blue; 11 blue, 5 green, 8 red; 7 blue, 3 red, 4 green
|
||||||
|
Game 93: 3 green, 1 red, 9 blue; 13 red, 5 blue, 8 green; 5 green, 2 red, 7 blue
|
||||||
|
Game 94: 4 green, 10 blue, 8 red; 4 red, 10 blue, 2 green; 2 green, 10 blue, 5 red; 5 green, 2 red, 10 blue
|
||||||
|
Game 95: 5 green, 1 blue; 3 blue, 11 green, 8 red; 8 blue, 2 red, 12 green; 4 green, 4 blue, 4 red
|
||||||
|
Game 96: 1 blue, 13 green; 8 blue, 3 red, 4 green; 1 red, 3 blue, 10 green
|
||||||
|
Game 97: 18 green, 4 red; 1 blue, 2 red, 9 green; 6 red, 3 blue, 10 green; 3 blue, 15 green, 4 red
|
||||||
|
Game 98: 2 blue, 3 green, 6 red; 1 green, 1 blue, 8 red; 8 red, 3 green, 1 blue; 2 blue; 8 red, 2 green, 2 blue
|
||||||
|
Game 99: 1 green, 2 red, 1 blue; 8 green, 4 blue, 1 red; 7 blue, 1 red, 11 green; 9 green, 3 blue; 1 red, 2 blue; 1 red, 6 blue
|
||||||
|
Game 100: 7 blue, 9 green, 2 red; 5 red, 9 green; 1 blue, 8 red, 13 green
|
5
inputs/pb2_sample.txt
Normal file
5
inputs/pb2_sample.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||||
|
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
|
||||||
|
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
||||||
|
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
||||||
|
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
|
11
main.cpp
11
main.cpp
@ -1,5 +1,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "pb_1/problem_1.hpp"
|
#include "pb_1/problem_1.hpp"
|
||||||
|
#include "pb_2/problem_2.hpp"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
{
|
{
|
||||||
@ -10,8 +11,14 @@ int main() {
|
|||||||
}
|
}
|
||||||
{
|
{
|
||||||
std::cout << "Begin Problem 1 part 2\n";
|
std::cout << "Begin Problem 1 part 2\n";
|
||||||
if (const auto result = pb1::solve_problem_part2("inputs/pb1_p2_sample.txt"); result != 0) {
|
// if (const auto result = pb1::solve_problem_part2("inputs/pb1_p2_sample.txt"); result != 0) {
|
||||||
std::cout << "Problem 1 part2: " << result << '\n';
|
// std::cout << "Problem 1 part2: " << result << '\n';
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::cout << "Begin Problem 2 part 1\n";
|
||||||
|
if (const auto result = pb2::problem_part1("inputs/pb2.txt"); result != 0) {
|
||||||
|
std::cout << "Problem 2 part 1: " << result << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
@ -4,8 +4,8 @@ add_library(pb_1 STATIC problem_1.cpp problem_1.hpp)
|
|||||||
|
|
||||||
target_compile_definitions(pb_1 PUBLIC $<$<AND:$<CONFIG:Debug>,$<STREQUAL:$<CXX_COMPILER_ID>,GNU>>:_GLIBCXX_DEBUG>)
|
target_compile_definitions(pb_1 PUBLIC $<$<AND:$<CONFIG:Debug>,$<STREQUAL:$<CXX_COMPILER_ID>,GNU>>:_GLIBCXX_DEBUG>)
|
||||||
|
|
||||||
target_compile_options(pb_1 PUBLIC ${COMPILE_FLAGS})
|
#target_compile_options(pb_1 PUBLIC ${COMPILE_FLAGS})
|
||||||
|
|
||||||
target_link_options(pb_1 PUBLIC ${LINKER_OPTIONS})
|
#target_link_options(pb_1 PUBLIC ${LINKER_OPTIONS})
|
||||||
|
|
||||||
target_link_libraries(pb_1 PUBLIC ${LINKER_FLAGS})
|
target_link_libraries(pb_1 PUBLIC ${LINKER_FLAGS})
|
||||||
|
13
pb_2/CMakeLists.txt
Normal file
13
pb_2/CMakeLists.txt
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
project(pb_2 CXX)
|
||||||
|
|
||||||
|
find_package(Boost REQUIRED)
|
||||||
|
|
||||||
|
add_library(pb_2 STATIC problem_2.cpp problem_2.hpp)
|
||||||
|
|
||||||
|
target_include_directories(pb_2 PRIVATE ${Boost_INCLUDE_DIR})
|
||||||
|
|
||||||
|
target_compile_definitions(pb_2 PUBLIC $<$<AND:$<CONFIG:Debug>,$<STREQUAL:$<CXX_COMPILER_ID>,GNU>>:_GLIBCXX_DEBUG>)
|
||||||
|
|
||||||
|
#target_compile_options(pb_2 PUBLIC ${COMPILE_FLAGS})
|
||||||
|
|
||||||
|
#target_link_options(pb_2 PUBLIC ${LINKER_OPTIONS})
|
151
pb_2/problem_2.cpp
Normal file
151
pb_2/problem_2.cpp
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
//
|
||||||
|
// Created by postaron on 17/01/24.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "problem_2.hpp"
|
||||||
|
#include <optional>
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <format>
|
||||||
|
#include <list>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <numeric>
|
||||||
|
#include <charconv>
|
||||||
|
#include <boost/algorithm/string.hpp>
|
||||||
|
|
||||||
|
namespace pb2 {
|
||||||
|
struct Cube final {
|
||||||
|
int red{ 0 };
|
||||||
|
int green{ 0 };
|
||||||
|
int blue{ 0 };
|
||||||
|
|
||||||
|
constexpr Cube() noexcept = default;
|
||||||
|
|
||||||
|
constexpr Cube(int red, int green, int blue) noexcept: red(red), green(green), blue(blue) {}
|
||||||
|
|
||||||
|
friend std::ostream &operator<<(std::ostream &os, const Cube &cube) {
|
||||||
|
os << "red: " << cube.red << " green: " << cube.green << " blue: " << cube.blue;
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cube &operator+=(const Cube &rhs) noexcept {
|
||||||
|
red += rhs.red;
|
||||||
|
green += rhs.green;
|
||||||
|
blue += rhs.blue;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
friend Cube operator+(Cube lhs, const Cube &rhs) noexcept {
|
||||||
|
lhs += rhs;
|
||||||
|
return lhs;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::optional<std::ifstream> read_file(const fs::path &problemFile) noexcept {
|
||||||
|
std::ifstream file(problemFile);
|
||||||
|
if (!file.is_open()) {
|
||||||
|
const auto error_state = file.rdstate();
|
||||||
|
switch (error_state) {
|
||||||
|
case std::ios::badbit:
|
||||||
|
std::cerr << "Fatal I/O error occurred.\n";
|
||||||
|
break;
|
||||||
|
case std::ios::eofbit:
|
||||||
|
std::cerr << "End of file reached.\n";
|
||||||
|
break;
|
||||||
|
case std::ios::failbit:
|
||||||
|
std::cerr << "Non-fatal I/O error occurred.\n";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
std::cerr << "impossible to reach.\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
const auto path_string = problemFile.string();
|
||||||
|
const auto msg = std::format("Failed to open file {}: ", path_string);
|
||||||
|
std::perror(msg.c_str());
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cube parse_line(const std::string &line) {
|
||||||
|
std::list<std::string_view> cubes_str;
|
||||||
|
std::list<Cube> cubes;
|
||||||
|
std::vector<std::string_view> cube_str;
|
||||||
|
cube_str.reserve(2);
|
||||||
|
std::string_view line_view = line;
|
||||||
|
line_view = line_view.substr(line.find_first_of(':') + 1);
|
||||||
|
boost::split(cubes_str, line_view.cbegin(), boost::is_any_of(","));
|
||||||
|
return std::transform_reduce(cubes_str.begin(),
|
||||||
|
cubes_str.end(),
|
||||||
|
Cube{},
|
||||||
|
std::plus<>{},
|
||||||
|
[&cube_str](std::string_view &str) -> Cube {
|
||||||
|
str = str.substr(1);
|
||||||
|
const std::string tmp(str.cbegin(), str.cend());
|
||||||
|
boost::split(cube_str, tmp, boost::is_any_of(" "));
|
||||||
|
Cube cube;
|
||||||
|
const auto &chars = cube_str.front();
|
||||||
|
switch (cube_str.back().size()) {
|
||||||
|
case 3:
|
||||||
|
// means it's red
|
||||||
|
if (const auto result = std::from_chars(chars.data(),
|
||||||
|
chars.data() + chars.size(),
|
||||||
|
cube.red);
|
||||||
|
result.ec != std::errc{}) {
|
||||||
|
std::cerr << "Error: "
|
||||||
|
<< std::quoted(std::make_error_code(result.ec).message())
|
||||||
|
<< '\n';
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
// means it's blue
|
||||||
|
if (const auto result = std::from_chars(chars.data(),
|
||||||
|
chars.data() + chars.size(),
|
||||||
|
cube.blue);
|
||||||
|
result.ec != std::errc{}) {
|
||||||
|
std::cerr << "Error: "
|
||||||
|
<< std::quoted(std::make_error_code(result.ec).message())
|
||||||
|
<< '\n';
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// means it's green
|
||||||
|
if (const auto result = std::from_chars(chars.data(),
|
||||||
|
chars.data() + chars.size(),
|
||||||
|
cube.green);
|
||||||
|
result.ec != std::errc{}) {
|
||||||
|
std::cerr << "Error: "
|
||||||
|
<< std::quoted(std::make_error_code(result.ec).message())
|
||||||
|
<< '\n';
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
cube_str.clear();
|
||||||
|
return cube;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr int maxRed = 12;
|
||||||
|
|
||||||
|
constexpr int maxGreen = 13;
|
||||||
|
|
||||||
|
constexpr int maxBlue = 14;
|
||||||
|
|
||||||
|
std::size_t problem_part1(const fs::path &problemFile) noexcept {
|
||||||
|
auto file_optional = read_file(problemFile);
|
||||||
|
if (!file_optional) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
std::ifstream &file = *file_optional;
|
||||||
|
int i = 1;
|
||||||
|
int output = 0;
|
||||||
|
for (std::string line; std::getline(file, line); ++i) {
|
||||||
|
std::replace(line.begin(), line.end(), ';', ',');
|
||||||
|
auto tmp = parse_line(line);
|
||||||
|
if (tmp.red <= maxRed && tmp.green <= maxGreen && tmp.blue <= maxBlue) {
|
||||||
|
output += i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
}
|
14
pb_2/problem_2.hpp
Normal file
14
pb_2/problem_2.hpp
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#ifndef ADVENTOFCODE2023_PROBLEM_2_HPP
|
||||||
|
#define ADVENTOFCODE2023_PROBLEM_2_HPP
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
namespace pb2 {
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
std::size_t problem_part1(const fs::path &problemFile) noexcept;
|
||||||
|
|
||||||
|
std::size_t problem_part2(const fs::path &problemFile) noexcept;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //ADVENTOFCODE2023_PROBLEM_2_HPP
|
Loading…
Reference in New Issue
Block a user