Skip to content

Commit

Permalink
Merge pull request #403 from david-ds/skasch/day17
Browse files Browse the repository at this point in the history
Day 17, C++
  • Loading branch information
skasch authored Oct 13, 2024
2 parents 8f9a4f3 + ec3c7b4 commit e7356e3
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 4 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ jobs:
- uses: jiro4989/setup-nim-action@v1
with:
nim-version: 1.4.8
- uses: goto-bus-stop/setup-zig@v1
with:
version: 0.7.0
- uses: goto-bus-stop/setup-zig@v2
# TODO: switch back to nocturlab/setup-vlang-action@v1 once https://github.com/nocturlab/setup-vlang-action/issues/72 is fixed
- name: Install vlang
run: |
Expand Down Expand Up @@ -56,7 +54,9 @@ jobs:
key: ${{ runner.os }}-${{ hashFiles('requirements.txt') }}
- name: deps-python
if: steps.cache-python.outputs.cache-hit != 'true'
run: sudo apt-get install python3-setuptools && python3 -m pip install --user wheel && python3 -m pip install --user -r requirements.txt
run:
sudo apt-get install python3-setuptools && python3 -m pip install --user wheel && python3 -m pip install
--user -r requirements.txt

# Install ocaml packages if not cached
- name: install-esy
Expand Down
8 changes: 8 additions & 0 deletions day-17/input/skasch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
..##.##.
#.#..###
##.#.#.#
#.#.##.#
###..#..
.#.#..##
#.##.###
#.#..##.
101 changes: 101 additions & 0 deletions day-17/part-1/skasch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <array>
#include <ctime>
#include <iostream>
#include <string>
#include <unordered_set>
#include <utility>

static constexpr int kSize = 8;
static constexpr int kCycles = 6;
static constexpr int kEdge = 2 * kCycles + kSize;
static constexpr int kHeight = 2 * kCycles + 1;
static constexpr int kCubeSize = kEdge * kEdge * kHeight;

struct P {
int x;
int y;
int z;

P operator+(const P &o) { return {x + o.x, y + o.y, z + o.z}; }
};

constexpr int to_index(const P &p) {
return p.x + kEdge * p.y + kEdge * kEdge * p.z;
}

bool contains(const std::unordered_set<int> &s, int v) {
return s.find(v) != s.end();
}

static std::array<P, 26> kNeighbors = {
P{-1, -1, -1}, P{-1, -1, 0}, P{-1, -1, 1}, P{-1, 0, -1}, P{-1, 0, 0},
P{-1, 0, 1}, P{-1, 1, -1}, P{-1, 1, 0}, P{-1, 1, 1}, P{0, -1, -1},
P{0, -1, 0}, P{0, -1, 1}, P{0, 0, -1}, P{0, 0, 1}, P{0, 1, -1},
P{0, 1, 0}, P{0, 1, 1}, P{1, -1, -1}, P{1, -1, 0}, P{1, -1, 1},
P{1, 0, -1}, P{1, 0, 0}, P{1, 0, 1}, P{1, 1, -1}, P{1, 1, 0},
P{1, 1, 1},
};

void Iterate(std::unordered_set<int> &active, int cycle) {
std::unordered_set<int> new_active;
for (int x = -cycle; x < kSize + cycle; ++x) {
for (int y = -cycle; y < kSize + cycle; ++y) {
for (int z = -cycle; z < 1 + cycle; ++z) {
P pos = {x, y, z};
int cnt = 0;
for (const P &neighbor : kNeighbors) {
if (contains(active, to_index(pos + neighbor))) {
++cnt;
}
}
int index = to_index(pos);
if (contains(active, index)) {
if (cnt == 2 || cnt == 3) {
new_active.insert(index);
}
} else {
if (cnt == 3) {
new_active.insert(index);
}
}
}
}
}
std::swap(active, new_active);
}

std::string run(const std::string &input) {
int x = 0;
int y = 0;
std::unordered_set<int> active = {};
for (auto c : input) {
if (c == '\n') {
++x;
y = 0;
continue;
}
if (c == '#') {
active.insert(to_index({x, y, 0}));
}
++y;
}
for (int cycle = 1; cycle <= kCycles; ++cycle) {
Iterate(active, cycle);
}
return std::to_string(active.size());
}

int main(int argc, char **argv) {
if (argc < 2) {
std::cout << "Missing one argument" << std::endl;
exit(1);
}

clock_t start = clock();
auto answer = run(argv[1]);

std::cout << "_duration:" << float(clock() - start) * 1000.0 / CLOCKS_PER_SEC
<< "\n";
std::cout << answer << "\n";
return 0;
}
118 changes: 118 additions & 0 deletions day-17/part-2/skasch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include <array>
#include <ctime>
#include <iostream>
#include <string>
#include <unordered_set>
#include <utility>

static constexpr int kSize = 8;
static constexpr int kCycles = 6;
static constexpr int kEdge = 2 * kCycles + kSize;
static constexpr int kHeight = 2 * kCycles + 1;
static constexpr int kCubeSize = kEdge * kEdge * kHeight;

struct P {
int x;
int y;
int z;
int w;

P operator+(const P &o) { return {x + o.x, y + o.y, z + o.z, w + o.w}; }
};

constexpr int to_index(const P &p) {
return p.x + kEdge * p.y + kEdge * kEdge * p.z + kEdge * kEdge * kEdge * p.w;
}

bool contains(const std::unordered_set<int> &s, int v) {
return s.find(v) != s.end();
}

static std::array<P, 80> kNeighbors = {
P{-1, -1, -1, -1}, P{-1, -1, 0, -1}, P{-1, -1, 1, -1}, P{-1, 0, -1, -1},
P{-1, 0, 0, -1}, P{-1, 0, 1, -1}, P{-1, 1, -1, -1}, P{-1, 1, 0, -1},
P{-1, 1, 1, -1}, P{0, -1, -1, -1}, P{0, -1, 0, -1}, P{0, -1, 1, -1},
P{0, 0, -1, -1}, P{0, 0, 0, -1}, P{0, 0, 1, -1}, P{0, 1, -1, -1},
P{0, 1, 0, -1}, P{0, 1, 1, -1}, P{1, -1, -1, -1}, P{1, -1, 0, -1},
P{1, -1, 1, -1}, P{1, 0, -1, -1}, P{1, 0, 0, -1}, P{1, 0, 1, -1},
P{1, 1, -1, -1}, P{1, 1, 0, -1}, P{1, 1, 1, -1}, P{-1, -1, -1, 0},
P{-1, -1, 0, 0}, P{-1, -1, 1, 0}, P{-1, 0, -1, 0}, P{-1, 0, 0, 0},
P{-1, 0, 1, 0}, P{-1, 1, -1, 0}, P{-1, 1, 0, 0}, P{-1, 1, 1, 0},
P{0, -1, -1, 0}, P{0, -1, 0, 0}, P{0, -1, 1, 0}, P{0, 0, -1, 0},
P{0, 0, 1, 0}, P{0, 1, -1, 0}, P{0, 1, 0, 0}, P{0, 1, 1, 0},
P{1, -1, -1, 0}, P{1, -1, 0, 0}, P{1, -1, 1, 0}, P{1, 0, -1, 0},
P{1, 0, 0, 0}, P{1, 0, 1, 0}, P{1, 1, -1, 0}, P{1, 1, 0, 0},
P{1, 1, 1, 0}, P{-1, -1, -1, 1}, P{-1, -1, 0, 1}, P{-1, -1, 1, 1},
P{-1, 0, -1, 1}, P{-1, 0, 0, 1}, P{-1, 0, 1, 1}, P{-1, 1, -1, 1},
P{-1, 1, 0, 1}, P{-1, 1, 1, 1}, P{0, -1, -1, 1}, P{0, -1, 0, 1},
P{0, -1, 1, 1}, P{0, 0, -1, 1}, P{0, 0, 0, 1}, P{0, 0, 1, 1},
P{0, 1, -1, 1}, P{0, 1, 0, 1}, P{0, 1, 1, 1}, P{1, -1, -1, 1},
P{1, -1, 0, 1}, P{1, -1, 1, 1}, P{1, 0, -1, 1}, P{1, 0, 0, 1},
P{1, 0, 1, 1}, P{1, 1, -1, 1}, P{1, 1, 0, 1}, P{1, 1, 1, 1},
};

void Iterate(std::unordered_set<int> &active, int cycle) {
std::unordered_set<int> new_active;
for (int x = -cycle; x < kSize + cycle; ++x) {
for (int y = -cycle; y < kSize + cycle; ++y) {
for (int z = -cycle; z < 1 + cycle; ++z) {
for (int w = -cycle; w < 1 + cycle; ++w) {
P pos = {x, y, z, w};
int cnt = 0;
for (const P &neighbor : kNeighbors) {
if (contains(active, to_index(pos + neighbor))) {
++cnt;
}
}
int index = to_index(pos);
if (contains(active, index)) {
if (cnt == 2 || cnt == 3) {
new_active.insert(index);
}
} else {
if (cnt == 3) {
new_active.insert(index);
}
}
}
}
}
}
std::swap(active, new_active);
}

std::string run(const std::string &input) {
int x = 0;
int y = 0;
std::unordered_set<int> active = {};
for (auto c : input) {
if (c == '\n') {
++x;
y = 0;
continue;
}
if (c == '#') {
active.insert(to_index({x, y, 0, 0}));
}
++y;
}
for (int cycle = 1; cycle <= kCycles; ++cycle) {
Iterate(active, cycle);
}
return std::to_string(active.size());
}

int main(int argc, char **argv) {
if (argc < 2) {
std::cout << "Missing one argument" << std::endl;
exit(1);
}

clock_t start = clock();
auto answer = run(argv[1]);

std::cout << "_duration:" << float(clock() - start) * 1000.0 / CLOCKS_PER_SEC
<< "\n";
std::cout << answer << "\n";
return 0;
}

0 comments on commit e7356e3

Please sign in to comment.