|
1 | 1 | //
|
2 |
| -// Copyright (C) Martin Beaudoin. 2019. All Rights Reserved. |
| 2 | +// Copyright (C) Martin Beaudoin. 2020. All Rights Reserved. |
3 | 3 | //
|
4 | 4 | // See the repository's LICENSE file for the full license details.
|
5 | 5 | //
|
6 | 6 |
|
7 | 7 | #include <iostream>
|
8 | 8 | #include <cassert>
|
9 | 9 | #include <vector>
|
| 10 | +#include <sstream> // std::stringstream |
10 | 11 | #include "myutils.h"
|
11 | 12 |
|
12 | 13 | using namespace std;
|
13 | 14 |
|
| 15 | +typedef enum |
| 16 | +{ |
| 17 | + ON, |
| 18 | + OFF, |
| 19 | + TOGGLE |
| 20 | +} Command; |
| 21 | + |
| 22 | +class Instruction |
| 23 | +{ |
| 24 | +public: |
| 25 | + Command command; // Command |
| 26 | + myutils::point c1; // Corner points |
| 27 | + myutils::point c2; |
| 28 | + |
| 29 | + // Default constructor |
| 30 | + Instruction() {}; |
| 31 | + |
| 32 | + // Copy constructor |
| 33 | + Instruction(const Instruction &b) |
| 34 | + { |
| 35 | + command = b.command; |
| 36 | + c1 = b.c1; |
| 37 | + c2 = b.c2; |
| 38 | + }; |
| 39 | + |
| 40 | +}; |
| 41 | + |
14 | 42 | // Solve puzzle #1
|
15 | 43 | template <typename T>
|
16 | 44 | constexpr int solve_puzzle1(T data)
|
17 | 45 | {
|
| 46 | + vector<Instruction> instructions(0); |
| 47 | + |
| 48 | + for(auto d : data) |
| 49 | + { |
| 50 | + Instruction instr; |
| 51 | + |
| 52 | + cout << d << endl; |
| 53 | + |
| 54 | + size_t last_index = d.find_first_of("0123456789"); |
| 55 | + string result = d.substr(last_index - 1); |
| 56 | + cout << result << endl; |
| 57 | + |
| 58 | + myutils::point c1, c2; // Corner points |
| 59 | + string c_scrap; |
| 60 | + char sep; |
| 61 | + std::stringstream ss(d.substr(last_index - 1)); |
| 62 | + ss >> instr.c1.x >> sep >> instr.c1.y |
| 63 | + >> c_scrap |
| 64 | + >> instr.c2.x >> sep >> instr.c2.y; |
| 65 | + |
| 66 | + if(d.find("turn on") != string::npos) |
| 67 | + { |
| 68 | + instr.command = ON; |
| 69 | + } |
| 70 | + else if(d.find("turn off") != string::npos) |
| 71 | + { |
| 72 | + instr.command = OFF; |
| 73 | + } |
| 74 | + else if(d.find("toggle") != string::npos) |
| 75 | + { |
| 76 | + instr.command = TOGGLE; |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + cout << " Error: bad command : " << d << endl; |
| 81 | + } |
| 82 | + |
| 83 | + instructions.push_back(instr); |
| 84 | + } |
| 85 | + |
| 86 | + bool grid[1000][1000]; |
18 | 87 | return 42;
|
19 | 88 | }
|
20 | 89 |
|
@@ -43,20 +112,21 @@ int main(int argc, char *argv[])
|
43 | 112 | }
|
44 | 113 |
|
45 | 114 | // Reading the data
|
46 |
| - auto data = myutils::read_file<int, std::vector<int> >(filename); |
| 115 | + vector<string> data; |
| 116 | + myutils::read_file(data, filename, true, false); |
47 | 117 |
|
48 | 118 | // --------- Puzzle #1 ---------
|
49 | 119 | // Verify puzzle1 examples
|
50 |
| - const auto example1 = 1; |
51 |
| - assert(solve_puzzle1<vector<int>>({example1}) == 42 && "Error verifying puzzle #1"); |
| 120 | + //const auto example1 = "1"; |
| 121 | + //assert(solve_puzzle1<vector<string>>({example1}) == 42 && "Error verifying puzzle #1"); |
52 | 122 |
|
53 | 123 | // Solve puzzle #1
|
54 | 124 | std::cout << "Answer for puzzle #1: "<< solve_puzzle1(data) << std::endl;
|
55 | 125 |
|
56 | 126 | // --------- Puzzle #2 ---------
|
57 | 127 | // Verify puzzle2 examples
|
58 |
| - const auto example2 = 2; |
59 |
| - assert(solve_puzzle2<vector<int>>({example2}) == 42 && "Error verifying puzzle #2"); |
| 128 | + const auto example2 = "2"; |
| 129 | + assert(solve_puzzle2<vector<string>>({example2}) == 42 && "Error verifying puzzle #2"); |
60 | 130 |
|
61 | 131 | // Solve puzzle #2
|
62 | 132 | std::cout << "Answer for puzzle #2: "<< solve_puzzle2(data) << std::endl;
|
|
0 commit comments