diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-11-11 18:16:20 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-11-11 18:16:20 +0100 |
| commit | d28cd15ecccea5e123905de524858dea18008ba4 (patch) | |
| tree | 75dea768925ce05ef150caf3c7fbc4f6cc1a9d10 /2015/src | |
| parent | 7c1b63888bcc82f9d49e25726359d5c73c3e92a4 (diff) | |
| download | advent-of-code-d28cd15ecccea5e123905de524858dea18008ba4.tar.gz advent-of-code-d28cd15ecccea5e123905de524858dea18008ba4.tar.bz2 advent-of-code-d28cd15ecccea5e123905de524858dea18008ba4.zip | |
aoc 2015, day01
Diffstat (limited to '2015/src')
| -rw-r--r-- | 2015/src/day01.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/2015/src/day01.cpp b/2015/src/day01.cpp new file mode 100644 index 0000000..5d6d482 --- /dev/null +++ b/2015/src/day01.cpp | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <string> | ||
| 4 | |||
| 5 | using namespace std; | ||
| 6 | |||
| 7 | string | ||
| 8 | read_file(string_view filename) | ||
| 9 | { | ||
| 10 | fstream input{ filename }; | ||
| 11 | return { istreambuf_iterator<char>{ input }, {} }; | ||
| 12 | } | ||
| 13 | |||
| 14 | void | ||
| 15 | part1(string_view puzzle) | ||
| 16 | { | ||
| 17 | int floor = 0; | ||
| 18 | for (const auto& chr : puzzle) { | ||
| 19 | if (chr == '(') { | ||
| 20 | ++floor; | ||
| 21 | } | ||
| 22 | else if (chr == ')') { | ||
| 23 | --floor; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | cout << floor << endl; | ||
| 27 | } | ||
| 28 | |||
| 29 | void | ||
| 30 | part2(string_view puzzle) | ||
| 31 | { | ||
| 32 | int floor = 0; | ||
| 33 | for (string_view::size_type pos = 0; pos != puzzle.length(); ++pos) { | ||
| 34 | const auto chr = puzzle[pos]; | ||
| 35 | if (chr == '(') { | ||
| 36 | ++floor; | ||
| 37 | } | ||
| 38 | else if (chr == ')') { | ||
| 39 | --floor; | ||
| 40 | } | ||
| 41 | if (floor == -1) { | ||
| 42 | cout << pos+1 << endl; | ||
| 43 | break; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | int | ||
| 49 | main() | ||
| 50 | { | ||
| 51 | auto puzzle = read_file("data/day01.txt"); | ||
| 52 | part1(puzzle); | ||
| 53 | part2(puzzle); | ||
| 54 | } | ||
