aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/2015/day1/README.md11
-rw-r--r--src/2015/day1/aoc.cpp31
-rw-r--r--src/2015/day1/aoc.h1
3 files changed, 42 insertions, 1 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);
}