From fc76349588afa69995e0f9a4ee742979c9d9edd8 Mon Sep 17 00:00:00 2001 From: Thraix Date: Wed, 12 Aug 2026 21:00:56 +0200 Subject: [PATCH] Add solution for 2016 Day 3 --- res/2016/day03/test_input.txt | 3 +++ src/2016/Day03.cpp | 43 ++++++++++++++++++++++++++--------- src/common/lib/Input.h | 23 +++++++++++++++++++ 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/res/2016/day03/test_input.txt b/res/2016/day03/test_input.txt index e69de29..dda7484 100644 --- a/res/2016/day03/test_input.txt +++ b/res/2016/day03/test_input.txt @@ -0,0 +1,3 @@ +5 10 25 +11 15 25 +8 25 20 diff --git a/src/2016/Day03.cpp b/src/2016/Day03.cpp index df67cd4..e269af2 100644 --- a/src/2016/Day03.cpp +++ b/src/2016/Day03.cpp @@ -2,30 +2,51 @@ namespace y2016::day03 { - REGISTER_DAY(2016, Day03, std::vector, int); + REGISTER_DAY(2016, Day03, Array2D, int); - REGISTER_TEST_EXAMPLE(2016, Day03, ExampleInput, 1, 0); - REGISTER_TEST(2016, Day03, Input, 1, 0); - REGISTER_TEST_EXAMPLE(2016, Day03, ExampleInput, 2, 0); - REGISTER_TEST(2016, Day03, Input, 2, 0); + REGISTER_TEST_EXAMPLE(2016, Day03, ExampleInput, 1, 2); + REGISTER_TEST(2016, Day03, Input, 1, 983); + REGISTER_TEST_EXAMPLE(2016, Day03, ExampleInput, 2, 2); + REGISTER_TEST(2016, Day03, Input, 2, 1836); READ_INPUT(input) { - std::vector vec; - std::string str; - while (getline(input, str)) + return Input::ReadIntsAsArray2D(input); + } + + bool IsValid(const std::array& triangle) + { + for (int i = 0; i < 3; i++) { + int length = triangle.at(i) + triangle.at((i + 1) % 3); + if (length <= triangle.at((i + 2) % 3)) + return false; } - return vec; + return true; } OUTPUT1(input) { - return 0; + int count = 0; + for (int y = 0; y < input.height; y++) + { + if (IsValid({input.Get(0, y), input.Get(1, y), input.Get(2, y)})) + count++; + } + return count; } OUTPUT2(input) { - return 0; + int count = 0; + for (int y = 0; y < input.height; y += 3) + { + for (int x = 0; x < 3; x++) + { + if (IsValid({input.Get(x, y), input.Get(x, y + 1), input.Get(x, y + 2)})) + count++; + } + } + return count; } } diff --git a/src/common/lib/Input.h b/src/common/lib/Input.h index 336d853..bfe89b3 100644 --- a/src/common/lib/Input.h +++ b/src/common/lib/Input.h @@ -3,6 +3,7 @@ #include #include +#include #include #include "Array2D.h" @@ -56,6 +57,28 @@ struct Input return Array2D(width, height, data); } + static Array2D ReadIntsAsArray2D(std::istream& input) + { + std::vector data; + int width = 0; + int height = 0; + std::string str; + while (getline(input, str)) + { + height++; + std::stringstream ss{str}; + int i; + int size = 0; + while (ss >> i) + { + data.emplace_back(i); + size++; + } + width = size; + } + return Array2D(width, height, data); + } + static std::vector ReadInts(std::istream& input) { return ReadInts(input, '\n');