This commit is contained in:
Tyrel Souza 2023-12-03 16:29:10 -05:00
parent d97c512816
commit 7f47fd46ad
No known key found for this signature in database
GPG Key ID: F3614B02ACBE438E

View File

@ -8,6 +8,11 @@ from collections import defaultdict
SYMBOLS="*%@#+-/$=&" SYMBOLS="*%@#+-/$=&"
def get_all_numbers_and_starting_coords(mat): def get_all_numbers_and_starting_coords(mat):
"""
for a coordinate, walks right until it encounters a non digit,
when it does, it adds that number string to a collection,
and starts over, walking right until it continues to next row
"""
nums = [] nums = []
for y, row in enumerate(mat): for y, row in enumerate(mat):
x = 0 x = 0
@ -20,12 +25,23 @@ def get_all_numbers_and_starting_coords(mat):
x += len(num) x += len(num)
return nums return nums
def get_row_coords(x1,y, length): def get_row_coords(x1, y, length):
"""
for a given x,y point, it just creates the rest of the x/y coords to the right of this coord for a run
"""
coords = [] coords = []
for x in range(x1, x1+length): for x in range(x1, x1+length):
coords.append((x,y)) coords.append((x,y))
return coords return coords
def process_number(mx, coords, num):
line_coords = get_row_coords(*coords, len(num))
for coord in line_coords:
any_symbols = [(cs, v) for cs,v in matrix.get_neighbor_coords(mx, *coord) if v in SYMBOLS]
if any_symbols:
return True, any_symbols[0]
return False, None
# @shared.profile # @shared.profile
def part1(mat): def part1(mat):
choose = [] choose = []
@ -40,15 +56,6 @@ def part1(mat):
print(total) print(total)
def process_number(mx, coords, num):
line_coords = get_row_coords(*coords, len(num))
for coord in line_coords:
any_symbols = [(cs, v) for cs,v in matrix.get_neighbor_coords(mx, *coord) if v in SYMBOLS]
if any_symbols:
return True, any_symbols[0]
return False, None
# @shared.profile # @shared.profile
def part2(mat): def part2(mat):
splats = matrix.find_in_matrix(mat, "*", False) splats = matrix.find_in_matrix(mat, "*", False)
@ -60,6 +67,7 @@ def part2(mat):
valid, sc = process_number(mat, coords, num) valid, sc = process_number(mat, coords, num)
if valid: if valid:
symbols[(sc[0]['r'],sc[0]['c'])].append(num) symbols[(sc[0]['r'],sc[0]['c'])].append(num)
total = 0 total = 0
for splat in splats: for splat in splats:
neighbors = symbols[splat] neighbors = symbols[splat]