2025(06): solve part 1 without dependencies
This commit is contained in:
@ -36,3 +36,28 @@ To check their work, cephalopod students are given the _grand total_ of adding t
|
||||
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?_
|
||||
|
||||
56
2025/06/part1.py
Normal file
56
2025/06/part1.py
Normal file
@ -0,0 +1,56 @@
|
||||
debug = False
|
||||
|
||||
def do_homework(filename: str):
|
||||
# Read file as list of strings
|
||||
with open(filename, "r") as fp:
|
||||
data = fp.read().splitlines()
|
||||
|
||||
# Gimme the operators
|
||||
last_row = data[-1]
|
||||
operators = last_row.split()
|
||||
|
||||
# Use that to find the lengths
|
||||
num_of_cols = len(operators)
|
||||
|
||||
icol_prev = 0
|
||||
final_result = 0
|
||||
for icol in range(num_of_cols):
|
||||
op = operators[icol]
|
||||
|
||||
# Find the length of this column.
|
||||
col_len = len(last_row[icol_prev:]) - len(last_row[icol_prev + 1:].lstrip())
|
||||
|
||||
# Do the math
|
||||
if op == '*':
|
||||
result = 1
|
||||
if debug:
|
||||
print(1, end="")
|
||||
for row in data[:-1]:
|
||||
num = int(row[icol_prev:icol_prev + col_len])
|
||||
if debug:
|
||||
print(f" * {num}", end="")
|
||||
|
||||
result *= num
|
||||
elif op == '+':
|
||||
result = 0
|
||||
if debug:
|
||||
print(0, end="")
|
||||
for row in data[:-1]:
|
||||
num = int(row[icol_prev:icol_prev + col_len])
|
||||
if debug:
|
||||
print(f" + {num}", end="")
|
||||
|
||||
result += num
|
||||
else:
|
||||
assert False, "fuck this"
|
||||
|
||||
icol_prev += col_len
|
||||
final_result += result
|
||||
if debug:
|
||||
print(f" = {result}")
|
||||
return final_result
|
||||
|
||||
if __name__ == "__main__":
|
||||
assert do_homework("testinput") == 4277556
|
||||
|
||||
print(do_homework("input"))
|
||||
Reference in New Issue
Block a user