advent-of-code/2023/python/day03.py
2023-12-03 16:04:40 -05:00

75 lines
1.7 KiB
Python

import matrix
import shared
import itertools
import functools
SYMBOLS="*%@#+-/$=&"
def get_all_numbers_and_starting_coords(mat):
nums = []
for y, row in enumerate(mat):
x = 0
while x < len(row):
num = matrix.number_starting_at(mat, x,y)
if num is None:
x += 1
continue
nums.append(((x,y), num))
x += len(num)
return nums
def get_row_coords(x1,y, length):
coords = []
for x in range(x1, x1+length):
coords.append((x,y))
return coords
# @shared.profile
def part1(mat):
choose = []
nums = get_all_numbers_and_starting_coords(mat)
for coords, num in nums:
valid = process_number(mat, coords, num)
if valid:
choose.append(num)
total = 0
for n in choose:
total += int(n)
print(total)
def process_number(mx, coords, num):
line_coords = get_row_coords(*coords, len(num))
for coord in line_coords:
any_symbols = [v for _,v in matrix.get_neighbor_coords(mx, *coord) if v in SYMBOLS]
if any_symbols:
return True
return False
# @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)
def main():
mat = shared.load_file_char_matrix(shared.get_fname(3))
with shared.elapsed_timer() as elapsed:
part1(mat)
print("🕒", elapsed())
with shared.elapsed_timer() as elapsed:
part2(mat)
print("🕒", elapsed())
if __name__ == "__main__":
main()