aboutsummaryrefslogtreecommitdiff
path: root/src/2016
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-05-09 15:03:28 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-05-09 15:03:28 +0800
commitdca2e70a10b7928c8a73f73d5dc7ef74f9001005 (patch)
treec63729b19e4f78df3634fd8691f75f8533441523 /src/2016
parent7c3b65e0e91161526c5f8d6feebef1199709c6a8 (diff)
downloadadvent-of-code-dca2e70a10b7928c8a73f73d5dc7ef74f9001005.tar.gz
advent-of-code-dca2e70a10b7928c8a73f73d5dc7ef74f9001005.zip
2016 day8 part1
Diffstat (limited to 'src/2016')
-rw-r--r--src/2016/day8/aoc.cpp6
-rw-r--r--src/2016/day8/aoc.h67
2 files changed, 72 insertions, 1 deletions
diff --git a/src/2016/day8/aoc.cpp b/src/2016/day8/aoc.cpp
index 55f4247..fd56a51 100644
--- a/src/2016/day8/aoc.cpp
+++ b/src/2016/day8/aoc.cpp
@@ -2,4 +2,10 @@
namespace aoc2016 {
+int day8(line_view file) {
+ grid<5, 6> g;
+ g.set(2, 2);
+ return 0;
}
+
+} // namespace aoc2016
diff --git a/src/2016/day8/aoc.h b/src/2016/day8/aoc.h
index 95823fe..d92a8f7 100644
--- a/src/2016/day8/aoc.h
+++ b/src/2016/day8/aoc.h
@@ -3,4 +3,69 @@
namespace aoc2016 {
-}
+template <size_t W, size_t H>
+struct grid {
+ char cells[W * H] = {0};
+
+ void print() {
+ for (size_t i = 0; i < ARRAY_SIZE(cells); i++) {
+ printf("%d", cells[i]);
+ if (i % W == W - 1) {
+ printf("\n");
+ }
+ }
+ printf("\n");
+ }
+
+ void set(int x, int y) {
+ for (int j = 0; j < y; j++) {
+ for (int i = 0; i < x; i++) {
+ cell(i, j) = 1;
+ }
+ }
+ }
+
+ char& cell(int x, int y) { return cells[y * W + x]; }
+ char& prev_h(int x, int y) {
+ int n = x == 0 ? W - 1 : x - 1;
+ return cell(n, y);
+ }
+
+ void rotate_h(int y) {
+ char last = cell(W - 1, y);
+ for (int x = W - 1; x > 0; x--) {
+ cell(x, y) = prev_h(x, y);
+ }
+ cell(0, y) = last;
+ }
+
+ void rotate_h(int y, int t) {
+ t %= W;
+ while (t-- > 0) {
+ rotate_h(y);
+ }
+ }
+
+ char& prev_v(int x, int y) {
+ int n = y == 0 ? H - 1 : y - 1;
+ return cell(x, n);
+ }
+
+ void rotate_v(int x) {
+ char last = cell(x, H - 1);
+ for (int y = H - 1; y > 0; y--) {
+ cell(x, y) = prev_v(x, y);
+ }
+ cell(x, 0) = last;
+ }
+
+ void rotate_v(int x, int t) {
+ t %= H;
+ while (t-- > 0) {
+ rotate_v(x);
+ }
+ }
+};
+
+int day8(line_view);
+} // namespace aoc2016