aboutsummaryrefslogtreecommitdiff
path: root/src/2016/day8/aoc.h
blob: 2313bc9fbe999614f20a7ffdf6872e2168a39c2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#pragma once
#include "common.h"

namespace aoc2016 {

template <size_t W, size_t H>
struct grid {
  char cells[W * H] = {0};

  int count() const noexcept {
    int total{0};
    for (size_t i = 0; i < ARRAY_SIZE(cells); i++) {
      total += int(cells[i] == 1);
    }
    return total;
  }

  void print() {
    for (size_t i = 0; i < ARRAY_SIZE(cells); i++) {
      printf("%s", cells[i] == 0 ? "." : "#");
      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