From d9f64d28158f4c8b67c5799f8c1647cd2ea04b79 Mon Sep 17 00:00:00 2001 From: jacopograndi Date: Sat, 8 Jan 2022 22:29:51 +0000 Subject: day14 --- 2021/day14/day14 | Bin 0 -> 178632 bytes 2021/day14/day14.cpp | 87 +++++++++++++++++++++++++++++++++++ 2021/day14/day14_input.txt | 102 ++++++++++++++++++++++++++++++++++++++++++ 2021/day14/day14_input_ez.txt | 18 ++++++++ 2021/day14/makefile | 2 + 5 files changed, 209 insertions(+) create mode 100755 2021/day14/day14 create mode 100644 2021/day14/day14.cpp create mode 100644 2021/day14/day14_input.txt create mode 100644 2021/day14/day14_input_ez.txt create mode 100644 2021/day14/makefile (limited to '2021/day14') diff --git a/2021/day14/day14 b/2021/day14/day14 new file mode 100755 index 0000000..45b2679 Binary files /dev/null and b/2021/day14/day14 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 +#include +#include +#include +#include +#include "../utils.h" + +std::map apply (std::map pairs, + std::map rules, std::map &count) +{ + std::map 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 &pairs) { + for (std::size_t i=0; i map) { + std::vector 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 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 pairs; + std::map rules; + for (auto strrule : strrules) { + if (strrule.size() == 0) continue; + std::vector rl; + split(rl, strrule, " -> "); + rules[rl[0]] = rl[1][0]; + pairs[rl[0]] = 0; + } + + to_pairs(polymer, pairs); + + std::map 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 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 -- cgit v1.2.3-54-g00ecf