aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day10/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-12-10 16:14:55 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-12-10 16:14:55 +0800
commit15db562e03e485010380cdea6a3c2005c05f17cf (patch)
tree5aa1102952c7b33d4082d61a1e2b58c2a6c88916 /src/2022/day10/aoc.cpp
parent9081328b9a158c447ee7f23eaa45ff235250f9a6 (diff)
downloadadvent-of-code-15db562e03e485010380cdea6a3c2005c05f17cf.tar.gz
advent-of-code-15db562e03e485010380cdea6a3c2005c05f17cf.zip
2022 day10
Diffstat (limited to 'src/2022/day10/aoc.cpp')
-rw-r--r--src/2022/day10/aoc.cpp105
1 files changed, 104 insertions, 1 deletions
diff --git a/src/2022/day10/aoc.cpp b/src/2022/day10/aoc.cpp
index 58b89e4..b076561 100644
--- a/src/2022/day10/aoc.cpp
+++ b/src/2022/day10/aoc.cpp
@@ -1,10 +1,113 @@
#include "aoc.h"
+#include <functional>
+#include <string.h>
namespace aoc2022 {
+static int X = 1;
+static int cycles = 1;
+
+static int get_number(const char *p) {
+ int sign = *p == '-' ? -1 : 1;
+ if (sign == -1) p++;
+ int d{0};
+ while(*p >= '0' && *p <= '9') {
+ d = d * 10 + *p - '0';
+ p++;
+ }
+ return sign * d;
+}
+
+struct CP {
+ int d{0};
+ bool done = false;
+
+ CP(int x): d(x) {}
+};
+
+void point(int d, CP cp[], int* total) {
+ // printf("cycle %d X %d\n", d, X);
+ for (int i = 0; i < 6; i++) {
+ if (!cp[i].done && cp[i].d == d) {
+ *total += X * d;
+ // printf("%d: %d = %d\n", d, X, X * d);
+ cp[i].done = true;
+ break;
+ }
+ }
+}
+
+// static void print(char p[]) {
+// for(int i = 0; i < 240; i++) {
+// printf("%c", p[i]);
+// if ((i+1) % 40 == 0) {
+// printf("\n");
+// }
+// }
+// }
+
+static void draw(int i, char crt[], char c) {
+ crt[i - 1] = c;
+}
+
+static void reset(char crt[]) {
+ for (int i = 0; i < 240; i++) {
+ * (crt + i) = '.';
+ }
+}
+
+static void set(char crt[], int x) {
+ reset(crt);
+ crt[x] = '#';
+ crt[x+1] = '#';
+ crt[x+2] = '#';
+}
+
std::pair<int, int> day10(line_view file)
{
- return {0, 0};
+
+ char screen[240] = {0};
+ char sprite[240] = {0};
+ reset(screen);
+ set(sprite, X);
+
+ struct {
+ const char *cmd;
+ std::function<void(const char*, CP [], int*, char [])> f;
+ } cmds[] = {
+ {"addx", [&sprite](const char* p, CP cp[], int* total, char crt[]){
+ auto d = get_number(p);
+ point(cycles, cp, total);
+ draw(cycles, crt, sprite[cycles % 40]);
+ cycles += 1;
+ point(cycles, cp, total);
+ draw(cycles, crt, sprite[cycles % 40]);
+ cycles += 1;
+ X += d;
+ set(sprite, X);
+ }},
+ {"noop", [&sprite](const char* p, CP cp[], int* total, char crt[]){
+ // printf("noop\n");
+ point(cycles, cp, total);
+ draw(cycles, crt, sprite[cycles % 40]);
+ cycles += 1;
+ }},
+ };
+
+ CP cp[] = {20, 60, 100, 140, 180, 220};
+ int total{0};
+ per_line(file, [&cmds, &cp, &total, &screen](line_view lv) {
+ for(auto &c: cmds) {
+ if (strncmp(c.cmd, lv.line, 4) == 0) {
+ c.f(lv.line+5, cp, &total, screen);
+ break;
+ }
+ }
+ return true;
+ });
+
+ // print(screen);
+ return {total, 0};
}
} // namespace aoc2022