From 00a147462bc83df76c879aa7b0a9223ca0797f7f Mon Sep 17 00:00:00 2001 From: Kees van Kempen Date: Sat, 6 Dec 2025 18:25:51 +0100 Subject: [PATCH] 2025(06): thanks to loops, things work MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 2.28 ms ± 347 μs per loop (mean ± std. dev. of 7 runs, 100 loops each) --- 2025/06/part2.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 2025/06/part2.py diff --git a/2025/06/part2.py b/2025/06/part2.py new file mode 100644 index 0000000..c240a09 --- /dev/null +++ b/2025/06/part2.py @@ -0,0 +1,68 @@ +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()) + + # For the last column, I don't have that nice column of nothing + if icol == num_of_cols - 1: + col_len += 1 + + # Prepare empty string for my numbers + nums = (col_len - 1)*[""] + + # Make the num strings by moving row by row + for jcol in range(icol_prev, icol_prev + col_len - 1): + for row in data[:-1]: + nums[jcol - icol_prev] += row[jcol] + + # Do the math + if op == '*': + result = 1 + if debug: + print(1, end="") + for num in nums: + num = int(num) + if debug: + print(f" * {num}", end="") + + result *= num + elif op == '+': + result = 0 + if debug: + print(0, end="") + for num in nums: + num = int(num) + 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") == 3263827 + + print(do_homework("input")) \ No newline at end of file