From d97c512816b998ff6ed4f193b796de3554479f37 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Sun, 3 Dec 2023 16:18:14 -0500 Subject: [PATCH] day 3 part 2 --- 2023/python/day03.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) 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)