From bec8be80a3d2db40c28c705cf55e8a3565a3adcc Mon Sep 17 00:00:00 2001 From: Thraix Date: Fri, 4 Sep 2026 18:37:13 +0200 Subject: [PATCH] Add solution for 2016 Day 22 --- res/2016/day22/test_input.txt | 11 ++++ src/2016/Day22.cpp | 121 +++++++++++++++++++++++++++++++--- src/common/lib/Array2D.h | 5 ++ 3 files changed, 129 insertions(+), 8 deletions(-) diff --git a/res/2016/day22/test_input.txt b/res/2016/day22/test_input.txt index e69de29..7793929 100644 --- a/res/2016/day22/test_input.txt +++ b/res/2016/day22/test_input.txt @@ -0,0 +1,11 @@ +root@ebhq-gridcenter# df -h +Filesystem Size Used Avail Use% +/dev/grid/node-x0-y0 10T 8T 2T 80% +/dev/grid/node-x0-y1 11T 6T 5T 54% +/dev/grid/node-x0-y2 32T 28T 4T 87% +/dev/grid/node-x1-y0 9T 7T 2T 77% +/dev/grid/node-x1-y1 8T 0T 8T 0% +/dev/grid/node-x1-y2 11T 7T 4T 63% +/dev/grid/node-x2-y0 10T 6T 4T 60% +/dev/grid/node-x2-y1 9T 8T 1T 88% +/dev/grid/node-x2-y2 9T 6T 3T 66% diff --git a/src/2016/Day22.cpp b/src/2016/Day22.cpp index 472b6b4..d92d364 100644 --- a/src/2016/Day22.cpp +++ b/src/2016/Day22.cpp @@ -2,30 +2,135 @@ namespace y2016::day22 { - REGISTER_DAY(2016, Day22, std::vector, int); + struct Fs + { + int used; + int avail; - REGISTER_TEST_EXAMPLE(2016, Day22, ExampleInput, 1, 0); - REGISTER_TEST(2016, Day22, Input, 1, 0); - REGISTER_TEST_EXAMPLE(2016, Day22, ExampleInput, 2, 0); - REGISTER_TEST(2016, Day22, Input, 2, 0); + bool operator==(const Fs& other) const + { + return used == other.used && avail == other.avail; + } + }; + + REGISTER_DAY(2016, Day22, Array2D, int); + + REGISTER_TEST_EXAMPLE(2016, Day22, ExampleInput, 1, 7); + REGISTER_TEST(2016, Day22, Input, 1, 1024); + REGISTER_TEST_EXAMPLE(2016, Day22, ExampleInput, 2, 7); + REGISTER_TEST(2016, Day22, Input, 2, 230); READ_INPUT(input) { - std::vector vec; + std::vector, Fs>> fss; + Array2D vec{}; std::string str; + getline(input, str); + getline(input, str); + int maxX = 0; + int maxY = 0; while (getline(input, str)) { + // /dev/grid/node-x0-y0 91T 71T 20T 78% + Fs fs; + std::stringstream ss{str}; + int dummy; + int x; + int y; + ss >> "/dev/grid/node-x" >> x >> "-y" >> y >> dummy >> "T" >> fs.used >> "T" >> fs.avail >> "T"; + fss.emplace_back(std::pair{std::pair{x, y}, fs}); + maxX = std::max(maxX, x); + maxY = std::max(maxY, y); + } + vec = Array2D{maxX + 1, maxY + 1}; + for (const auto& [pos, fs] : fss) + { + vec.Set(pos.first, pos.second, fs); } return vec; } + struct State + { + Index2D goal; + Index2D empty; + + bool operator<(const State& other) const + { + if (goal != other.goal) + return goal < other.goal; + return empty < other.empty; + } + + friend std::ostream& operator<<(std::ostream& os, const State& state) + { + return os << state.goal << " " << state.empty; + } + }; + + std::vector> Branch(const Array2D& initial, const State& state) + { + std::vector> states{}; + Fs emptyFs = initial.Get(state.empty); + std::vector neighbors = initial.GetNeighbors(state.empty, false); + for (const auto& neighbor : neighbors) + { + if (initial.Get(neighbor).used < 400) + { + State newState = state; + if (neighbor == state.goal) + newState.goal = state.empty; + newState.empty = neighbor; + states.emplace_back(1, newState); + } + } + return states; + } + + bool Goal(const Array2D& initial, const State& state) + { + return state.goal == Index2D{0, 0}; + } + + int Heuristic(const Array2D& initial, const State& state) + { + return state.goal.x + state.goal.y; + } + OUTPUT1(input) { - return 0; + int count = 0; + for (const auto& val : input) + { + if (val.used == 0) + continue; + for (const auto& val2 : input) + { + if (val == val2) + continue; + if (val.used <= val2.avail) + { + count++; + } + } + } + return count; } OUTPUT2(input) { - return 0; + State state; + state.goal = Index2D{input.width - 1, 0}; + for (int i = 0; i < input.height; i++) + { + for (int j = 0; j < input.width; j++) + { + if (input.Get(j, i).used == 0) + { + state.empty = Index2D{j, i}; + } + } + } + return Helper::AStar(input, state, Heuristic, Branch, Goal); } } diff --git a/src/common/lib/Array2D.h b/src/common/lib/Array2D.h index 366cf63..19e0fdc 100644 --- a/src/common/lib/Array2D.h +++ b/src/common/lib/Array2D.h @@ -500,6 +500,11 @@ struct Array2D return ConstIterator{*this, Index2D{0, height}}; } + bool operator<(const Array2D& other) const + { + return array2D < other.array2D; + } + friend std::ostream& operator<<(std::ostream& os, const Array2D& array2D) { for (int y = 0; y < array2D.height; y++)