Skip to content

Instantly share code, notes, and snippets.

@vadim2404
Last active December 3, 2023 11:00
Show Gist options
  • Save vadim2404/e9e104517243cf0e08e6208a59bf12af to your computer and use it in GitHub Desktop.
Save vadim2404/e9e104517243cf0e08e6208a59bf12af to your computer and use it in GitHub Desktop.
Day 3
#!/usr/bin/env python3
from typing import List, Tuple
LEFT_RIGHT_DIRECTIONS = tuple(range(-1, 2))
CENTRAL_DIRECTIONS = tuple(i for i in range(-1, 2) if i != 0)
with open("input.txt", "r") as f:
matrix = [line.strip() for line in f]
n, m = len(matrix), len(matrix[0])
total_sum = 0
def find_the_symbol(i: int, j: int, n: int, matrix: List[str], directions: Tuple[int, ...]) -> bool:
for di in directions:
if 0 <= i + di < n and not matrix[i + di][j].isnumeric() and matrix[i + di][j] != ".":
return True
return False
for i in range(n):
j = 0
while j < m:
if matrix[i][j].isnumeric():
number = int(matrix[i][j])
jj = j + 1
has_symbols = False
if j > 0:
has_symbols = find_the_symbol(i, j - 1, n, matrix, LEFT_RIGHT_DIRECTIONS)
if not has_symbols:
has_symbols = find_the_symbol(i, j, n, matrix, CENTRAL_DIRECTIONS)
while jj < m and matrix[i][jj].isnumeric():
number = number * 10 + int(matrix[i][jj])
if not has_symbols:
has_symbols = find_the_symbol(i, jj, n, matrix, CENTRAL_DIRECTIONS)
jj += 1
if not has_symbols and jj < m:
has_symbols = find_the_symbol(i, jj, n, matrix, LEFT_RIGHT_DIRECTIONS)
if has_symbols:
total_sum += number
j = jj
else:
j += 1
print(total_sum)
#!/usr/bin/env python3
from typing import List, Optional, Tuple
LEFT_RIGHT_DIRECTIONS = tuple(range(-1, 2))
CENTRAL_DIRECTIONS = tuple(i for i in range(-1, 2) if i != 0)
ALL_DIRECTIONS = tuple((i, j,) for i in range(-1, 2) for j in range(-1, 2) if i != 0 or j != 0)
with open("input.txt", "r") as f:
matrix = [line.strip() for line in f]
n, m = len(matrix), len(matrix[0])
total_sum = 0
def find_gear_part(i: int, j: int, n: int, matrix: List[str], directions: Tuple[int, ...]) -> Optional[Tuple[int, int]]:
for di in directions:
if 0 <= i + di < n and matrix[i + di][j] == "*":
return (i + di, j, )
return None
for i in range(n):
j = 0
while j < m:
if matrix[i][j].isnumeric():
used = {(i, j, ), }
number = int(matrix[i][j])
jj = j + 1
asterisk_coordinates = None
if j > 0:
asterisk_coordinates = find_gear_part(i, j - 1, n, matrix, LEFT_RIGHT_DIRECTIONS)
if not asterisk_coordinates:
asterisk_coordinates = find_gear_part(i, j, n, matrix, CENTRAL_DIRECTIONS)
while jj < m and matrix[i][jj].isnumeric():
used.add((i, jj, ))
number = number * 10 + int(matrix[i][jj])
if not asterisk_coordinates:
asterisk_coordinates = find_gear_part(i, jj, n, matrix, CENTRAL_DIRECTIONS)
jj += 1
if not asterisk_coordinates and jj < m:
asterisk_coordinates = find_gear_part(i, jj, n, matrix, LEFT_RIGHT_DIRECTIONS)
if asterisk_coordinates:
number_position = None
for di, dj in ALL_DIRECTIONS:
if 0 <= asterisk_coordinates[0] + di < n and \
0 <= asterisk_coordinates[1] + dj < m and \
not (asterisk_coordinates[0] + di, asterisk_coordinates[1] + dj, ) in used and \
matrix[asterisk_coordinates[0] + di][asterisk_coordinates[1] + dj].isnumeric():
number_position = (asterisk_coordinates[0] + di, asterisk_coordinates[1] + dj, )
break
if number_position:
i2, j2 = number_position
while j2 > 0 and matrix[i2][j2 - 1].isnumeric():
j2 -= 1
new_number = int(matrix[i2][j2])
j2 += 1
while j2 < m and matrix[i2][j2].isnumeric():
new_number = new_number * 10 + int(matrix[i2][j2])
j2 += 1
total_sum += number * new_number
j = jj
else:
j += 1
print(total_sum >> 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment