diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-11-17 11:59:16 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-11-17 11:59:16 +0100 |
| commit | f619033995b385168c01f645b4b85b9703d66f02 (patch) | |
| tree | b3d74e36a0190777208bb7ebf53343ec408edb7b /2015/src/day16.cpp | |
| parent | f2afed267a34b5f0d76e63437d0fcc078d3841b0 (diff) | |
| download | advent-of-code-f619033995b385168c01f645b4b85b9703d66f02.tar.gz advent-of-code-f619033995b385168c01f645b4b85b9703d66f02.tar.bz2 advent-of-code-f619033995b385168c01f645b4b85b9703d66f02.zip | |
aoc 2015, day 16
Diffstat (limited to '2015/src/day16.cpp')
| -rw-r--r-- | 2015/src/day16.cpp | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/2015/src/day16.cpp b/2015/src/day16.cpp new file mode 100644 index 0000000..c9447a7 --- /dev/null +++ b/2015/src/day16.cpp | |||
| @@ -0,0 +1,141 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <sstream> | ||
| 4 | #include <string> | ||
| 5 | #include <vector> | ||
| 6 | using namespace std; | ||
| 7 | |||
| 8 | struct Sue { | ||
| 9 | optional<long> children; | ||
| 10 | optional<long> cats; | ||
| 11 | optional<long> samoyeds; | ||
| 12 | optional<long> pomeranians; | ||
| 13 | optional<long> akitas; | ||
| 14 | optional<long> vizslas; | ||
| 15 | optional<long> goldfish; | ||
| 16 | optional<long> trees; | ||
| 17 | optional<long> cars; | ||
| 18 | optional<long> perfumes; | ||
| 19 | }; | ||
| 20 | |||
| 21 | auto | ||
| 22 | split(const string& line, char sep = ' ') | ||
| 23 | { | ||
| 24 | vector<string> parts; | ||
| 25 | stringstream input{ line }; | ||
| 26 | |||
| 27 | for ( string part; getline(input, part, sep); ) { | ||
| 28 | parts.emplace_back(part); | ||
| 29 | } | ||
| 30 | |||
| 31 | return parts; | ||
| 32 | } | ||
| 33 | |||
| 34 | auto | ||
| 35 | read_file(string_view filename) | ||
| 36 | { | ||
| 37 | fstream input{ filename }; | ||
| 38 | vector<Sue> records; | ||
| 39 | |||
| 40 | auto invalidChar = [](char chr) { | ||
| 41 | return chr == ':' || chr == ','; | ||
| 42 | }; | ||
| 43 | |||
| 44 | for ( string line; getline(input, line); ) { | ||
| 45 | line.erase(remove_if(line.begin(), line.end(), invalidChar), line.end()); | ||
| 46 | auto parts = split(line); | ||
| 47 | |||
| 48 | Sue sue; | ||
| 49 | for ( size_t idx = 2; idx != parts.size(); idx += 2 ) { | ||
| 50 | auto key = parts[idx]; | ||
| 51 | auto value = stol(parts[idx + 1]); | ||
| 52 | if ( key == "children" ) { | ||
| 53 | sue.children = value; | ||
| 54 | } | ||
| 55 | else if ( key == "cats" ) { | ||
| 56 | sue.cats = value; | ||
| 57 | } | ||
| 58 | else if ( key == "samoyeds" ) { | ||
| 59 | sue.samoyeds = value; | ||
| 60 | } | ||
| 61 | else if ( key == "pomeranians" ) { | ||
| 62 | sue.pomeranians = value; | ||
| 63 | } | ||
| 64 | else if ( key == "akitas" ) { | ||
| 65 | sue.akitas = value; | ||
| 66 | } | ||
| 67 | else if ( key == "vizslas" ) { | ||
| 68 | sue.vizslas = value; | ||
| 69 | } | ||
| 70 | else if ( key == "goldfish" ) { | ||
| 71 | sue.goldfish = value; | ||
| 72 | } | ||
| 73 | else if ( key == "trees" ) { | ||
| 74 | sue.trees = value; | ||
| 75 | } | ||
| 76 | else if ( key == "cars" ) { | ||
| 77 | sue.cars = value; | ||
| 78 | } | ||
| 79 | else if ( key == "perfumes" ) { | ||
| 80 | sue.perfumes = value; | ||
| 81 | } | ||
| 82 | else { | ||
| 83 | cerr << "unknown property: " << key << ": " << value << endl; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | records.emplace_back(sue); | ||
| 87 | } | ||
| 88 | return records; | ||
| 89 | } | ||
| 90 | |||
| 91 | void | ||
| 92 | part1(const vector<Sue>& sues) | ||
| 93 | { | ||
| 94 | Sue testSue{ 3, 7, 2, 3, 0, 0, 5, 3, 2, 1 }; // NOLINT | ||
| 95 | |||
| 96 | for ( size_t idx = 0; idx != sues.size(); ++idx ) { | ||
| 97 | const auto& sue = sues[idx]; | ||
| 98 | if ( (!sue.children.has_value() || sue.children == testSue.children) && | ||
| 99 | (!sue.cats.has_value() || sue.cats == testSue.cats) && | ||
| 100 | (!sue.samoyeds.has_value() || sue.samoyeds == testSue.samoyeds) && | ||
| 101 | (!sue.pomeranians.has_value() || sue.pomeranians == testSue.pomeranians) && | ||
| 102 | (!sue.akitas.has_value() || sue.akitas == testSue.akitas) && | ||
| 103 | (!sue.vizslas.has_value() || sue.vizslas == testSue.vizslas) && | ||
| 104 | (!sue.goldfish.has_value() || sue.goldfish == testSue.goldfish) && | ||
| 105 | (!sue.trees.has_value() || sue.trees == testSue.trees) && | ||
| 106 | (!sue.cars.has_value() || sue.cars == testSue.cars) && | ||
| 107 | (!sue.perfumes.has_value() || sue.perfumes == testSue.perfumes) ) { | ||
| 108 | cout << idx + 1 << endl; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | void | ||
| 114 | part2(const vector<Sue>& sues) | ||
| 115 | { | ||
| 116 | Sue testSue{ 3, 7, 2, 3, 0, 0, 5, 3, 2, 1 }; // NOLINT | ||
| 117 | |||
| 118 | for ( size_t idx = 0; idx != sues.size(); ++idx ) { | ||
| 119 | const auto& sue = sues[idx]; | ||
| 120 | if ( (!sue.children.has_value() || sue.children == testSue.children) && | ||
| 121 | (!sue.cats.has_value() || sue.cats > testSue.cats) && | ||
| 122 | (!sue.samoyeds.has_value() || sue.samoyeds == testSue.samoyeds) && | ||
| 123 | (!sue.pomeranians.has_value() || sue.pomeranians < testSue.pomeranians) && | ||
| 124 | (!sue.akitas.has_value() || sue.akitas == testSue.akitas) && | ||
| 125 | (!sue.vizslas.has_value() || sue.vizslas == testSue.vizslas) && | ||
| 126 | (!sue.goldfish.has_value() || sue.goldfish < testSue.goldfish) && | ||
| 127 | (!sue.trees.has_value() || sue.trees > testSue.trees) && | ||
| 128 | (!sue.cars.has_value() || sue.cars == testSue.cars) && | ||
| 129 | (!sue.perfumes.has_value() || sue.perfumes == testSue.perfumes) ) { | ||
| 130 | cout << idx + 1 << endl; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | int | ||
| 136 | main() | ||
| 137 | { | ||
| 138 | auto sues = read_file("data/day16.txt"); | ||
| 139 | part1(sues); | ||
| 140 | part2(sues); | ||
| 141 | } | ||
