Ver código fonte

day 18 and day 20. eto pizdec

metya 4 anos atrás
pai
commit
841a471edd

+ 264 - 0
day18_snailfish/README.md

@@ -0,0 +1,264 @@
+--- Day 18: Snailfish ---
+-------------------------
+
+You descend into the ocean trench and encounter some [snailfish](https://en.wikipedia.org/wiki/Snailfish). They say they saw the sleigh keys! They'll even tell you which direction the keys went if you help one of the smaller snailfish with his *math homework*.
+
+
+Snailfish numbers aren't like regular numbers. Instead, every snailfish number is a *pair* - an ordered list of two elements. Each element of the pair can be either a regular number or another pair.
+
+
+Pairs are written as `[x,y]`, where `x` and `y` are the elements within the pair. Here are some example snailfish numbers, one snailfish number per line:
+
+
+
+```
+[1,2]
+[[1,2],3]
+[9,[8,7]]
+[[1,9],[8,5]]
+[[[[1,2],[3,4]],[[5,6],[7,8]]],9]
+[[[9,[3,8]],[[0,9],6]],[[[3,7],[4,9]],3]]
+[[[[1,3],[5,3]],[[1,3],[8,7]]],[[[4,9],[6,9]],[[8,2],[7,3]]]]
+
+```
+
+This snailfish homework is about *addition*. To add two snailfish numbers, form a pair from the left and right parameters of the addition operator. For example, `[1,2]` + `[[3,4],5]` becomes `[[1,2],[[3,4],5]]`.
+
+
+There's only one problem: *snailfish numbers must always be reduced*, and the process of adding two snailfish numbers can result in snailfish numbers that need to be reduced.
+
+
+To *reduce a snailfish number*, you must repeatedly do the first action in this list that applies to the snailfish number:
+
+
+* If any pair is *nested inside four pairs*, the leftmost such pair *explodes*.
+* If any regular number is *10 or greater*, the leftmost such regular number *splits*.
+
+
+Once no action in the above list applies, the snailfish number is reduced.
+
+
+During reduction, at most one action applies, after which the process returns to the top of the list of actions. For example, if *split* produces a pair that meets the *explode* criteria, that pair *explodes* before other *splits* occur.
+
+
+To *explode* a pair, the pair's left value is added to the first regular number to the left of the exploding pair (if any), and the pair's right value is added to the first regular number to the right of the exploding pair (if any). Exploding pairs will always consist of two regular numbers. Then, the entire exploding pair is replaced with the regular number `0`.
+
+
+Here are some examples of a single explode action:
+
+
+* `[[[[*[9,8]*,1],2],3],4]` becomes `[[[[*0*,*9*],2],3],4]` (the `9` has no regular number to its left, so it is not added to any regular number).
+* `[7,[6,[5,[4,*[3,2]*]]]]` becomes `[7,[6,[5,[*7*,*0*]]]]` (the `2` has no regular number to its right, and so it is not added to any regular number).
+* `[[6,[5,[4,*[3,2]*]]],1]` becomes `[[6,[5,[*7*,*0*]]],*3*]`.
+* `[[3,[2,[1,*[7,3]*]]],[6,[5,[4,[3,2]]]]]` becomes `[[3,[2,[*8*,*0*]]],[*9*,[5,[4,[3,2]]]]]` (the pair `[3,2]` is unaffected because the pair `[7,3]` is further to the left; `[3,2]` would explode on the next action).
+* `[[3,[2,[8,0]]],[9,[5,[4,*[3,2]*]]]]` becomes `[[3,[2,[8,0]]],[9,[5,[*7*,*0*]]]]`.
+
+
+To *split* a regular number, replace it with a pair; the left element of the pair should be the regular number divided by two and rounded *down*, while the right element of the pair should be the regular number divided by two and rounded *up*. For example, `10` becomes `[5,5]`, `11` becomes `[5,6]`, `12` becomes `[6,6]`, and so on.
+
+
+Here is the process of finding the reduced result of `[[[[4,3],4],4],[7,[[8,4],9]]]` + `[1,1]`:
+
+
+
+```
+after addition: [[[[*[4,3]*,4],4],[7,[[8,4],9]]],[1,1]]
+after explode:  [[[[0,7],4],[7,[*[8,4]*,9]]],[1,1]]
+after explode:  [[[[0,7],4],[*15*,[0,13]]],[1,1]]
+after split:    [[[[0,7],4],[[7,8],[0,*13*]]],[1,1]]
+after split:    [[[[0,7],4],[[7,8],[0,*[6,7]*]]],[1,1]]
+after explode:  [[[[0,7],4],[[7,8],[6,0]]],[8,1]]
+
+```
+
+Once no reduce actions apply, the snailfish number that remains is the actual result of the addition operation: `[[[[0,7],4],[[7,8],[6,0]]],[8,1]]`.
+
+
+The homework assignment involves adding up a *list of snailfish numbers* (your puzzle input). The snailfish numbers are each listed on a separate line. Add the first snailfish number and the second, then add that result and the third, then add that result and the fourth, and so on until all numbers in the list have been used once.
+
+
+For example, the final sum of this list is `[[[[1,1],[2,2]],[3,3]],[4,4]]`:
+
+
+
+```
+[1,1]
+[2,2]
+[3,3]
+[4,4]
+
+```
+
+The final sum of this list is `[[[[3,0],[5,3]],[4,4]],[5,5]]`:
+
+
+
+```
+[1,1]
+[2,2]
+[3,3]
+[4,4]
+[5,5]
+
+```
+
+The final sum of this list is `[[[[5,0],[7,4]],[5,5]],[6,6]]`:
+
+
+
+```
+[1,1]
+[2,2]
+[3,3]
+[4,4]
+[5,5]
+[6,6]
+
+```
+
+Here's a slightly larger example:
+
+
+
+```
+[[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]
+[7,[[[3,7],[4,3]],[[6,3],[8,8]]]]
+[[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]]
+[[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]]
+[7,[5,[[3,8],[1,4]]]]
+[[2,[2,2]],[8,[8,1]]]
+[2,9]
+[1,[[[9,3],9],[[9,0],[0,7]]]]
+[[[5,[7,4]],7],1]
+[[[[4,2],2],6],[8,7]]
+
+```
+
+The final sum `[[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]` is found after adding up the above snailfish numbers:
+
+
+
+```
+  [[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]
++ [7,[[[3,7],[4,3]],[[6,3],[8,8]]]]
+= [[[[4,0],[5,4]],[[7,7],[6,0]]],[[8,[7,7]],[[7,9],[5,0]]]]
+
+  [[[[4,0],[5,4]],[[7,7],[6,0]]],[[8,[7,7]],[[7,9],[5,0]]]]
++ [[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]]
+= [[[[6,7],[6,7]],[[7,7],[0,7]]],[[[8,7],[7,7]],[[8,8],[8,0]]]]
+
+  [[[[6,7],[6,7]],[[7,7],[0,7]]],[[[8,7],[7,7]],[[8,8],[8,0]]]]
++ [[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]]
+= [[[[7,0],[7,7]],[[7,7],[7,8]]],[[[7,7],[8,8]],[[7,7],[8,7]]]]
+
+  [[[[7,0],[7,7]],[[7,7],[7,8]]],[[[7,7],[8,8]],[[7,7],[8,7]]]]
++ [7,[5,[[3,8],[1,4]]]]
+= [[[[7,7],[7,8]],[[9,5],[8,7]]],[[[6,8],[0,8]],[[9,9],[9,0]]]]
+
+  [[[[7,7],[7,8]],[[9,5],[8,7]]],[[[6,8],[0,8]],[[9,9],[9,0]]]]
++ [[2,[2,2]],[8,[8,1]]]
+= [[[[6,6],[6,6]],[[6,0],[6,7]]],[[[7,7],[8,9]],[8,[8,1]]]]
+
+  [[[[6,6],[6,6]],[[6,0],[6,7]]],[[[7,7],[8,9]],[8,[8,1]]]]
++ [2,9]
+= [[[[6,6],[7,7]],[[0,7],[7,7]]],[[[5,5],[5,6]],9]]
+
+  [[[[6,6],[7,7]],[[0,7],[7,7]]],[[[5,5],[5,6]],9]]
++ [1,[[[9,3],9],[[9,0],[0,7]]]]
+= [[[[7,8],[6,7]],[[6,8],[0,8]]],[[[7,7],[5,0]],[[5,5],[5,6]]]]
+
+  [[[[7,8],[6,7]],[[6,8],[0,8]]],[[[7,7],[5,0]],[[5,5],[5,6]]]]
++ [[[5,[7,4]],7],1]
+= [[[[7,7],[7,7]],[[8,7],[8,7]]],[[[7,0],[7,7]],9]]
+
+  [[[[7,7],[7,7]],[[8,7],[8,7]]],[[[7,0],[7,7]],9]]
++ [[[[4,2],2],6],[8,7]]
+= [[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]
+
+```
+
+To check whether it's the right answer, the snailfish teacher only checks the *magnitude* of the final sum. The magnitude of a pair is 3 times the magnitude of its left element plus 2 times the magnitude of its right element. The magnitude of a regular number is just that number.
+
+
+For example, the magnitude of `[9,1]` is `3*9 + 2*1 = *29*`; the magnitude of `[1,9]` is `3*1 + 2*9 = *21*`. Magnitude calculations are recursive: the magnitude of `[[9,1],[1,9]]` is `3*29 + 2*21 = *129*`.
+
+
+Here are a few more magnitude examples:
+
+
+* `[[1,2],[[3,4],5]]` becomes `*143*`.
+* `[[[[0,7],4],[[7,8],[6,0]]],[8,1]]` becomes `*1384*`.
+* `[[[[1,1],[2,2]],[3,3]],[4,4]]` becomes `*445*`.
+* `[[[[3,0],[5,3]],[4,4]],[5,5]]` becomes `*791*`.
+* `[[[[5,0],[7,4]],[5,5]],[6,6]]` becomes `*1137*`.
+* `[[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]` becomes `*3488*`.
+
+
+So, given this example homework assignment:
+
+
+
+```
+[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]
+[[[5,[2,8]],4],[5,[[9,9],0]]]
+[6,[[[6,2],[5,6]],[[7,6],[4,7]]]]
+[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]]
+[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]]
+[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]]
+[[[[5,4],[7,7]],8],[[8,3],8]]
+[[9,3],[[9,9],[6,[4,9]]]]
+[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]
+[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]]
+
+```
+
+The final sum is:
+
+
+
+```
+[[[[6,6],[7,6]],[[7,7],[7,0]]],[[[7,7],[7,7]],[[7,8],[9,9]]]]
+```
+
+The magnitude of this final sum is `*4140*`.
+
+
+Add up all of the snailfish numbers from the homework assignment in the order they appear. *What is the magnitude of the final sum?*
+
+
+--- Part Two ---
+----------------
+
+You notice a second question on the back of the homework assignment:
+
+
+What is the largest magnitude you can get from adding only two of the snailfish numbers?
+
+
+Note that snailfish addition is not [commutative](https://en.wikipedia.org/wiki/Commutative_property) - that is, `x + y` and `y + x` can produce different results.
+
+
+Again considering the last example homework assignment above:
+
+
+
+```
+[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]
+[[[5,[2,8]],4],[5,[[9,9],0]]]
+[6,[[[6,2],[5,6]],[[7,6],[4,7]]]]
+[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]]
+[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]]
+[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]]
+[[[[5,4],[7,7]],8],[[8,3],8]]
+[[9,3],[[9,9],[6,[4,9]]]]
+[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]
+[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]]
+
+```
+
+The largest magnitude of the sum of any two snailfish numbers in this list is `*3993*`. This is the magnitude of `[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]` + `[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]`, which reduces to `[[[[7,8],[6,6]],[[6,0],[7,7]]],[[[7,8],[8,8]],[[7,9],[0,6]]]]`.
+
+
+*What is the largest magnitude of any sum of two different snailfish numbers from the homework assignment?*
+
+

+ 10 - 0
day18_snailfish/example.txt

@@ -0,0 +1,10 @@
+[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]
+[[[5,[2,8]],4],[5,[[9,9],0]]]
+[6,[[[6,2],[5,6]],[[7,6],[4,7]]]]
+[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]]
+[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]]
+[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]]
+[[[[5,4],[7,7]],8],[[8,3],8]]
+[[9,3],[[9,9],[6,[4,9]]]]
+[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]
+[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]]

+ 100 - 0
day18_snailfish/input.txt

@@ -0,0 +1,100 @@
+[[0,6],[[[4,0],[6,6]],[[2,2],9]]]
+[[9,[[1,6],[6,0]]],[[1,[0,8]],[[0,8],[9,8]]]]
+[[[0,[2,1]],3],[[[2,4],[1,2]],[7,5]]]
+[[[[8,3],[8,5]],[[7,8],[5,5]]],[9,2]]
+[[8,[1,9]],[[[9,9],[9,2]],1]]
+[[[[3,7],[2,1]],[0,9]],4]
+[[[[3,8],[6,0]],[0,7]],[[[6,3],[2,0]],9]]
+[[[9,[7,0]],[8,[9,6]]],[[5,6],4]]
+[[[[3,6],[3,6]],[0,2]],[[[8,3],9],[[3,4],8]]]
+[[7,[8,4]],1]
+[6,[[3,[5,6]],[0,6]]]
+[[[7,[4,7]],[[4,5],[4,3]]],[[5,5],[0,[4,2]]]]
+[[[0,[2,9]],[[2,4],[4,8]]],[[8,[9,5]],[[9,6],0]]]
+[[[[2,0],[9,7]],[[3,2],0]],[7,7]]
+[[5,[2,1]],[[3,[5,1]],[[8,5],[1,8]]]]
+[[[[9,7],6],[[7,8],7]],[[[6,8],9],[[9,5],7]]]
+[[4,2],[[[0,1],[7,2]],[[0,2],[5,5]]]]
+[[1,8],[[5,[7,9]],[[3,1],[7,1]]]]
+[[[4,[4,6]],6],5]
+[[[5,[3,6]],6],[[[8,0],[8,6]],[[3,3],[0,1]]]]
+[[4,[[2,6],[0,9]]],[[0,6],[4,2]]]
+[[[[9,4],[6,5]],7],[[[1,5],[0,9]],[4,[4,2]]]]
+[[7,[[6,5],8]],[[[5,6],0],[6,[3,5]]]]
+[[[5,[6,4]],[8,[0,4]]],[[3,[9,3]],4]]
+[[[[4,0],6],[6,[6,5]]],[[9,[6,3]],[[9,6],7]]]
+[[[[2,2],4],[8,[7,2]]],[2,1]]
+[5,[9,[[5,9],4]]]
+[[[1,[7,7]],[[2,2],8]],[[[9,7],5],[4,3]]]
+[[[[6,8],1],1],[1,[[2,0],6]]]
+[[[[0,5],8],[[8,9],[9,3]]],[[[5,5],[4,2]],2]]
+[[[9,[2,5]],[6,[1,7]]],[5,[3,[2,2]]]]
+[[[7,6],8],[[[1,9],3],[5,2]]]
+[8,[[2,[0,7]],8]]
+[[[[8,1],[0,0]],5],1]
+[[1,[[4,8],0]],[[9,[7,8]],5]]
+[[[[1,3],1],[[9,8],[6,6]]],5]
+[[[3,2],[[0,5],[0,1]]],[[9,[9,3]],[4,9]]]
+[[[0,[2,4]],[[3,3],[6,5]]],[[1,[2,1]],[[3,4],9]]]
+[[2,[3,[7,6]]],[5,5]]
+[[[[8,2],0],[[9,6],[9,0]]],[[[6,2],[5,0]],9]]
+[7,[9,7]]
+[3,[[[5,5],1],[8,5]]]
+[[[5,5],[5,6]],[9,5]]
+[[[9,7],[1,2]],[8,[5,[7,0]]]]
+[[[1,[5,2]],[7,[8,9]]],[2,[[4,5],[2,3]]]]
+[[[4,[2,2]],[5,[4,7]]],[[[0,3],2],[5,[2,6]]]]
+[[0,[[6,5],5]],[[7,[7,2]],3]]
+[[[4,[9,4]],[1,9]],[7,[[7,1],[6,1]]]]
+[1,[0,2]]
+[[[[5,1],[2,1]],[[7,8],6]],[[3,[4,9]],2]]
+[[9,[[4,0],[8,8]]],[[[6,6],[2,8]],[1,[1,5]]]]
+[[[1,2],[7,0]],[7,[[3,0],5]]]
+[[[6,[0,8]],3],[[3,7],1]]
+[[[[6,1],[1,0]],9],[[4,8],[3,[0,8]]]]
+[[6,[3,[5,8]]],9]
+[[[[5,0],[7,7]],[[3,1],[4,8]]],5]
+[[[3,7],[9,0]],[[[0,2],7],0]]
+[8,9]
+[[8,[[0,8],4]],[1,[[4,6],2]]]
+[[[5,5],3],[[6,6],[0,[6,3]]]]
+[[[7,[3,7]],[[6,1],[9,4]]],[[[8,9],1],[[8,7],6]]]
+[[6,[[0,9],[2,3]]],[[1,[5,3]],[8,4]]]
+[[[3,5],8],[[[2,4],[7,5]],5]]
+[[0,[[7,0],[9,4]]],[[[0,0],[6,7]],[6,5]]]
+[[[[1,9],[6,4]],0],[6,[3,[4,8]]]]
+[[[[1,6],[0,4]],8],[[8,8],6]]
+[[[[7,4],[9,6]],7],[[1,6],[1,0]]]
+[1,[[[6,8],5],5]]
+[8,4]
+[9,[[9,[3,9]],0]]
+[5,[[[4,9],7],[[1,0],0]]]
+[[[6,1],[0,[2,3]]],[[[7,8],[5,9]],9]]
+[3,[[3,[3,4]],[6,[7,8]]]]
+[[[7,[7,1]],[4,[2,0]]],[6,[7,3]]]
+[[6,9],[[3,[4,7]],3]]
+[1,[[9,[5,1]],[7,[7,5]]]]
+[[3,2],[[9,[6,8]],[[1,0],2]]]
+[[[[3,2],8],[7,6]],9]
+[[3,[[9,5],6]],[5,9]]
+[[[3,[6,3]],[[7,0],[5,7]]],[[3,3],[[4,9],[4,8]]]]
+[[[0,[4,3]],2],[3,[0,[1,3]]]]
+[[[7,[3,4]],[7,[3,1]]],[[0,[4,7]],6]]
+[[[1,[7,4]],[[8,7],3]],4]
+[[[5,5],[[0,3],2]],[1,[[9,4],6]]]
+[[[[6,0],[8,8]],[6,[6,0]]],[5,6]]
+[[[1,[5,4]],[[5,9],[1,7]]],[[5,[4,7]],[4,[4,4]]]]
+[[0,[[2,6],0]],[[6,[4,3]],5]]
+[[[1,[5,3]],[9,[1,2]]],[[[4,8],[5,6]],0]]
+[[0,7],[1,[7,7]]]
+[4,[[7,[7,2]],[[9,1],7]]]
+[2,[[1,6],[6,9]]]
+[[[4,[4,5]],9],[[[1,7],6],[3,[7,3]]]]
+[[6,[[1,1],[7,8]]],[[[5,2],[8,1]],5]]
+[[[5,5],[[4,1],[1,2]]],[[3,8],[3,4]]]
+[[[[1,9],[0,3]],[4,[0,9]]],4]
+[[[4,9],0],[[9,0],[8,[7,5]]]]
+[[6,[5,3]],[[[6,6],4],[[6,8],4]]]
+[[[[1,1],2],1],[1,[[6,4],2]]]
+[[[[6,3],[1,5]],[6,[7,7]]],[6,6]]
+[[[[3,0],[5,6]],1],[[[9,3],[1,7]],[[3,4],[2,7]]]]

+ 91 - 0
day18_snailfish/main.py

@@ -0,0 +1,91 @@
+import os, sys, re
+
+task_dir = os.path.dirname(__file__)
+sys.path.append(f"{task_dir}/..")
+from get_tasks import get_input, generate_readme, check_example, bench
+from math import floor, ceil
+
+
+def explodes(strl, v=False):
+    for pair in re.finditer(r"\[(\d+),(\d+)\]", strl):
+        sd, ed = pair.start(), pair.end()
+        if strl[:sd].count("[") - strl[:sd].count("]") >= 4:
+            p1, p2 = int(pair.group(1)), int(pair.group(2))
+            numreplace = list(re.finditer(r"\d+", strl[:sd]))[::-1]
+            if numreplace:
+                s, e, v = (
+                    numreplace[0].start(),
+                    numreplace[0].end(),
+                    int(numreplace[0][0]),
+                )
+                before = strl[:s] + str(p1 + v) + strl[e:sd]
+            else:
+                before = strl[:sd]
+            numreplace = list(re.finditer(r"\d+", strl[ed:]))
+            if numreplace:
+                s, e, v = (
+                    numreplace[0].start(),
+                    numreplace[0].end(),
+                    int(numreplace[0][0]),
+                )
+                after = strl[ed : ed + s] + str(p2 + v) + strl[ed + e :]
+            else:
+                after = strl[ed:]
+            return before + "0" + after, True
+    return strl, False
+
+
+def split(strl):
+    for num in re.finditer(r"\d{2}", strl):
+        s, e = num.start(), num.end()
+        d = int(num[0])
+        ls = floor(d / 2)
+        rs = ceil(d / 2)
+        return strl[:s] + f"[{ls},{rs}]" + strl[e:], True
+    return strl, False
+
+
+def addition(l):
+    e = True
+    while e:
+        l, e = explodes(l)
+        if not e:
+            l, e = split(l)
+    return l
+
+
+def magnitude(line):
+    while pairs := list(re.finditer(r"\[(\d+),(\d+)\]", line)):
+        for pair in pairs[::-1]:
+            sd, ed = pair.start(), pair.end()
+            p1, p2 = int(pair[1]), int(pair[2])
+            line = line[:sd] + str(p1 * 3 + p2 * 2) + line[ed:]
+    return int(line)
+
+@bench
+def part1(input):
+    res = []
+    for line in input:
+        res.append(line)
+        if len(res) > 1:
+            res.append(addition(f"[{res.pop(0)},{res.pop(0)}]"))
+    print(magnitude(res[0]))
+
+@bench
+def part2(input):
+    mag = []
+    for ind, line in enumerate(input):
+        for ind2, line2 in enumerate(input):
+            if ind == ind2:
+                continue
+            mag.append(magnitude(addition(f"[{line},{line2}]")))
+    print(max(mag))
+
+
+if __name__ == "__main__":
+    input, example = get_input(task_dir, 18)
+
+    part1(input)
+    part2(input)
+
+    generate_readme(task_dir, 18)

+ 159 - 0
day20_trench_map/README.md

@@ -0,0 +1,159 @@
+--- Day 20: Trench Map ---
+--------------------------
+
+With the scanners fully deployed, you turn their attention to mapping the floor of the ocean trench.
+
+
+When you get back the image from the scanners, it seems to just be random noise. Perhaps you can combine an image enhancement algorithm and the input image (your puzzle input) to clean it up a little.
+
+
+For example:
+
+
+
+```
+..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..##
+#..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###
+.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#.
+.#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#.....
+.#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#..
+...####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.....
+..##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#
+
+#..#.
+#....
+##..#
+..#..
+..###
+
+```
+
+The first section is the *image enhancement algorithm*. It is normally given on a single line, but it has been wrapped to multiple lines in this example for legibility. The second section is the *input image*, a two-dimensional grid of *light pixels* (`#`) and *dark pixels* (`.`).
+
+
+The image enhancement algorithm describes how to enhance an image by *simultaneously* converting all pixels in the input image into an output image. Each pixel of the output image is determined by looking at a 3x3 square of pixels centered on the corresponding input image pixel. So, to determine the value of the pixel at (5,10) in the output image, nine pixels from the input image need to be considered: (4,9), (4,10), (4,11), (5,9), (5,10), (5,11), (6,9), (6,10), and (6,11). These nine input pixels are combined into a single binary number that is used as an index in the *image enhancement algorithm* string.
+
+
+For example, to determine the output pixel that corresponds to the very middle pixel of the input image, the nine pixels marked by `[...]` would need to be considered:
+
+
+
+```
+# . . # .
+#[. . .].
+#[# . .]#
+.[. # .].
+. . # # #
+
+```
+
+Starting from the top-left and reading across each row, these pixels are `...`, then `#..`, then `.#.`; combining these forms `...#...#.`. By turning dark pixels (`.`) into `0` and light pixels (`#`) into `1`, the binary number `000100010` can be formed, which is `34` in decimal.
+
+
+The image enhancement algorithm string is exactly 512 characters long, enough to match every possible 9-bit binary number. The first few characters of the string (numbered starting from zero) are as follows:
+
+
+
+```
+0         10        20        30  *34*    40        50        60        70
+|         |         |         |   *|*     |         |         |         |
+..#.#..#####.#.#.#.###.##.....###.*#*#.#..###.####..#####..#....#..#..##..##
+
+```
+
+In the middle of this first group of characters, the character at index 34 can be found: `#`. So, the output pixel in the center of the output image should be `#`, a *light pixel*.
+
+
+This process can then be repeated to calculate every pixel of the output image.
+
+
+Through advances in imaging technology, the images being operated on here are *infinite* in size. *Every* pixel of the infinite output image needs to be calculated exactly based on the relevant pixels of the input image. The small input image you have is only a small region of the actual infinite input image; the rest of the input image consists of dark pixels (`.`). For the purposes of the example, to save on space, only a portion of the infinite-sized input and output images will be shown.
+
+
+The starting input image, therefore, looks something like this, with more dark pixels (`.`) extending forever in every direction not shown here:
+
+
+
+```
+...............
+...............
+...............
+...............
+...............
+.....#..#......
+.....#.........
+.....##..#.....
+.......#.......
+.......###.....
+...............
+...............
+...............
+...............
+...............
+
+```
+
+By applying the image enhancement algorithm to every pixel simultaneously, the following output image can be obtained:
+
+
+
+```
+...............
+...............
+...............
+...............
+.....##.##.....
+....#..#.#.....
+....##.#..#....
+....####..#....
+.....#..##.....
+......##..#....
+.......#.#.....
+...............
+...............
+...............
+...............
+
+```
+
+Through further advances in imaging technology, the above output image can also be used as an input image! This allows it to be enhanced *a second time*:
+
+
+
+```
+...............
+...............
+...............
+..........#....
+....#..#.#.....
+...#.#...###...
+...#...##.#....
+...#.....#.#...
+....#.#####....
+.....#.#####...
+......##.##....
+.......###.....
+...............
+...............
+...............
+
+```
+
+Truly incredible - now the small details are really starting to come through. After enhancing the original input image twice, `*35*` pixels are lit.
+
+
+Start with the original input image and apply the image enhancement algorithm twice, being careful to account for the infinite size of the images. *How many pixels are lit in the resulting image?*
+
+
+--- Part Two ---
+----------------
+
+You still can't quite make out the details in the image. Maybe you just didn't [enhance](https://en.wikipedia.org/wiki/Kernel_(image_processing)) it enough.
+
+
+If you enhance the starting input image in the above example a total of *50* times, `*3351*` pixels are lit in the final output image.
+
+
+Start again with the original input image and apply the image enhancement algorithm 50 times. *How many pixels are lit in the resulting image?*
+
+

+ 13 - 0
day20_trench_map/example.txt

@@ -0,0 +1,13 @@
+..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..##
+#..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###
+.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#.
+.#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#.....
+.#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#..
+...####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.....
+..##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#
+
+#..#.
+#....
+##..#
+..#..
+..###

+ 102 - 0
day20_trench_map/input.txt

@@ -0,0 +1,102 @@
+##..#.####..#.#.##.#######.######.#####..#...##.###....#..#.##..#....#.#.##.#.#.#...###..###..#.#..#.#####.#.#.##..##.#.###.#..##.###.###...####.#####....#....#..#.#...#.#.#.#.#...#####...##..####.##.##...#.##.#####........#..#####..##.##..###########....#.##.#####.##..#####...#..#.#.##...#.#.#..#...####......#.#.####....##.#.##.####..##..##.#.......#...#.###########.....###.#######....##.#.#######..#.......###.##....#.#..#....##..#.###.#..#.###.###.##..##.#.#...#.#.....###.#.#....###..#.....##.#.#####.....
+
+#.#.#.###...#####.#.###...#.#..##.#.#..#.#.#####.##......##..#.....#.#..#.##.#..##.##.#.#.#.##...#..
+.#.##..#.##.#..#...##..##....##....###.##.#..##..............#####.#.###..#..####....##.##.#.#....#.
+.##.##.###.##.#...#.#..#.###.##.####..#....#.#.####..##.####.###....##..###.#.#.....#..##.##.##.#...
+..#.##.####.####..##..####.##.###..#.#..#..##.#..#..####.#.....####.#..#####...###.##...#.##...###..
+###..##.#.#######.##....#.#.###..###..#..#####..#....##.##.###....#..#####.#..##.#...######.##..#.#.
+....#.#.##..##.####..###..#####.#.#.##....#..#...##########..##.#...#..#.##.#####.#.###..#.#..######
+.#....#.........##...#####.##.......#.###.#.###..###.#..##.###...####...........#..######...##..####
+...#.#..###.###.#.#.##.#.###.#.######.#####.#...#.#...##..###.##.####.###..#..#.####.####..#.##.#.##
+.#.#..####..#.#.#.##.......##..#..##..#..#.#.#..##..#..##.#.#.##..###....###.###.###..#....#.#..#.##
+#......###.##.#..###.#.#.###.#...#..#.#.##.##...##..####..##.###..#...######...####..##.#####......#
+.#...###.#.##.##...##......##..#.....###.###.#.#.####.###...#...#...#.##..#.##.###.#.#..#.....#####.
+.##..#..#.#..#..#.##..#..###..##.##....#.#....#.####.##.###.#...###.#..###..###..#......##..#...##.#
+..##..#..#..#..##.##...###.#.##.....#..###..##...##......#.#.#..#..#..#.#...#...#.........#.##.#...#
+#.###.#..##..##...#.#...#......###.....##.######..#....#...##.###..#..####...##......#.##...##.##.#.
+####....#.#..##.#.#.....##.##.#....#.##...###.#.#.#..##...#####.##.#.......##.#..#.##.....###.#.#..#
+...#.#...#.......#.##.#...#####...##.##.#....###.......##...#.##.#.##...#.#.....#.#####.###.#####.#.
+.###..#..##..####.##.#.###.###.###..#...###.#######...#...#.###..###..#..###.....##..#.###......##..
+.#####.##.#..##....#.########..####.##...###..#.###..#..#.#####...#...##.#.##.#.....##.#..#####...##
+...###......##.#.#.##...#....#.##..#.##.##..##.#.#.#####....#..##....##..##.#.##.....#..#.#..####..#
+.#..###....#....##.#..#.##...#.#..#.#####.###.#.#..#.#..#.##.#....##.#.##...#####.#####...#...##.#..
+#.#.#...#....#.#..#.##.#....#.#.###..##.##..#.#.#.....######.....#.#..#.###.#..##.##.#.###..#.###...
+..#.#.#.#......###..##.#.##.##.###...###..#..####...#.#.##.###.#..#.#.#...##..##..##..#..####.##..##
+...#.#.#...#..######.##.##.##...#..#..#..#..##.###.#....#..##..##.##.#......###.#.#..#....#.#...#...
+...#.##...#.....###...#.##.###...####.#####....##.#....##.#...#.#..##..##.#..#####.......##.#...#.#.
+##.#.#######.###...#.####..#..###.#.#..#.#.#.###..##.#.##.....#####..##.#...##...#....###.#.#...##..
+##.#....##.##.#.###.......##..#.##..#.#......#..#.##.#..##.###..##...#...###...#..##.###...###....##
+...#..#..#...##...#.###.#######.#.#....#..#.#####.#...#####....###.######....#.#..#.....##.#..##.##.
+...#.##########..#.##..###....##.....####.##..###..#.#.####..###.###...###..##...#.#.###...#...#.#.#
+.#...#.#.....#.##.#.#.##...#....#.....#.#.....#...##..##.#.##..#....#.##.#######.##.##....#...#..###
+.#..#....#.#...##.##.##...#.#..##.##..#...#.###..#.##.###.#...###....####..#.##..####.#.#..#########
+#.###.#..#####.##...##.##...#.#####...#..###....#.#..#..#.##..##.#..######..##.######.#..#.###..####
+##..#.##.##..#.....#...#...#####.##...##.#.##.####..#.###.##.#.....##..#....#.#..###.#.##.#.#.###...
+#.##..#.#.....#..#.##.###..###...##..#.#........##.###.#.........###.....#..#.##.##...###...#..###.#
+#.....##.#.....#.#..###..#.#...#..#..###.#......###..#.######..#...##.#.#...#.#.#.#..##.##...##.##..
+###...#....#..####.#..###..#.#.#..##.###..##.####...##.#..###.#.####.#..#....##...#.#...###..###..#.
+..##..#..#...##.....##..#.##.##..#.#.####.##.###.#.#####......#.#..##.#...##.#.#.####..###....##...#
+..##.#....#.####.....#.#.##....###..##..##..##....#...##...#.#.#.########...####....#..#.#.###.#.#..
+###.#.##.###......####....##.##.#...###.#.....#....###.##.#######.##.####.##.#.###.####..#......#..#
+...#..#..#.#....#.####...##.#..##.#####.###.#..#.##....#.##.#..####.#.#...######..###.#.######.##.#.
+####.#....#....#...###....#.#.#.##.#..##.#.####..##..#...#......#######.#.#.#.##..#.....#..##.......
+..##.#.#...#.##...#..#..##.#.##.......#.##.####..#.#.....#..#.....###.##.#.#...###..#..#...#.#.##.##
+.#.########..####.#.###...##...#........#.####.####.#.#####.###..#####...##.##..#..#.#......#...###.
+.#...#.###.#.#...#.#.#..####.##.#.####..#..#.##.....#####..#######.##....##...###..####.#.#####.....
+###.#.######.#.#.#..#.#....##.....#...###.####.#.#..#.....##.##.###.#.####.....##.#....#...##..#.#..
+##...#.....##.####.###.#.#.#.##.#.#.##.####....#.######....#....##......#....#..#.#..####.#.#.##.##.
+..#.##.##...#.#.....#.##.##.######.####..#...#..###.##..##...#.####.##....##..#.###.#.####.###.#...#
+#.##.##.##...#.....##.#.##.#.#####.#.#.....#..##...#.####...######..#....#.#.##..###.############..#
+#.##.###...#..#.#.....####..#..#.#..#.#########...#..##.#.#...#...#.#.#...#...#.#..##.#.#..####.#..#
+.##.##.##......##.#..###.#.##.####..#..##...###...##....#.##..#####.#.#.#...#.#.#..###..###..###..#.
+.##.#####..#.#.#.####.#.#.##.##.....######.#..###..#####..##....#.#.#.##.##.#...#.#.#.#.####.##.##..
+...#....#.##...#####.#.###.####...##.#...##.#.#..#.#...####......#####.#.##.#..#####.#...#..#...#...
+#..#.##...#.#..#.###.#.#.#...#.##.##..#.##...###.######..#.##..###..#.#.###.##.#.###.....#..#.#..###
+.###.#........##..##......##.#######.#.#..###...#..#.##..####.#.####..###...#..##.###.##.###.#...#..
+###.###..##..##.#..#.##....#####.##.#....#..###..#..#.#...#.#.#......#.##..#####...###..##.#.....###
+##.#..###...#.#..###.....###.###.#.#..#...#.#...###..#.....########.##......#...##..#.#.#.#..#.#.##.
+#..##.#.#.##.####..##.###..#...#..#..###..##...#..#......#.#....##......####.#.###...##..###...#...#
+..#..##..####..###..#####.#.#.##.##.##....##.#.#.##...#......#...#..###....####..#.##..#.###....#.##
+###..#######...#...######.#######.##..##..###.....##..#...#..##..#######.....#..##..##..##..#.####..
+..#.###.#.##...#.#.##.#.#.###..##.....###.###.#..####.#...##..##...#..#.###.##.##.####....#...##....
+##.#.#...##.....#.#.#.##..##..####....#.#...#.#.#.#..#..###..###.#.#.###..#.###..#..###...##.......#
+###...##..#.#.####..#..#...##..#..#.#.#.#...###.##.###.###.#####.#.....####..#####.###.##..###..#...
+..###.#####.##...#.####....#.#...##...##....###......###...#.#.#.......###..#..#...#####.#..########
+#.####....#.#.#.#.#.#.#..#.##...#.#.###.#.##.##..#..#.###.##.##....##.###...#..##.#.....##.###.##.#.
+.#.#.##..#.#.##...##.####.#...#.#..##.#..##.#.##..##..#.####.##.#...##..###.###..###..#.#....###.##.
+####.#...#....####.###.####...##.....#######.....###....##.#........#.###.....#.#.#.###.####.##.#.##
+.#..#..#..####.#.##..##...###..###.###...######.###....##...#.##..#####.#.####.......#...#.##.#.....
+.#..#....#...#####..#..#...#..#...##.#.##.#.##.#...#.##..##.####..#.###.###.#..##.###.......###.#...
+.##.##.#..#...###.####.#.###.#...#..#.##.#..##.#.#.##..####.#####....##.###.###....#....#..##.###..#
+..#..#..##.###...#.###.#.###...###....##.##.#.#..#..#.##.#.#.....#####..#.##.###.#.....#..#.#.######
+#...##..###.###.#..#....#..#.##..##.###..#.#.#..##...#.##..#..#.#####.......#.##......##.#.#..#..#..
+.#....####.##.###.##....#...###.#..#####.#..#..##....####.#...####.#..###...#....#.#..####..##....##
+##..#####..#..#..#.#####.#.###..#....#..##.#...#.###..#..#####....##..##.#......##.###..#.#...#.###.
+##.......#..##.#.#.##.#...#.########.#.#.##..##.#..###.....#.#..###...##..#......##.##.##......#..##
+.##...##.#.#.....#.#.#.#....###..#..##...#.#.#.##.####.##.##.##.....##.#.....#.#.##..#.#.####.#..##.
+..#..#....###.....#.###...###.###...##....##.#.#.##....#..######...#.##.#.#...##.#....##########.###
+.....#.#.#.##.#.#.....#..#.#.#..####.##.#...#.#.....#####.###.#...#...#....###.#.......#.#..#...##..
+.##..###.#....#..#.##.#.#.#..#..#.##.....#####..##.....##.##....#...#..##..#..#.....###..#...##.#..#
+#.#.#..#.#...#.#.#.#...#..#.#.###.#.....#...#...#.###..######.#..###..####..####...##..#.....#.####.
+#..##........#.#.##.#.##.#.#####..#....#####.####...#....#.#..####.#..###.#.###..##..#....##.#...#.#
+..#....#....#....#.#.####......#.##...###..###.#.#..#.#.#.###..#.#.###.....#..###..##..#.##..#.#..##
+#...#..#.###.#....#.#.#.#..#.####..#####..###.....##..#.###..#.##.#####.#.#....###.###..#.##.###..#.
+#####.###.#.#.####..#.###.##..#.#.#..#........#########.##.#.#.#..##.#...#..#...#.##....#..##.##.#.#
+#.##.....#.##.#..##.....####.##..#..#...#.#..#.#######..#.##.#..#.##...###.###....###..#.##.##.####.
+.#....##########..#.#..###..#.#.#.#...#..#.#..###..##....######.###....#...##...#...#..###.#.#...###
+#.#.#..#.##.....##..####.###.#.###.#..#....#.#...#..##.#.#.##...#..#.......#...#.....##.##.##...#...
+#....#...#..##..............##.###.#.#####.##..#########..#.#..##.###..##.##..#.##.##..##..#...###.#
+##...#.####...##.....##.##....##.######.##.#.##.####..##..#####..#..#.#.###.....#..##..#.#...####...
+#..#.####.#....##..#.###.##.#.#.#.#...##.##.#.####......###.#.####.#####...##.#.#.##.#..######..#..#
+..##...##.#.##.##.#.#..#....#.##.##.#..###...######.####.#......#..#.#..#.#####.....#.#.#..#......#.
+#......#.###.#......###....##.##.#.#...#...##.#.#.######..#.##.####..#..#...#.##.###....#......####.
+.#.#.#####.###.##.##......#......##.#.....##..#.###..#.##....##.#.##.#####..##.##..#####.#..#...##.#
+##.#.#.#..#.###.##.#.#....##.#.##.....#.#.#.#..#...#.....#.#.#.#.#####....#######.#..#..##..##.###.#
+..#.##..####.#.......#....######...####.#.#.###.#...######.#....##.#.##.##.###.#..#...##...##..#....
+.#.##.###..###........##.##.####.####.#...#.##....#####.##..####...##.##..#.#.####.#####....##....#.
+#.##.#.#.##......#.#####.#.#..###....#...#...#..##..##.#.#.####..###.#.#..######.###.......#..#.####
+..##.#.#...##...#.....#......#..##.#.##.###.#.###..#..#.#.####.##.#.####.#.####.#.#..###.##.###..#.#
+#........#.#..#.....##..#...##..##..##..#.##..##.#.####..####.#.#.#####..###.#.....#.#....#..#..###.
+#..##..#.######.....##..#.##..#..###.####.#.#....#.#.........#####.####..##.####.##..#.#..#..#.###..
+##.##..##.####..#.#...#..#..#..##.....##..#...#.###..#.............##.#...###....##.####.#.###..#..#
+#..#.#.###..###....#####.#.#.#..#.##.#.##...##..###.##..###....#.###.##..#.#.#.#######.##.#.#.#..#..

+ 82 - 0
day20_trench_map/main.py

@@ -0,0 +1,82 @@
+import os, sys
+
+task_dir = os.path.dirname(__file__)
+sys.path.append(f"{task_dir}/..")
+import numpy as np
+from get_tasks import get_input, generate_readme, check_example, bench
+from itertools import takewhile
+from scipy import ndimage
+
+
+def parse(input, test=False):
+    if test:
+        code = (
+            "".join([line for line in takewhile(lambda l: len(l) > 0, input)])
+            .replace("#", "1")
+            .replace(".", "0")
+        )
+        image = np.array([list(line) for line in input[8:]])
+    else:
+        code = input[0].replace("#", "1").replace(".", "0")
+        image = np.array([list(line) for line in input[2:]])
+    image[image == "."] = 0
+    image[image == "#"] = 1
+    return image.astype(int), code
+
+
+def naive_enhance_image(image, code, step):
+    if code[0] == "0":
+        pad = 0
+        val = pad
+    else:
+        pad = step % 2
+        val = 1 - pad
+    sp = 3 if step == 0 else 1
+    enhance_image = np.pad(image, sp, "constant", constant_values=pad)
+    enhanced_image = np.full_like(enhance_image, val, dtype=int)
+    for i in range(enhance_image.shape[0] - 2):
+        for j in range(enhance_image.shape[1] - 2):
+            win = enhance_image[i : i + 3, j : j + 3]
+            enhanced_image[i + 1, j + 1] = code[
+                int("".join(win.flatten().astype(str)), 2)
+            ]
+    return enhanced_image
+
+
+# works ony in test
+def scipy_enhance_image(image, code, outside=0):
+    def convert(values):
+        string = "".join(str(int(value)) for value in values)
+        return code[int(string, 2)]
+
+    enhance_image = np.pad(image, 1)
+    ndimage.generic_filter(
+        enhance_image, convert, size=3, mode="constant", cval=outside
+    )
+
+
+def part1(input, test=False):
+    image, code = parse(input, test)
+    for step in range(2):
+        image = naive_enhance_image(image, code, step)
+    print("The answer of part1 is:", image.sum())
+
+
+@bench
+def part2(input, test=False):
+    image, code = parse(input, test)
+    for step in range(50):
+        image = naive_enhance_image(image, code, step)
+    print("The answer of part2 is:", image.sum())
+
+
+if __name__ == "__main__":
+    input, example = get_input(task_dir, 20)
+
+    part1(example, True)
+    part2(example, True)
+
+    part1(input)
+    part2(input)
+
+    generate_readme(task_dir, 20)

+ 11 - 0
get_tasks.py

@@ -2,6 +2,7 @@ import os
 import requests
 import bs4
 import markdownify as md
+import time
 from typing import Any, Callable
 
 
@@ -67,6 +68,16 @@ def get_input(task_dir: str, day: int) -> tuple[list[str], list[str]]:
     return input, example
 
 
+def bench(part):
+    def wrapper(*args, **kwargs):
+        start = time.perf_counter()
+        value = part(*args, **kwargs)
+        print(f"\tevaluation time: {time.perf_counter() - start}s")
+        return value
+
+    return wrapper
+
+
 def check_example(example: Any, part: Callable):
     print(f'\n{10*"-"}Example test here{10*"-"}\n')
     part(example)