瀏覽代碼

day6 python

metya 1 月之前
父節點
當前提交
8fda234926
共有 6 個文件被更改,包括 145 次插入1 次删除
  1. 1 1
      aoc2025/aocutils.py
  2. 61 0
      aoc2025/day6/README.md
  3. 0 0
      aoc2025/day6/day6.go
  4. 79 0
      aoc2025/day6/day6.py
  5. 4 0
      aoc2025/day6/example.txt
  6. 0 0
      aoc2025/day6/input.txt

+ 1 - 1
aoc2025/aocutils.py

@@ -95,7 +95,7 @@ def bench(part):
 
 
 if __name__ == "__main__":
-    day = 5
+    day = 6
     root = os.path.dirname(__file__)
     task_dir = os.path.join(root, f"day{day}")
     generate_readme(task_dir, day)

+ 61 - 0
aoc2025/day6/README.md

@@ -0,0 +1,61 @@
+--- Day 6: Trash Compactor ---
+------------------------------
+
+After helping the Elves in the kitchen, you were taking a break and helping them re-enact a movie scene when you over-enthusiastically jumped into the garbage chute!
+
+A brief fall later, you find yourself in a garbage smasher. Unfortunately, the door's been magnetically sealed.
+
+As you try to find a way out, you are approached by a family of cephalopods! They're pretty sure they can get the door open, but it will take some time. While you wait, they're curious if you can help the youngest cephalopod with her [math homework](/2021/day/18).
+
+Cephalopod math doesn't look that different from normal math. The math worksheet (your puzzle input) consists of a list of *problems*; each problem has a group of numbers that need to be either *added* (`+`) or *multiplied* (`*`) together.
+
+However, the problems are arranged a little strangely; they seem to be presented next to each other in a very long horizontal list. For example:
+
+```
+123 328  51 64 
+ 45 64  387 23 
+  6 98  215 314
+*   +   *   +
+```
+
+Each problem's numbers are arranged vertically; at the bottom of the problem is the symbol for the operation that needs to be performed. Problems are separated by a full column of only spaces. The left/right alignment of numbers within each problem can be ignored.
+
+So, this worksheet contains four problems:
+
+* `123` \* `45` \* `6` = `33210`
+* `328` + `64` + `98` = `490`
+* `51` \* `387` \* `215` = `4243455`
+* `64` + `23` + `314` = `401`
+
+To check their work, cephalopod students are given the *grand total* of adding together all of the answers to the individual problems. In this worksheet, the grand total is `33210` + `490` + `4243455` + `401` = `4277556`.
+
+Of course, the actual worksheet is *much* wider. You'll need to make sure to unroll it completely so that you can read the problems clearly.
+
+Solve the problems on the math worksheet. *What is the grand total found by adding together all of the answers to the individual problems?*
+
+--- Part Two ---
+----------------
+
+The big cephalopods come back to check on how things are going. When they see that your grand total doesn't match the one expected by the worksheet, they realize they forgot to explain how to read cephalopod math.
+
+Cephalopod math is written *right-to-left in columns*. Each number is given in its own column, with the most significant digit at the top and the least significant digit at the bottom. (Problems are still separated with a column consisting only of spaces, and the symbol at the bottom of the problem is still the operator to use.)
+
+Here's the example worksheet again:
+
+```
+123 328  51 64 
+ 45 64  387 23 
+  6 98  215 314
+*   +   *   +
+```
+
+Reading the problems right-to-left one column at a time, the problems are now quite different:
+
+* The rightmost problem is `4` + `431` + `623` = `1058`
+* The second problem from the right is `175` \* `581` \* `32` = `3253600`
+* The third problem from the right is `8` + `248` + `369` = `625`
+* Finally, the leftmost problem is `356` \* `24` \* `1` = `8544`
+
+Now, the grand total is `1058` + `3253600` + `625` + `8544` = `3263827`.
+
+Solve the problems on the math worksheet again. *What is the grand total found by adding together all of the answers to the individual problems?*

+ 0 - 0
aoc2025/day6/day6.go


+ 79 - 0
aoc2025/day6/day6.py

@@ -0,0 +1,79 @@
+import os
+
+with open(os.path.join(os.path.dirname(__file__), "example.txt")) as example:
+    example_data = example.read().splitlines()
+
+with open(os.path.join(os.path.dirname(__file__), "input.txt")) as example:
+    input_data = example.read().splitlines()
+
+
+def part1(data):
+    problems = []
+    for problem in data[:-1]:
+        problems.append([int(d) for d in problem.split()])
+    mo = data[-1].split()
+    sum = 0
+    for row in range(len(mo)):
+        sp = 1 if mo[row] == "*" else 0
+        for problem in problems:
+            sp = sp * problem[row] if mo[row] == "*" else sp + problem[row]
+        sum += sp
+    print(f"Part1: {sum=}")
+
+
+def bench(part):
+    import time
+
+    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
+
+
+@bench
+def part2(data):
+    # get the max numer of tens in each colums
+    mo = data[-1].split()
+    tens = [list(map(len, row.split())) for row in data[:-1]]
+    max_tens = []
+    for i in range(len(mo)):
+        pipi = []
+        for ten in tens:
+            pipi.append(ten[i])
+        max_tens.append(max(pipi))
+
+    # get the numbers in colums with respect of tens and position of digits
+    cursor = 0
+    problems = []
+    for tens in max_tens:
+        digits = []
+        for problem in data[:-1]:
+            digits.append((problem[cursor : cursor + tens]))
+        problems.append(digits)
+        cursor += tens + 1
+
+    # get the result calculating digits in column with respect of tens
+    sum = 0
+    for problem, m in zip(problems, mo):
+        sp = 1 if m == "*" else 0
+        for i in range(len(problem[0])):
+            num = ""
+            for nums in problem:
+                digit = nums[i]
+                num += digit
+            if m == "*":
+                sp = sp * int(num)
+            else:
+                sp = sp + int(num)
+        sum += sp
+    print(f"Part2: {sum=}")
+
+
+part1(example_data)
+part1(input_data)
+
+part2(example_data)
+part2(input_data)

+ 4 - 0
aoc2025/day6/example.txt

@@ -0,0 +1,4 @@
+123 328  51 64 
+ 45 64  387 23 
+  6 98  215 314
+*   +   *   +

File diff suppressed because it is too large
+ 0 - 0
aoc2025/day6/input.txt


Some files were not shown because too many files changed in this diff