2025(06): solve part 1 without dependencies

This commit is contained in:
2025-12-06 18:06:05 +01:00
parent 819c9bff73
commit 0fa7e7d6fa
2 changed files with 81 additions and 0 deletions

56
2025/06/part1.py Normal file
View 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"))