Add solution for 2016 Day 18

This commit is contained in:
Thraix
2026-08-31 20:51:09 +02:00
parent ed11090cc2
commit 8162a46091
2 changed files with 37 additions and 10 deletions
+1
View File
@@ -0,0 +1 @@
.^^.^.^^^^
+36 -10
View File
@@ -2,30 +2,56 @@
namespace y2016::day18
{
REGISTER_DAY(2016, Day18, std::vector<int>, int);
REGISTER_DAY(2016, Day18, std::string, int);
REGISTER_TEST_EXAMPLE(2016, Day18, ExampleInput, 1, 0);
REGISTER_TEST(2016, Day18, Input, 1, 0);
REGISTER_TEST_EXAMPLE(2016, Day18, ExampleInput, 2, 0);
REGISTER_TEST(2016, Day18, Input, 2, 0);
REGISTER_TEST_EXAMPLE(2016, Day18, ExampleInput, 1, 38);
REGISTER_TEST(2016, Day18, Input, 1, 1913);
REGISTER_TEST_EXAMPLE(2016, Day18, ExampleInput, 2, 1935478);
REGISTER_TEST(2016, Day18, Input, 2, 19993564);
READ_INPUT(input)
{
std::vector<int> vec;
std::string str;
while (getline(input, str))
getline(input, str);
return str;
}
int Solve(const std::string& str, int rows)
{
int safes = std::count(str.begin(), str.end(), '.');
std::vector<char> row(str.size() + 2, false);
for (int i = 0; i < str.size(); i++)
row[i + 1] = str[i] == '^';
for (int i = 1; i < rows; i++)
{
std::vector<char> newRow(row.size(), false);
for (int j = 1; j < newRow.size() - 1; j++)
{
bool isTrap = false;
if (row[j - 1] && row[j] && !row[j + 1])
newRow[j] = true;
else if (!row[j - 1] && row[j] && row[j + 1])
newRow[j] = true;
else if (row[j - 1] && !row[j] && !row[j + 1])
newRow[j] = true;
else if (!row[j - 1] && !row[j] && row[j + 1])
newRow[j] = true;
else
safes++;
}
row = newRow;
}
return vec;
return safes;
}
OUTPUT1(input)
{
return 0;
return Solve(input, isExample ? 10 : 40);
}
OUTPUT2(input)
{
return 0;
return Solve(input, 400000);
}
}