Successful submission for day 06!
This commit is contained in:
parent
4d75e5ebb8
commit
56e423fe97
4 changed files with 124 additions and 12 deletions
6
.gitignore
vendored
6
.gitignore
vendored
|
|
@ -1,3 +1,7 @@
|
|||
.pyc
|
||||
*/__pycache__/*
|
||||
*.log
|
||||
*.log
|
||||
*/main
|
||||
**/*.o
|
||||
.vscode
|
||||
.mypy_cache
|
||||
59
06/README.md
Normal file
59
06/README.md
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
# --- 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.
|
||||
|
||||
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:
|
||||
|
||||
```txt
|
||||
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:
|
||||
|
||||
```txt
|
||||
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?
|
||||
4
06/input/test_worksheet.txt
Normal file
4
06/input/test_worksheet.txt
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
123 328 51 64
|
||||
45 64 387 23
|
||||
6 98 215 314
|
||||
* + * +
|
||||
67
06/main.py
67
06/main.py
|
|
@ -4,28 +4,73 @@ from logging import debug, DEBUG, basicConfig
|
|||
from sys import argv
|
||||
|
||||
|
||||
def parse_input(input_filepath: str) -> tuple[list[list[int]], list[str]]:
|
||||
numbers: list[list[int]] = []
|
||||
operators: list[str] = []
|
||||
def parse_input(
|
||||
input_filepath: str,
|
||||
) -> tuple[list[tuple[list[int], str]], list[tuple[list[int], str]]]:
|
||||
numbers: list[str] = []
|
||||
operators: str = ""
|
||||
|
||||
with open(file=input_filepath, mode="r") as input_file:
|
||||
input_data: list[str] = input_file.readlines()
|
||||
|
||||
for line in input_data:
|
||||
debug(f"\n\nRAW INPUT: {line}\n\n")
|
||||
|
||||
for line in input_data:
|
||||
if "÷" == line.split()[0] or "*" == line.split()[0]:
|
||||
operators = line.split()
|
||||
if line.find("+") != -1:
|
||||
operators = line
|
||||
break
|
||||
numbers.append([int(x) for x in line.split()])
|
||||
numbers.append(line)
|
||||
|
||||
return (numbers, operators)
|
||||
human_math: list[tuple[list[int], str]] = [
|
||||
([int(n.split()[o]) for n in numbers], operators.split()[o])
|
||||
for o in range(len(operators.split()))
|
||||
]
|
||||
|
||||
cephalapod_math: list[tuple[list[int], str]] = translate_input(numbers, operators)
|
||||
|
||||
return (human_math, cephalapod_math)
|
||||
|
||||
|
||||
def translate_input(numbers: list[str], operators: str) -> list[tuple[list[int], str]]:
|
||||
cephalapod_math: list[tuple[list[int], str]] = []
|
||||
current_numbers: list[int] = []
|
||||
current_operator = operators[0]
|
||||
|
||||
for i in range(len(operators)):
|
||||
if operators[i] in ["+", "*"] and current_numbers != []:
|
||||
cephalapod_math.append((current_numbers, current_operator))
|
||||
current_operator = operators[i]
|
||||
current_numbers = []
|
||||
temp_number: str = ""
|
||||
for row in numbers:
|
||||
temp_number += row[i].strip()
|
||||
if temp_number != "":
|
||||
current_numbers.append(int(temp_number))
|
||||
cephalapod_math.append((current_numbers, current_operator))
|
||||
|
||||
return cephalapod_math
|
||||
|
||||
|
||||
def get_solutions(math_problems: list[tuple[list[int], str]]) -> list[int]:
|
||||
debug(f"PROBLEMS: {math_problems}")
|
||||
solutions: list[int] = []
|
||||
for problem in math_problems:
|
||||
if problem[1] == "+":
|
||||
solutions.append(sum(problem[0]))
|
||||
continue
|
||||
product = problem[0][0]
|
||||
for n in problem[0][1:]:
|
||||
product *= n
|
||||
solutions.append(product)
|
||||
debug(f"SOLUTIONS: {solutions}")
|
||||
return solutions
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input_filepath = "input/worksheet.txt"
|
||||
numbers, operators = parse_input(input_filepath)
|
||||
human_math, cephalapod_math = parse_input(input_filepath)
|
||||
solutions = get_solutions(human_math)
|
||||
print(f"Sum of all (human math) solutions: {sum(solutions)}")
|
||||
solutions = get_solutions(cephalapod_math)
|
||||
print(f"Sum of all (cephalapod math) solutions: {sum(solutions)}")
|
||||
return
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue