Procházet zdrojové kódy

day 14 w/o rust. but maybe later.

metya před 4 roky
rodič
revize
40a5d2e111

+ 86 - 0
day14_extended_polymerization/README.md

@@ -0,0 +1,86 @@
+--- Day 14: Extended Polymerization ---
+---------------------------------------
+
+The incredible pressures at this depth are starting to put a strain on your submarine. The submarine has [polymerization](https://en.wikipedia.org/wiki/Polymerization) equipment that would produce suitable materials to reinforce the submarine, and the nearby volcanically-active caves should even have the necessary input elements in sufficient quantities.
+
+
+The submarine manual contains instructions for finding the optimal polymer formula; specifically, it offers a *polymer template* and a list of *pair insertion* rules (your puzzle input). You just need to work out what polymer would result after repeating the pair insertion process a few times.
+
+
+For example:
+
+
+
+```
+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
+
+```
+
+The first line is the *polymer template* - this is the starting point of the process.
+
+
+The following section defines the *pair insertion* rules. A rule like `AB -> C` means that when elements `A` and `B` are immediately adjacent, element `C` should be inserted between them. These insertions all happen simultaneously.
+
+
+So, starting with the polymer template `NNCB`, the first step simultaneously considers all three pairs:
+
+
+* The first pair (`NN`) matches the rule `NN -> C`, so element `*C*` is inserted between the first `N` and the second `N`.
+* The second pair (`NC`) matches the rule `NC -> B`, so element `*B*` is inserted between the `N` and the `C`.
+* The third pair (`CB`) matches the rule `CB -> H`, so element `*H*` is inserted between the `C` and the `B`.
+
+
+Note that these pairs overlap: the second element of one pair is the first element of the next pair. Also, because all pairs are considered simultaneously, inserted elements are not considered to be part of a pair until the next step.
+
+
+After the first step of this process, the polymer becomes `N*C*N*B*C*H*B`.
+
+
+Here are the results of a few steps using the above rules:
+
+
+
+```
+Template:     NNCB
+After step 1: NCNBCHB
+After step 2: NBCCNBBBCBHCB
+After step 3: NBBBCNCCNBBNBNBBCHBHHBCHB
+After step 4: NBBNBNBBCCNBCNCCNBBNBBNBBBNBBNBBCBHCBHHNHCBBCBHCB
+
+```
+
+This polymer grows quickly. After step 5, it has length 97; After step 10, it has length 3073. After step 10, `B` occurs 1749 times, `C` occurs 298 times, `H` occurs 161 times, and `N` occurs 865 times; taking the quantity of the most common element (`B`, 1749) and subtracting the quantity of the least common element (`H`, 161) produces `1749 - 161 = *1588*`.
+
+
+Apply 10 steps of pair insertion to the polymer template and find the most and least common elements in the result. *What do you get if you take the quantity of the most common element and subtract the quantity of the least common element?*
+
+
+--- Part Two ---
+----------------
+
+The resulting polymer isn't nearly strong enough to reinforce the submarine. You'll need to run more steps of the pair insertion process; a total of *40 steps* should do it.
+
+
+In the above example, the most common element is `B` (occurring `2192039569602` times) and the least common element is `H` (occurring `3849876073` times); subtracting these produces `*2188189693529*`.
+
+
+Apply *40* steps of pair insertion to the polymer template and find the most and least common elements in the result. *What do you get if you take the quantity of the most common element and subtract the quantity of the least common element?*
+
+

+ 18 - 0
day14_extended_polymerization/example.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

+ 102 - 0
day14_extended_polymerization/input.txt

@@ -0,0 +1,102 @@
+PKHOVVOSCNVHHCVVCBOH
+
+NO -> B
+PV -> P
+OC -> K
+SC -> K
+FK -> P
+PO -> P
+FC -> V
+KN -> V
+CN -> O
+CB -> K
+NF -> K
+CO -> F
+SK -> F
+VO -> B
+SF -> F
+PB -> F
+FF -> C
+HC -> P
+PF -> B
+OP -> B
+OO -> V
+OK -> N
+KB -> H
+PN -> V
+PP -> N
+FV -> S
+BO -> O
+HN -> C
+FP -> F
+BP -> B
+HB -> N
+VC -> F
+PC -> V
+FO -> O
+OH -> S
+FH -> B
+HK -> B
+BC -> F
+ON -> K
+FN -> N
+NN -> O
+PH -> P
+KS -> H
+HV -> F
+BK -> O
+NP -> S
+CC -> H
+KV -> V
+NB -> C
+NS -> S
+KO -> V
+NK -> H
+HO -> C
+KC -> P
+VH -> C
+VK -> O
+CP -> K
+BS -> N
+BB -> F
+VV -> K
+SH -> O
+SO -> N
+VF -> K
+NV -> K
+SV -> O
+NH -> C
+VS -> N
+OF -> N
+SP -> C
+HP -> O
+NC -> V
+KP -> B
+KH -> O
+SN -> S
+CS -> N
+FB -> P
+OB -> H
+VP -> B
+CH -> O
+BF -> B
+PK -> S
+CF -> V
+CV -> S
+VB -> P
+CK -> H
+PS -> N
+SS -> C
+OS -> P
+OV -> F
+VN -> V
+BV -> V
+HF -> B
+FS -> O
+BN -> K
+SB -> N
+HH -> S
+BH -> S
+KK -> H
+HS -> K
+KF -> V

+ 54 - 0
day14_extended_polymerization/main.py

@@ -0,0 +1,54 @@
+from doctest import Example
+import os, sys
+from tabnanny import check
+
+from numpy import poly
+
+task_dir = os.path.dirname(__file__)
+sys.path.append(f"{task_dir}/..")
+from get_tasks import get_input, check_example, generate_readme
+from collections import Counter, defaultdict
+
+
+def parse(input: list[str]) -> tuple[str, dict]:
+    template = input[0]
+    instructions = {}
+    for line in input[2:]:
+        key, value = line.split(" -> ")
+        instructions[(key[0], key[1])] = value
+    return template, instructions
+
+
+def polymerize(steps: int, template: str, instructions: dict) -> int:
+    counter = Counter(template)
+    combinations: defaultdict[tuple, int] = defaultdict(int)
+    for i in range(len(template) - 1):
+        combinations[(template[i], template[i + 1])] += 1
+    for _ in range(steps):
+        temp_combinations = combinations.copy()
+        for comb in temp_combinations:
+            counter[instructions[comb]] += temp_combinations[comb]
+            combinations[comb] -= temp_combinations[comb]
+            combinations[(comb[0], instructions[comb])] += temp_combinations[comb]
+            combinations[(instructions[comb], comb[1])] += temp_combinations[comb]
+    return max(counter.values()) - min(counter.values())
+
+
+def part1(input: list[str]):
+    print("The answer of part1 is:", polymerize(10, *parse(input)))
+
+
+def part2(input: list[str]):
+    print("The answer if part2 is:", polymerize(40, *parse(input)))
+
+
+if __name__ == "__main__":
+    input, example = get_input(task_dir, 14)
+
+    check_example(example, part1)
+    check_example(example, part2)
+
+    part1(input)
+    part2(input)
+
+    generate_readme(task_dir, 14)

+ 2 - 0
get_tasks.py

@@ -36,6 +36,8 @@ def get_input(task_dir: str, day: int) -> tuple[list[str], list[str]]:
         "session": "53616c7465645f5ffe3db8d154199da4d6e4e569142fda21d3350f5e550f2a4c509bd1b147264ffe0a0d2124909ec5d6"
     }
 
+    os.makedirs(task_dir, exist_ok=True)
+
     if os.path.exists(input_path):
         with open(input_path, "r") as f:
             input = f.read().splitlines()