-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #396 from david-ds/skasch/day10
Day 10, C++
- Loading branch information
Showing
3 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
73 | ||
114 | ||
100 | ||
122 | ||
10 | ||
141 | ||
89 | ||
70 | ||
134 | ||
2 | ||
116 | ||
30 | ||
123 | ||
81 | ||
104 | ||
42 | ||
142 | ||
26 | ||
15 | ||
92 | ||
56 | ||
60 | ||
3 | ||
151 | ||
11 | ||
129 | ||
167 | ||
76 | ||
18 | ||
78 | ||
32 | ||
110 | ||
8 | ||
119 | ||
164 | ||
143 | ||
87 | ||
4 | ||
9 | ||
107 | ||
130 | ||
19 | ||
52 | ||
84 | ||
55 | ||
69 | ||
71 | ||
83 | ||
165 | ||
72 | ||
156 | ||
41 | ||
40 | ||
1 | ||
61 | ||
158 | ||
27 | ||
31 | ||
155 | ||
25 | ||
93 | ||
166 | ||
59 | ||
108 | ||
98 | ||
149 | ||
124 | ||
65 | ||
77 | ||
88 | ||
46 | ||
14 | ||
64 | ||
39 | ||
140 | ||
95 | ||
113 | ||
54 | ||
66 | ||
137 | ||
101 | ||
22 | ||
82 | ||
21 | ||
131 | ||
109 | ||
45 | ||
150 | ||
94 | ||
36 | ||
20 | ||
33 | ||
49 | ||
146 | ||
157 | ||
99 | ||
7 | ||
53 | ||
161 | ||
115 | ||
127 | ||
152 | ||
128 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include <algorithm> | ||
#include <ctime> | ||
#include <iostream> | ||
#include <sstream> | ||
#include <string> | ||
#include <vector> | ||
|
||
static constexpr int kMaxAdapters = 128; | ||
|
||
std::string run(const std::string& input) { | ||
// Your code goes here | ||
std::vector<int> adapters; | ||
adapters.reserve(kMaxAdapters); | ||
std::istringstream iss(input); | ||
for (std::string line; std::getline(iss, line);) { | ||
adapters.push_back(std::atoi(line.c_str())); | ||
} | ||
std::sort(adapters.begin(), adapters.end()); | ||
int count_1s = 0; | ||
int count_3s = 1; | ||
switch (adapters[0]) { | ||
case 1: | ||
++count_1s; | ||
break; | ||
case 3: | ||
++count_3s; | ||
break; | ||
default: | ||
break; | ||
} | ||
for (int idx = 1; idx < adapters.size(); ++idx) { | ||
switch (adapters[idx] - adapters[idx - 1]) { | ||
case 1: | ||
++count_1s; | ||
break; | ||
case 3: | ||
++count_3s; | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
return std::to_string(count_1s * count_3s); | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
if (argc < 2) { | ||
std::cout << "Missing one argument" << std::endl; | ||
exit(1); | ||
} | ||
|
||
clock_t start = clock(); | ||
auto answer = run(argv[1]); | ||
|
||
std::cout << "_duration:" << float(clock() - start) * 1000.0 / CLOCKS_PER_SEC | ||
<< "\n"; | ||
std::cout << answer << "\n"; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include <algorithm> | ||
#include <array> | ||
#include <cstdint> | ||
#include <ctime> | ||
#include <iostream> | ||
#include <sstream> | ||
#include <string> | ||
#include <vector> | ||
|
||
static constexpr int kMaxAdapters = 128; | ||
static std::array<std::int64_t, kMaxAdapters> kMem; | ||
|
||
std::int64_t CountArrangements(const std::vector<int>& adapters, int index) { | ||
if (kMem[index] != 0) { | ||
return kMem[index]; | ||
} | ||
int next_index = index + 1; | ||
std::int64_t result = 0; | ||
while (next_index < adapters.size() && | ||
adapters[next_index] <= adapters[index] + 3) { | ||
result += CountArrangements(adapters, next_index); | ||
++next_index; | ||
} | ||
kMem[index] = result; | ||
return result; | ||
} | ||
|
||
std::string run(const std::string& input) { | ||
// Your code goes here | ||
std::vector<int> adapters = {0}; | ||
adapters.reserve(kMaxAdapters); | ||
std::istringstream iss(input); | ||
for (std::string line; std::getline(iss, line);) { | ||
adapters.push_back(std::atoi(line.c_str())); | ||
} | ||
std::sort(adapters.begin(), adapters.end()); | ||
kMem[adapters.size() - 1] = 1; | ||
return std::to_string(CountArrangements(adapters, 0)); | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
if (argc < 2) { | ||
std::cout << "Missing one argument" << std::endl; | ||
exit(1); | ||
} | ||
|
||
clock_t start = clock(); | ||
auto answer = run(argv[1]); | ||
|
||
std::cout << "_duration:" << float(clock() - start) * 1000.0 / CLOCKS_PER_SEC | ||
<< "\n"; | ||
std::cout << answer << "\n"; | ||
return 0; | ||
} |