2023 dec 3rd

This commit is contained in:
Julia Lange 2023-12-04 18:57:59 -08:00
parent 4be3f6dbd4
commit 5bd8d2a7f1
Signed by: Julia
SSH key fingerprint: SHA256:KI8YxpkPRbnDRkXPgCuQCVz181++Vy7NAvmQj8alOhM

46
Python/2023/03/main.py Normal file
View file

@ -0,0 +1,46 @@
input = open("input", 'r')
data = []
for line in input:
data.append(line.rstrip())
number_pos = set()
for i, line in enumerate(data):
for j, character in enumerate(line):
if not character.isdigit() and character != '.':
for x,y in [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]:
if data[i+y][j+x].isdigit():
number_pos.add((i+y,j+x))
seen_pos = set()
numbers = []
for pos_y,pos_x in number_pos:
if (pos_y, pos_x) in seen_pos:
continue
seen_pos.add((pos_y, pos_x))
line = data[pos_y]
start = pos_x
end = pos_x
while line[start-1].isdigit():
start -= 1
if start-1 < 0:
break
while line[end+1].isdigit():
end += 1
if end+1 >= len(line):
break
for i in range(start,end+1):
seen_pos.add((pos_y,i))
numbers.append(int(line[start:end+1]))
print(numbers)
print(sum(numbers))