diff --git a/2023/python/day03.py b/2023/python/day03.py index 05b9802..c8d454f 100644 --- a/2023/python/day03.py +++ b/2023/python/day03.py @@ -2,6 +2,8 @@ import matrix import shared import itertools import functools +from pprint import pprint +from collections import defaultdict SYMBOLS="*%@#+-/$=&" @@ -49,13 +51,23 @@ def process_number(mx, coords, num): # @shared.profile def part2(mat): - print(matrix.ppmx(mat, pad=False)) - _nums = get_all_numbers_and_starting_coords(mat) - nums = [] - #get_row_coords(*coords, len(num)) - splats = matrix.find_in_matrix(mat, "*", False) - print(splats) + _nums = get_all_numbers_and_starting_coords(mat) + + symbols = defaultdict(list) + nums = get_all_numbers_and_starting_coords(mat) + for coords, num in nums: + valid, sc = process_number(mat, coords, num) + if valid: + symbols[(sc[0]['r'],sc[0]['c'])].append(num) + total = 0 + for splat in splats: + neighbors = symbols[splat] + if len(neighbors) != 2: + continue + ratio = int(neighbors[0]) * int(neighbors[1]) + total += ratio + print(total)