Add solution for 2016 Day 3
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
5 10 25
|
||||||
|
11 15 25
|
||||||
|
8 25 20
|
||||||
|
|||||||
+32
-11
@@ -2,30 +2,51 @@
|
|||||||
|
|
||||||
namespace y2016::day03
|
namespace y2016::day03
|
||||||
{
|
{
|
||||||
REGISTER_DAY(2016, Day03, std::vector<int>, int);
|
REGISTER_DAY(2016, Day03, Array2D<int>, int);
|
||||||
|
|
||||||
REGISTER_TEST_EXAMPLE(2016, Day03, ExampleInput, 1, 0);
|
REGISTER_TEST_EXAMPLE(2016, Day03, ExampleInput, 1, 2);
|
||||||
REGISTER_TEST(2016, Day03, Input, 1, 0);
|
REGISTER_TEST(2016, Day03, Input, 1, 983);
|
||||||
REGISTER_TEST_EXAMPLE(2016, Day03, ExampleInput, 2, 0);
|
REGISTER_TEST_EXAMPLE(2016, Day03, ExampleInput, 2, 2);
|
||||||
REGISTER_TEST(2016, Day03, Input, 2, 0);
|
REGISTER_TEST(2016, Day03, Input, 2, 1836);
|
||||||
|
|
||||||
READ_INPUT(input)
|
READ_INPUT(input)
|
||||||
{
|
{
|
||||||
std::vector<int> vec;
|
return Input::ReadIntsAsArray2D(input);
|
||||||
std::string str;
|
}
|
||||||
while (getline(input, str))
|
|
||||||
|
bool IsValid(const std::array<int, 3>& 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)
|
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)
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "Array2D.h"
|
#include "Array2D.h"
|
||||||
@@ -56,6 +57,28 @@ struct Input
|
|||||||
return Array2D<int>(width, height, data);
|
return Array2D<int>(width, height, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Array2D<int> ReadIntsAsArray2D(std::istream& input)
|
||||||
|
{
|
||||||
|
std::vector<int> 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<int>(width, height, data);
|
||||||
|
}
|
||||||
|
|
||||||
static std::vector<int> ReadInts(std::istream& input)
|
static std::vector<int> ReadInts(std::istream& input)
|
||||||
{
|
{
|
||||||
return ReadInts(input, '\n');
|
return ReadInts(input, '\n');
|
||||||
|
|||||||
Reference in New Issue
Block a user