aboutsummaryrefslogtreecommitdiff
path: root/src/2016/day21/aoc.cpp
blob: 8c72582ca5578e9a36bf7c20370a7238b6144c37 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "aoc.h"

namespace aoc2016 {

typedef void (*scramble)(char cs[8], int, int);

static int index(char cs[8], char x) {
  for (int i = 0; i < 8; i++) {
    if (cs[i] == x) {
      return i;
    }
  }
  return 8;
}

// swap position X with position Y
static void swap(char cs[8], int x, int y) { std::swap(cs[x], cs[y]); }

// swap letter X with letter Y
static void swap_char(char cs[8], int x, int y) {
  auto xi = index(cs, (char)x);
  auto yi = index(cs, (char)y);
  std::swap(cs[xi], cs[yi]);
}

// rotate left/right X steps
static void rotate(char cs[8], int left, int right) {
  while (left-- > 0) {
    char head = cs[0];
    for (int i = 1; i < 8; i++) {
      cs[i - 1] = cs[i];
    }
    cs[7] = head;
  }
  while (right-- > 0) {
    char tail = cs[7];
    for (int i = 6; i >= 0; i--) {
      cs[i + 1] = cs[i];
    }
    cs[0] = tail;
  }
}

// rotate based on position of letter X
// determine the index of letter X
// rotate the string to the right one time
// plus a number of times equal to that index
// plus one additional time if the index was at least 4
static void rotate_char(char cs[8], int x, int) {
  int i = index(cs, x);
  rotate(cs, 0, i >= 4 ? 2 + i : 1 + i);
}

// reverse positions X through Y
// including the letters at X and Y
static void reverse(char cs[8], int x, int y) {
  while (x < y) {
    std::swap(cs[x], cs[y]);
    x++;
    y--;
  }
}

// move position X to position Y
static void move(char cs[8], int x, int y) {
  char c = cs[x];
  if (x > y) {
    for (int i = x; i > y; i--) {
      cs[i] = cs[i - 1];
    }
  }
  if (x < y) {
    for (int i = x; i < y; i++) {
      cs[i] = cs[i + 1];
    }
  }
  cs[y] = c;
}

typedef void (*parse)(const char*, int ds[2]);
static void parse_swap(const char* p, int ds[2]) {
  ds[0] = *(p + 14) - '0';
  ds[1] = *(p + 30) - '0';
}

static void parse_swap_char(const char* p, int ds[2]) {
  ds[0] = *(p + 12);
  ds[1] = *(p + 26);
}

static void parse_rotate(const char* p, int ds[2]) {
  if (*(p + 7) == 'l') {
    ds[0] = *(p + 12) - '0';
  }
  if (*(p + 7) == 'r') {
    ds[1] = *(p + 13) - '0';
  }
}

static void parse_rotate_char(const char* p, int ds[2]) {
  ds[0] = *(p + 35);
  ds[1] = 0;
}

static void parse_reverse(const char* p, int ds[2]) {
  ds[0] = *(p + 18) - '0';
  ds[1] = *(p + 28) - '0';
}

static void parse_move(const char* p, int ds[2]) {
  ds[0] = *(p + 14) - '0';
  ds[1] = *(p + 28) - '0';
}

static void print(char* cs) {
  for (int i = 0; i < 8; i++) {
    auto c = cs[i];
    printf("%c", c);
  }
  printf("\n");
}

std::pair<int64_t, int64_t> day21(line_view file) {
  struct {
    parse pf;
    scramble sf;
  } fs[] = {{parse_swap, swap},       {parse_swap_char, swap_char},
            {parse_rotate, rotate},   {parse_rotate_char, rotate_char},
            {parse_reverse, reverse}, {parse_move, move}};

  char cs[8] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
  per_line(
      file,
      [fs](line_view lv, char cs[8]) {
        const char* p = lv.line;
        int ds[2] = {0, 0};
        int i = 6;
        if (*p == 's' && *(p + 5) == 'p') {
          i = 0;
        }
        if (*p == 's' && *(p + 5) == 'l') {
          i = 1;
        }
        if (*p == 'r' && *(p + 1) == 'o' && *(p + 7) == 'r') {
          i = 2;
        }
        if (*p == 'r' && *(p + 1) == 'o' && *(p + 7) == 'l') {
          i = 2;
        }
        if (*p == 'r' && *(p + 1) == 'o' && *(p + 7) == 'b') {
          i = 3;
        }
        if (*p == 'r' && *(p + 1) == 'e') {
          i = 4;
        }
        if (*p == 'm') {
          i = 5;
        }
        fs[i].pf(p, ds);
        fs[i].sf(cs, ds[0], ds[1]);
        print(cs);
        return true;
      },
      cs);

  print(cs);
  return {0, 0};
}

} // namespace aoc2016