-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.cpp
101 lines (89 loc) · 2.57 KB
/
day14.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <algorithm>
#include <array>
#include "AOC_Solver.h"
#include <map>
#include <unordered_map>
uint64_t aoc::day14::part_1(std::vector<std::string>& input) {
std::string& polymer = input[0];
std::unordered_map<std::string, uint64_t>parts{};
std::map<std::string, char>rules{};
for (int i = 0; i < polymer.length() - 1; ++i)
{
++parts[polymer.substr(i, 2)];
}
for (int i = 2; i < input.size(); ++i)
{
rules[input[i].substr(0, 2)] = input[i][6];
}
for (int i = 0; i < 10; ++i)
{
std::unordered_map<std::string, uint64_t>temp_parts{};
for (auto [combination, count] : parts)
{
std::string combination_a{ combination[0], rules[combination] };
std::string combination_b{ rules[combination] , combination[1] };
temp_parts[combination_a] += count;
temp_parts[combination_b] += count;
}
std::swap(parts, temp_parts);
}
std::unordered_map<char, uint64_t>letters{};
for (auto [combination, count] : parts)
{
letters[combination[0]] += count;
}
letters[polymer.back()]++;
auto max = std::max_element(letters.begin(), letters.end(), [](auto& a, auto& b)
{
return a.second < b.second;
});
auto min = std::min_element(letters.begin(), letters.end(), [](auto& a, auto& b)
{
if (a.second == 0)return false;
if (b.second == 0)return true;
return a.second < b.second;
});
return (*max).second - (*min).second;
}
uint64_t aoc::day14::part_2(std::vector<std::string>& input) {
std::string& polymer = input[0];
std::unordered_map<std::string, uint64_t>parts{};
std::map<std::string, char>rules{};
for (int i = 0; i < polymer.length() - 1;++i)
{
++parts[polymer.substr(i, 2)];
}
for (int i = 2; i < input.size(); ++i)
{
rules[input[i].substr(0, 2)] = input[i][6];
}
for(int i=0;i<40;++i)
{
std::unordered_map<std::string, uint64_t>temp_parts{};
for(auto[combination,count]:parts)
{
std::string combination_a{ combination[0], rules[combination] };
std::string combination_b{ rules[combination] , combination[1] };
temp_parts[combination_a] += count;
temp_parts[combination_b] += count;
}
std::swap(parts, temp_parts);
}
std::unordered_map<char, uint64_t>letters{};
for (auto [combination, count] : parts)
{
letters[combination[0]]+=count;
}
letters[polymer.back()]++;
auto max = std::max_element(letters.begin(), letters.end(),[](auto& a, auto& b)
{
return a.second < b.second;
});
auto min = std::min_element(letters.begin(), letters.end(), [](auto& a, auto& b)
{
if (a.second == 0)return false;
if (b.second == 0)return true;
return a.second < b.second;
});
return (*max).second -(*min).second;
}