diff options
-rw-r--r-- | src/2018/day3/aoc.cpp | 17 | ||||
-rw-r--r-- | src/2018/day3/aoc.h | 16 | ||||
-rw-r--r-- | test/test_2018.cpp | 4 |
3 files changed, 30 insertions, 7 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 diff --git a/test/test_2018.cpp b/test/test_2018.cpp index db1c037..f6c61bd 100644 --- a/test/test_2018.cpp +++ b/test/test_2018.cpp @@ -24,5 +24,7 @@ TEST_CASE("Inventory Management System", "[2018]") { TEST_CASE("No Matter How You Slice It", "[2018]") { line_view lv = load_file("../src/2018/day3/input"); - REQUIRE(109785 == aoc2018::day3(lv, 1)); + auto p = aoc2018::day3(lv, 1); + REQUIRE(109785 == p.first); + REQUIRE(504 == p.second); } |