aboutsummaryrefslogtreecommitdiff
path: root/2021/day14
diff options
context:
space:
mode:
Diffstat (limited to '2021/day14')
-rwxr-xr-x2021/day14/day14bin0 -> 178632 bytes
-rw-r--r--2021/day14/day14.cpp87
-rw-r--r--2021/day14/day14_input.txt102
-rw-r--r--2021/day14/day14_input_ez.txt18
-rw-r--r--2021/day14/makefile2
5 files changed, 209 insertions, 0 deletions
diff --git a/2021/day14/day14 b/2021/day14/day14
new file mode 100755
index 0000000..45b2679
--- /dev/null
+++ b/2021/day14/day14
Binary files differ
diff --git a/2021/day14/day14.cpp b/2021/day14/day14.cpp
new file mode 100644
index 0000000..63656b6
--- /dev/null
+++ b/2021/day14/day14.cpp
@@ -0,0 +1,87 @@
+#include <iostream>
+#include <fstream>
+#include <vector>
+#include <string>
+#include <map>
+#include "../utils.h"
+
+std::map<std::string, long> apply (std::map<std::string, long> pairs,
+ std::map<std::string, char> rules, std::map<char, long> &count)
+{
+ std::map<std::string, long> next = pairs;
+ for (auto &pair : pairs) {
+ char c = rules[pair.first];
+ count[c] += pair.second;
+ std::string l;
+ l.push_back(pair.first[0]);
+ l.push_back(c);
+ std::string r;
+ r.push_back(c);
+ r.push_back(pair.first[1]);
+ next[pair.first] -= pair.second;
+ next[l] += pair.second;
+ next[r] += pair.second;
+ }
+ return next;
+}
+
+void to_pairs (std::string init, std::map<std::string, long> &pairs) {
+ for (std::size_t i=0; i<init.size()-1; i++) {
+ std::string seg = init.substr(i, 2);
+ pairs[seg] ++;
+ }
+}
+
+long tally (std::map<char, long> map) {
+ std::vector<long> f;
+ for (auto p : map) {
+ f.push_back(p.second);
+ std::cout << p.first << " " << p.second << std::endl;
+ }
+ std::sort(f.begin(), f.end());
+ return f[f.size()-1] - f[0];
+}
+
+int main (int argc, char *argv[]) {
+ std::string raw;
+ std::getline(std::ifstream(argv[1]), raw, '\0');
+ std::vector<std::string> parts, strrules;
+ split(parts, raw, "\n\n");
+ split(strrules, parts[1], "\n");
+
+ std::string polymer = parts[0];
+ std::cout << "starting polymer: " << polymer << std::endl;
+
+ std::map<std::string, long> pairs;
+ std::map<std::string, char> rules;
+ for (auto strrule : strrules) {
+ if (strrule.size() == 0) continue;
+ std::vector<std::string> rl;
+ split(rl, strrule, " -> ");
+ rules[rl[0]] = rl[1][0];
+ pairs[rl[0]] = 0;
+ }
+
+ to_pairs(polymer, pairs);
+
+ std::map<char, long> count;
+ for (auto pair : pairs) {
+ count[pair.first[0]] = 0;
+ count[pair.first[1]] = 0;
+ }
+
+ for (char c : polymer) {
+ count[c] ++;
+ }
+
+ long iter = 10;
+ if (argc > 2) iter = atoi(argv[2]);
+ for (long i=0; i<iter; i++) {
+ pairs = apply(pairs, rules, count);
+ }
+
+ long res = tally(count);
+ std::cout << "most common - least common: " << res << std::endl;
+
+ return 0;
+}
diff --git a/2021/day14/day14_input.txt b/2021/day14/day14_input.txt
new file mode 100644
index 0000000..55664c0
--- /dev/null
+++ b/2021/day14/day14_input.txt
@@ -0,0 +1,102 @@
+VHCKBFOVCHHKOHBPNCKO
+
+SO -> F
+OP -> V
+NF -> F
+BO -> V
+BH -> S
+VB -> B
+SV -> B
+BK -> S
+KC -> N
+SP -> O
+CP -> O
+VN -> O
+HO -> S
+PC -> B
+CS -> O
+PO -> K
+KF -> B
+BP -> K
+VO -> O
+HB -> N
+PH -> O
+FF -> O
+FB -> K
+CC -> H
+FK -> F
+HV -> P
+CO -> S
+OC -> N
+KV -> V
+SS -> O
+FC -> O
+NP -> B
+OH -> B
+OF -> K
+KB -> K
+BN -> C
+OK -> C
+NC -> O
+NO -> O
+FS -> C
+VP -> K
+KP -> S
+VS -> B
+VV -> N
+NN -> P
+KH -> P
+OB -> H
+HP -> H
+KK -> H
+FH -> F
+KS -> V
+BS -> V
+SN -> H
+CB -> B
+HN -> K
+SB -> O
+OS -> K
+BC -> H
+OV -> N
+PN -> B
+VH -> N
+SK -> C
+PV -> K
+VC -> N
+PF -> S
+NB -> B
+PP -> S
+NS -> F
+PB -> B
+CV -> C
+HK -> P
+PK -> S
+NH -> B
+SH -> V
+KO -> H
+NV -> B
+HH -> V
+FO -> O
+CK -> O
+VK -> F
+HF -> O
+BF -> C
+BV -> P
+KN -> K
+VF -> C
+FN -> V
+ON -> C
+SF -> F
+SC -> C
+OO -> S
+FP -> K
+PS -> C
+NK -> O
+BB -> V
+HC -> H
+FV -> V
+CH -> N
+HS -> V
+CF -> F
+CN -> S
diff --git a/2021/day14/day14_input_ez.txt b/2021/day14/day14_input_ez.txt
new file mode 100644
index 0000000..b5594dd
--- /dev/null
+++ b/2021/day14/day14_input_ez.txt
@@ -0,0 +1,18 @@
+NNCB
+
+CH -> B
+HH -> N
+CB -> H
+NH -> C
+HB -> C
+HC -> B
+HN -> C
+NN -> C
+BH -> H
+NC -> B
+NB -> B
+BN -> B
+BB -> N
+BC -> B
+CC -> N
+CN -> C
diff --git a/2021/day14/makefile b/2021/day14/makefile
new file mode 100644
index 0000000..b1ed8ec
--- /dev/null
+++ b/2021/day14/makefile
@@ -0,0 +1,2 @@
+all day14.cpp:
+ g++ -std=c++20 -o day14 day14.cpp