diff options
-rw-r--r-- | src/2015/day7/aoc.h | 15 | ||||
-rw-r--r-- | src/common.h | 7 | ||||
-rw-r--r-- | test/test_2015.cpp | 12 |
3 files changed, 24 insertions, 10 deletions
diff --git a/src/2015/day7/aoc.h b/src/2015/day7/aoc.h index b03c3a8..e161166 100644 --- a/src/2015/day7/aoc.h +++ b/src/2015/day7/aoc.h @@ -66,6 +66,7 @@ struct cals { } result compute(line_view w) const noexcept { + // std::cout << "compute " << w << std::endl; uint16_t x = 0; if (is_value(w, &x)) { return {result::c_value, x}; @@ -75,33 +76,33 @@ struct cals { } } - std::function<result()> op_and(line_view x, line_view y) const noexcept { + std::function<result()> op_and(line_view& x, line_view& y) const noexcept { return [&x, &y, this]() -> result { return compute(x).op_and(compute(y)); }; } - std::function<result()> op_or(line_view x, line_view y) const noexcept { + std::function<result()> op_or(line_view& x, line_view& y) const noexcept { return [&x, &y, this]() -> result { return compute(x).op_or(compute(y)); }; } - std::function<result()> op_leftshift(line_view x, line_view y) const noexcept { + std::function<result()> op_leftshift(line_view& x, line_view& y) const noexcept { return [&x, &y, this]() -> result { return compute(x).op_leftshift(compute(y)); }; } - std::function<result()> op_rightshift(line_view x, line_view y) const noexcept { + std::function<result()> op_rightshift(line_view& x, line_view& y) const noexcept { return [&x, &y, this]() -> result { return compute(x).op_rightshift(compute(y)); }; } - std::function<result()> op_not(line_view x, line_view y) const noexcept { + std::function<result()> op_not(line_view& x, line_view& y) const noexcept { return [&x, this]() -> result { return compute(x).op_not(); }; } - std::function<result()> op_value(line_view x, line_view y) const noexcept { + std::function<result()> op_value(line_view& x, line_view& y) const noexcept { return [&x, this]() -> result { return compute(x); }; } std::function<result()> parse(line_view line, line_view* x, line_view* y, line_view* w) const noexcept { static struct _ { - std::function<result()> (cals::*op)(line_view, line_view) const noexcept; + std::function<result()> (cals::*op)(line_view&, line_view&) const noexcept; const char* label; } ops[] = { {&cals::op_and, "AND"}, diff --git a/src/common.h b/src/common.h index e9dd01c..7659322 100644 --- a/src/common.h +++ b/src/common.h @@ -27,9 +27,14 @@ struct line_view { size_t i = 0; size_t j = 0; while (i < length && j < lv.length) { - if (line[i++] < line[j++]) { + if (line[i] < lv.line[j]) { return true; } + if (line[i] > lv.line[j]) { + return false; + } + i++; + j++; } return j < lv.length; } diff --git a/test/test_2015.cpp b/test/test_2015.cpp index c3b60b9..159e36b 100644 --- a/test/test_2015.cpp +++ b/test/test_2015.cpp @@ -91,6 +91,14 @@ TEST_CASE("Probably a Fire Hazard", "[day6]") { TEST_CASE("Some Assembly Required", "[day 7]") { aoc2015::cals cals; - cals.parse("1 OR 2 -> ab"); - printf("%d\n", cals.compute("ab").value); + cals.parse("1 -> ab"); + cals.parse("8 RSHIFT ab -> x"); + cals.parse("1 LSHIFT x -> ac"); + cals.parse("ac OR ab -> ae"); + cals.parse("NOT x -> f"); + REQUIRE(1 == cals.compute("ab").value); + REQUIRE(4 == cals.compute("x").value); + REQUIRE(16 == cals.compute("ac").value); + REQUIRE(17 == cals.compute("ae").value); + REQUIRE(65531 == cals.compute("f").value); } |