|
| 1 | +// |
| 2 | +// Copyright (C) Martin Beaudoin. 2020. All Rights Reserved. |
| 3 | +// |
| 4 | +// See the repository's LICENSE file for the full license details. |
| 5 | +// |
| 6 | + |
| 7 | +#include <iostream> |
| 8 | +#include <cassert> |
| 9 | +#include <vector> |
| 10 | +#include <numeric> // std::adjacent_difference |
| 11 | +#include <algorithm> // std::adjacent_find |
| 12 | +#include "myutils.h" |
| 13 | + |
| 14 | +using namespace std; |
| 15 | + |
| 16 | +// Solve puzzle #1 |
| 17 | +template <typename T> |
| 18 | +constexpr int solve_puzzle1(T data) |
| 19 | +{ |
| 20 | + int nbrDiff1 = 1; // accounting for charging outlet |
| 21 | + int nbrDiff3 = 1; // accounting for device builtin adapter |
| 22 | + std::sort(data.begin(), data.end()); |
| 23 | + |
| 24 | + //cout << " Puzzle #1:" << endl; |
| 25 | + |
| 26 | + for(auto i = data.begin(); i < data.end() - 1; i++) |
| 27 | + { |
| 28 | + //cout << *i << ":" << *(i+1) << ":" << *(i+1) - *i << endl; |
| 29 | + |
| 30 | + nbrDiff1 += (*(i+1) - *i == 1); |
| 31 | + nbrDiff3 += (*(i+1) - *i == 3); |
| 32 | + } |
| 33 | + |
| 34 | + return nbrDiff1 * nbrDiff3; |
| 35 | +} |
| 36 | + |
| 37 | +long sumOfConsecutiveNumbers(int n) |
| 38 | +{ |
| 39 | + long sum=0; |
| 40 | + for(int i=1; i<=n; i++) |
| 41 | + sum += i; |
| 42 | + |
| 43 | + return sum; |
| 44 | +} |
| 45 | +// Solve puzzle #2 |
| 46 | +template <typename T> |
| 47 | +unsigned long solve_puzzle2(T data) |
| 48 | +{ |
| 49 | + unsigned long nbrCombinations = 0; |
| 50 | + // Add the charging outlet |
| 51 | + data.push_back(0); |
| 52 | + |
| 53 | + // Add the device builtin adapter |
| 54 | + data.push_back(*max_element(data.begin(), data.end()) + 3); |
| 55 | + |
| 56 | + std::sort(data.begin(), data.end()); |
| 57 | + |
| 58 | + // Find blocks of +1 deltas |
| 59 | + vector<int> diff(data.size()); |
| 60 | + std::adjacent_difference (data.begin(), data.end(), diff.begin()); |
| 61 | + |
| 62 | + // We can remove at most 2 out of a group of n consecutive delta1 |
| 63 | + |
| 64 | + // Compute the length of blocks of delta1 |
| 65 | + int blockLength = 0; |
| 66 | + for(auto d = diff.begin(); d < diff.end() - 1; d++) |
| 67 | + { |
| 68 | + if(*d == 1 && *(d+1) == 1) |
| 69 | + { |
| 70 | + blockLength++; |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + // We have a block of delta1 |
| 75 | + if(blockLength > 0) |
| 76 | + { |
| 77 | + // The number of possible combinations is the sum of [1..'blockLength'] numbers |
| 78 | + // plus 1 (the original unmodified sequence) |
| 79 | + int blockCombinations = sumOfConsecutiveNumbers(blockLength) + 1; |
| 80 | + |
| 81 | + if(nbrCombinations == 0) |
| 82 | + nbrCombinations += blockCombinations; |
| 83 | + else |
| 84 | + nbrCombinations *= blockCombinations; |
| 85 | + |
| 86 | + // Reset for next block |
| 87 | + blockLength = 0; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return nbrCombinations; |
| 92 | +} |
| 93 | + |
| 94 | +int main(int argc, char *argv[]) |
| 95 | +{ |
| 96 | + // Need a command-line parameter for the input filename |
| 97 | + if (argc != 2) { |
| 98 | + cerr << "Error: missing input" << endl; |
| 99 | + return EXIT_FAILURE; |
| 100 | + } |
| 101 | + |
| 102 | + string filename(argv[1]); |
| 103 | + |
| 104 | + // This file needs to exist |
| 105 | + if (! myutils::file_exists(filename)) |
| 106 | + { |
| 107 | + cerr << "Error: nonexistent file: " << filename << endl; |
| 108 | + return EXIT_FAILURE; |
| 109 | + } |
| 110 | + |
| 111 | + // Reading the data |
| 112 | + vector<int> data; |
| 113 | + myutils::read_file<int, vector<int>>(data, filename, true, false); |
| 114 | + |
| 115 | + // --------- Puzzle #1 --------- |
| 116 | + // Verify puzzle1 examples |
| 117 | + vector<int>example1 = { |
| 118 | + 16, |
| 119 | + 10, |
| 120 | + 15, |
| 121 | + 5, |
| 122 | + 1, |
| 123 | + 11, |
| 124 | + 7, |
| 125 | + 19, |
| 126 | + 6, |
| 127 | + 12, |
| 128 | + 4}; |
| 129 | + |
| 130 | + vector<int>example2 = { |
| 131 | + 28, |
| 132 | + 33, |
| 133 | + 18, |
| 134 | + 42, |
| 135 | + 31, |
| 136 | + 14, |
| 137 | + 46, |
| 138 | + 20, |
| 139 | + 48, |
| 140 | + 47, |
| 141 | + 24, |
| 142 | + 23, |
| 143 | + 49, |
| 144 | + 45, |
| 145 | + 19, |
| 146 | + 38, |
| 147 | + 39, |
| 148 | + 11, |
| 149 | + 1, |
| 150 | + 32, |
| 151 | + 25, |
| 152 | + 35, |
| 153 | + 8, |
| 154 | + 17, |
| 155 | + 7, |
| 156 | + 9, |
| 157 | + 4, |
| 158 | + 2, |
| 159 | + 34, |
| 160 | + 10, |
| 161 | + 3 |
| 162 | + }; |
| 163 | + |
| 164 | + assert(solve_puzzle1<vector<int>>(example1) == 35 && "Error verifying puzzle #1"); |
| 165 | + assert(solve_puzzle1<vector<int>>(example2) == 220 && "Error verifying puzzle #1"); |
| 166 | + |
| 167 | + // Solve puzzle #1 |
| 168 | + std::cout << "Answer for puzzle #1: "<< solve_puzzle1(data) << std::endl; |
| 169 | + |
| 170 | + // --------- Puzzle #2 --------- |
| 171 | + // Verify puzzle2 examples |
| 172 | + assert(solve_puzzle2<vector<int>>(example1) == 8 && "Error verifying puzzle #2 with example1"); |
| 173 | + assert(solve_puzzle2<vector<int>>(example2) == 19208 && "Error verifying puzzle #2 with example2"); |
| 174 | + |
| 175 | + // Solve puzzle #2 |
| 176 | + std::cout << "Answer for puzzle #2: "<< solve_puzzle2(data) << std::endl; |
| 177 | +} |
0 commit comments