diff options
| author | Thomas Schmucker <ts@its1.de> | 2023-12-06 18:08:52 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2023-12-06 18:08:52 +0100 |
| commit | 87948e3f7a475c12259aac9c2b883691328c1130 (patch) | |
| tree | c77040461a2abadb70d070a0ee9a60e4e365bd6f | |
| parent | f95d6c39353700924a71014ea4b05b1d9c865284 (diff) | |
| download | advent-of-code-87948e3f7a475c12259aac9c2b883691328c1130.tar.gz advent-of-code-87948e3f7a475c12259aac9c2b883691328c1130.tar.bz2 advent-of-code-87948e3f7a475c12259aac9c2b883691328c1130.zip | |
Lösung für Tag6
| -rw-r--r-- | makefile | 3 | ||||
| -rw-r--r-- | src/day06.cpp | 74 |
2 files changed, 76 insertions, 1 deletions
| @@ -4,7 +4,8 @@ all: bin/day01 \ | |||
| 4 | bin/day02 \ | 4 | bin/day02 \ |
| 5 | bin/day03 \ | 5 | bin/day03 \ |
| 6 | bin/day04 \ | 6 | bin/day04 \ |
| 7 | bin/day05 | 7 | bin/day05 \ |
| 8 | bin/day06 | ||
| 8 | 9 | ||
| 9 | bin: | 10 | bin: |
| 10 | mkdir $@ | 11 | mkdir $@ |
diff --git a/src/day06.cpp b/src/day06.cpp new file mode 100644 index 0000000..9a429c5 --- /dev/null +++ b/src/day06.cpp | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | #include <cstdint> | ||
| 2 | #include <fstream> | ||
| 3 | #include <iostream> | ||
| 4 | #include <numeric> | ||
| 5 | #include <sstream> | ||
| 6 | #include <vector> | ||
| 7 | using namespace std; | ||
| 8 | |||
| 9 | template<typename T = long> | ||
| 10 | vector<T> | ||
| 11 | read_ints(const string& line) | ||
| 12 | { | ||
| 13 | stringstream iss{ line }; | ||
| 14 | return vector<T>{ istream_iterator<T>{ iss }, istream_iterator<T>{} }; | ||
| 15 | } | ||
| 16 | |||
| 17 | long | ||
| 18 | solve(long time, long winningDistance) | ||
| 19 | { | ||
| 20 | long counter = 0; | ||
| 21 | for ( long pushTime = 0; pushTime < time; ++pushTime ) { | ||
| 22 | auto distance = (time * pushTime - pushTime * pushTime); | ||
| 23 | counter += long(distance > winningDistance); | ||
| 24 | } | ||
| 25 | return counter; | ||
| 26 | } | ||
| 27 | |||
| 28 | void | ||
| 29 | part1() | ||
| 30 | { | ||
| 31 | fstream input{ "data/day06.txt" }; | ||
| 32 | string line; | ||
| 33 | |||
| 34 | getline(input, line); | ||
| 35 | auto times = read_ints(line.substr(line.find(':') + 1)); | ||
| 36 | |||
| 37 | getline(input, line); | ||
| 38 | auto distances = read_ints(line.substr(line.find(':') + 1)); | ||
| 39 | |||
| 40 | long result = 1; | ||
| 41 | for ( size_t idx = 0; idx != times.size(); ++idx ) { | ||
| 42 | result *= solve(times[idx], distances[idx]); | ||
| 43 | } | ||
| 44 | cout << result << endl; | ||
| 45 | } | ||
| 46 | |||
| 47 | string | ||
| 48 | join(const string& line) | ||
| 49 | { | ||
| 50 | stringstream iss{ line }; | ||
| 51 | return accumulate(istream_iterator<string>{ iss }, istream_iterator<string>{}, string{}); | ||
| 52 | } | ||
| 53 | |||
| 54 | void | ||
| 55 | part2() | ||
| 56 | { | ||
| 57 | fstream input{ "data/day06.txt" }; | ||
| 58 | string line; | ||
| 59 | |||
| 60 | getline(input, line); | ||
| 61 | auto time = stol(join(line.substr(line.find(':') + 1))); | ||
| 62 | |||
| 63 | getline(input, line); | ||
| 64 | auto distance = stol(join(line.substr(line.find(':') + 1))); | ||
| 65 | |||
| 66 | cout << solve(time, distance) << endl; | ||
| 67 | } | ||
| 68 | |||
| 69 | int | ||
| 70 | main() | ||
| 71 | { | ||
| 72 | part1(); | ||
| 73 | part2(); | ||
| 74 | } | ||
