diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-01-04 14:15:17 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-01-04 14:15:17 +0100 |
| commit | d73937c6977a7cb6b2516ba662495ef662fd3fb0 (patch) | |
| tree | 3cefbb11ff5535ea7265712b61a2fa23129d2bf9 | |
| parent | 3ce3a813e312bc61455dd73f32d6ae13bdf78c35 (diff) | |
| download | advent-of-code-d73937c6977a7cb6b2516ba662495ef662fd3fb0.tar.gz advent-of-code-d73937c6977a7cb6b2516ba662495ef662fd3fb0.tar.bz2 advent-of-code-d73937c6977a7cb6b2516ba662495ef662fd3fb0.zip | |
solution for day 3
| -rw-r--r-- | 2020/src/day03.cpp | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/2020/src/day03.cpp b/2020/src/day03.cpp new file mode 100644 index 0000000..f9cf200 --- /dev/null +++ b/2020/src/day03.cpp | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <functional> | ||
| 3 | #include <iostream> | ||
| 4 | #include <iterator> | ||
| 5 | #include <numeric> | ||
| 6 | #include <string> | ||
| 7 | #include <vector> | ||
| 8 | using namespace std; | ||
| 9 | |||
| 10 | auto | ||
| 11 | read_file(string_view filename) | ||
| 12 | { | ||
| 13 | fstream input{ filename }; | ||
| 14 | vector<string> data; | ||
| 15 | |||
| 16 | for ( string line; getline(input, line); ) { | ||
| 17 | data.emplace_back(line); | ||
| 18 | } | ||
| 19 | |||
| 20 | return data; | ||
| 21 | } | ||
| 22 | |||
| 23 | auto | ||
| 24 | solve(const vector<string>& puzzle, size_t delta_col, size_t delta_row) | ||
| 25 | { | ||
| 26 | size_t col = 0; | ||
| 27 | size_t row = 0; | ||
| 28 | |||
| 29 | auto sum = 0L; | ||
| 30 | while ( row < puzzle.size() ) { | ||
| 31 | if ( puzzle[row][col] == '#' ) { | ||
| 32 | ++sum; | ||
| 33 | } | ||
| 34 | |||
| 35 | row += delta_row; | ||
| 36 | |||
| 37 | col += delta_col; | ||
| 38 | col %= puzzle[0].size(); | ||
| 39 | } | ||
| 40 | return sum; | ||
| 41 | } | ||
| 42 | |||
| 43 | void | ||
| 44 | part1(const vector<string>& puzzle) | ||
| 45 | { | ||
| 46 | cout << solve(puzzle, 3, 1) << endl; | ||
| 47 | } | ||
| 48 | |||
| 49 | void | ||
| 50 | part2(const vector<string>& puzzle) | ||
| 51 | { | ||
| 52 | cout << solve(puzzle, 1, 1) * solve(puzzle, 3, 1) * solve(puzzle, 5, 1) * solve(puzzle, 7, 1) * solve(puzzle, 1, 2) << endl; | ||
| 53 | } | ||
| 54 | |||
| 55 | int | ||
| 56 | main() | ||
| 57 | { | ||
| 58 | const auto puzzle = read_file("data/day03.txt"); | ||
| 59 | part1(puzzle); | ||
| 60 | part2(puzzle); | ||
| 61 | } | ||
| 62 | |||
