diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/2018/day3/aoc.cpp | 17 | ||||
-rw-r--r-- | src/2018/day3/aoc.h | 16 |
2 files changed, 27 insertions, 6 deletions
diff --git a/src/2018/day3/aoc.cpp b/src/2018/day3/aoc.cpp index c5aa403..3ea5432 100644 --- a/src/2018/day3/aoc.cpp +++ b/src/2018/day3/aoc.cpp @@ -2,14 +2,23 @@ namespace aoc2018 { -int day3(line_view file, int x) { +std::pair<int, int> day3(line_view file, int x) { + fabric* f = new fabric; - per_line(file, [&f](line_view lv) { - f->parse(lv); + std::vector<claim> cs; + per_line(file, [&f, &cs](line_view lv) { + f->parse(lv, cs); return true; }); - return f->count(x); + int id{0}; + for (auto& c : cs) { + if (!f->overlap(c)) { + id = c.id; + } + } + + return {f->count(x), id}; } } // namespace aoc2018 diff --git a/src/2018/day3/aoc.h b/src/2018/day3/aoc.h index 24fad47..42b3c90 100644 --- a/src/2018/day3/aoc.h +++ b/src/2018/day3/aoc.h @@ -34,6 +34,17 @@ struct fabric { return total; } + bool overlap(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++) { + if (claims[y * 1000 + x] > 1) { + return true; + } + } + } + return false; + } + void get_number(const char** pp, int* is[], int i) { *is[i] = 0; const char* p = *pp; @@ -44,7 +55,7 @@ struct fabric { *pp = p; } - void parse(line_view lv) { + void parse(line_view lv, std::vector<claim>& cs) { claim c; int* is[] = {&c.id, &c.x, &c.y, &c.width, &c.height}; int i{0}; @@ -56,9 +67,10 @@ struct fabric { p++; } apply(c); + cs.push_back(c); } }; -int day3(line_view, int); +std::pair<int, int> day3(line_view, int); } // namespace aoc2018 |