diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-03-15 09:29:28 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-03-15 09:29:28 +0800 |
commit | 8c0bc49502ee9101d3f761cd791ebe770012d26d (patch) | |
tree | 7cd518ca5d5f3ce679a408824a821de4f038146d | |
parent | 1b7948bab0ae9b23cdb0693e5590205d177bee13 (diff) | |
download | advent-of-code-8c0bc49502ee9101d3f761cd791ebe770012d26d.tar.gz advent-of-code-8c0bc49502ee9101d3f761cd791ebe770012d26d.zip |
day1
-rw-r--r-- | src/2015/day1/README.md | 11 | ||||
-rw-r--r-- | src/2015/day1/aoc.cpp | 31 | ||||
-rw-r--r-- | src/2015/day1/aoc.h | 1 | ||||
-rw-r--r-- | test/test_2015.cpp | 8 | ||||
-rw-r--r-- | test/test_common.cpp | 4 |
5 files changed, 50 insertions, 5 deletions
diff --git a/src/2015/day1/README.md b/src/2015/day1/README.md index 98b59a6..cf9151a 100644 --- a/src/2015/day1/README.md +++ b/src/2015/day1/README.md @@ -22,3 +22,14 @@ For example: To what floor do the instructions take Santa? +--- Part Two --- + +Now, given the same instructions, find the position of the first character that causes him to enter the basement (floor -1). The first character in the instructions has position 1, the second character has position 2, and so on. + +For example: + + ) causes him to enter the basement at character position 1. + ()()) causes him to enter the basement at character position 5. + +What is the position of the character that causes Santa to first enter the basement? + diff --git a/src/2015/day1/aoc.cpp b/src/2015/day1/aoc.cpp index 2263558..4a4d4fc 100644 --- a/src/2015/day1/aoc.cpp +++ b/src/2015/day1/aoc.cpp @@ -5,9 +5,38 @@ namespace aoc2015 { int day1(line_view lv) { int level = 0; for (size_t i = 0; i < lv.length; ++i) { - level += lv.line[i] == '(' ? 1 : -1; + switch (lv.line[i]) { + case '(': + level += 1; + break; + case ')': + level += -1; + break; + default: + break; + } } return level; } +int day1(line_view lv, int target) { + int level = 0; + for (size_t i = 0; i < lv.length; ++i) { + switch (lv.line[i]) { + case '(': + level += 1; + break; + case ')': + level += -1; + break; + default: + break; + } + if (level == target) { + return i + 1; + } + } + return -1; +} + } // namespace aoc2015 diff --git a/src/2015/day1/aoc.h b/src/2015/day1/aoc.h index b83b525..af68f6a 100644 --- a/src/2015/day1/aoc.h +++ b/src/2015/day1/aoc.h @@ -5,5 +5,6 @@ namespace aoc2015 { int day1(line_view lv); +int day1(line_view lv, int target); } diff --git a/test/test_2015.cpp b/test/test_2015.cpp index 67e25f0..7955ede 100644 --- a/test/test_2015.cpp +++ b/test/test_2015.cpp @@ -1,5 +1,9 @@ #include "catch.hpp" +#include "2015/day1/aoc.h" +#include <stdio.h> -TEST_CASE("", "[]") { - +TEST_CASE("Not Quite Lisp", "[]") { + line_view lv = load_file("../src/2015/day1/input"); + REQUIRE(aoc2015::day1(lv) == 74); + REQUIRE(aoc2015::day1(lv, -1) == 1795); } diff --git a/test/test_common.cpp b/test/test_common.cpp index 141398b..851bef7 100644 --- a/test/test_common.cpp +++ b/test/test_common.cpp @@ -2,8 +2,8 @@ #include "common.h" TEST_CASE("load file", "[]") { - line_view file{"\n\n111\n2222\n33333\n", 17}; - const char* lines[] = {"\n", "\n", "111\n", "2222\n", "33333\n"}; + line_view file{"\n111\n2222\n33333\n\n", 17}; + const char* lines[] = {"\n", "111\n", "2222\n", "33333\n", "\n"}; int i = 0; per_line( file, |