aboutsummaryrefslogtreecommitdiff
path: root/src/2018/day3/aoc.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/2018/day3/aoc.h')
-rw-r--r--src/2018/day3/aoc.h60
1 files changed, 59 insertions, 1 deletions
diff --git a/src/2018/day3/aoc.h b/src/2018/day3/aoc.h
index 4bfdbf3..24fad47 100644
--- a/src/2018/day3/aoc.h
+++ b/src/2018/day3/aoc.h
@@ -1,6 +1,64 @@
#pragma once
#include "common.h"
+#include <vector>
namespace aoc2018 {
-}
+struct claim {
+ int id;
+ int x;
+ int y;
+ int width;
+ int height;
+};
+
+struct fabric {
+ // 0 ... 999
+ uint16_t claims[1000 * 1000] = {0};
+
+ void apply(const claim& c) {
+ for (int x = c.x; x < c.x + c.width; x++) {
+ for (int y = c.y; y < c.y + c.height; y++) {
+ claims[y * 1000 + x] += 1;
+ }
+ }
+ }
+
+ int count(int x) {
+ int total{0};
+ for (auto c : claims) {
+ if (c > x) {
+ total += 1;
+ }
+ }
+ return total;
+ }
+
+ void get_number(const char** pp, int* is[], int i) {
+ *is[i] = 0;
+ const char* p = *pp;
+ while (*p >= '0' && *p <= '9') {
+ *is[i] = *is[i] * 10 + *p - '0';
+ p++;
+ }
+ *pp = p;
+ }
+
+ void parse(line_view lv) {
+ claim c;
+ int* is[] = {&c.id, &c.x, &c.y, &c.width, &c.height};
+ int i{0};
+ const char* p = lv.line;
+ while (p < lv.line + lv.length) {
+ if (*p >= '0' && *p <= '9') {
+ get_number(&p, is, i++);
+ }
+ p++;
+ }
+ apply(c);
+ }
+};
+
+int day3(line_view, int);
+
+} // namespace aoc2018