Compare commits
2 Commits
75bae4dd22
...
feb5cdd844
Author | SHA1 | Date | |
---|---|---|---|
feb5cdd844 | |||
8eded1bfe8 |
10
2023/day03/example1.txt
Normal file
10
2023/day03/example1.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
467..114..
|
||||||
|
...*......
|
||||||
|
..35..633.
|
||||||
|
......#...
|
||||||
|
617*......
|
||||||
|
.....+.58.
|
||||||
|
..592.....
|
||||||
|
......755.
|
||||||
|
...$.*....
|
||||||
|
.664.598..
|
10
2023/day03/example2.txt
Normal file
10
2023/day03/example2.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
467..114..
|
||||||
|
...*......
|
||||||
|
..35..633.
|
||||||
|
......#...
|
||||||
|
617*......
|
||||||
|
.....+.58.
|
||||||
|
..592.....
|
||||||
|
......755.
|
||||||
|
...$.*....
|
||||||
|
.664.598..
|
43
2023/day03/src/part1.py
Normal file
43
2023/day03/src/part1.py
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
text: str = open("input.txt", "r").read()
|
||||||
|
|
||||||
|
value: int = 0;
|
||||||
|
|
||||||
|
def get_symbol_around(list:[str], pos: tuple, length: int):
|
||||||
|
start = [0,0]
|
||||||
|
end = [0,0]
|
||||||
|
|
||||||
|
start[0] = pos[0]
|
||||||
|
start[1] = pos[1]
|
||||||
|
start[0] -= (start[0] != 0)
|
||||||
|
start[1] -= (start[1] != 0)
|
||||||
|
end[0] = pos[0]
|
||||||
|
end[1] = pos[1]
|
||||||
|
end[0] += length
|
||||||
|
end[0] += end[0] < len(list[pos[1]])
|
||||||
|
end[1] += end[1] < len(list)
|
||||||
|
bozo = list[start[1]:end[1] + 1]
|
||||||
|
for y in bozo:
|
||||||
|
bozo2 = y[start[0]:end[0]]
|
||||||
|
print(bozo2)
|
||||||
|
for x in bozo2:
|
||||||
|
if (not x.isdigit() and not x == "."):
|
||||||
|
print()
|
||||||
|
return 1
|
||||||
|
print()
|
||||||
|
return 0
|
||||||
|
|
||||||
|
lst = text.split("\n")
|
||||||
|
end: int = 0
|
||||||
|
for y in range(len(lst)):
|
||||||
|
lst[y] += "."
|
||||||
|
for x in range(len(lst[y])):
|
||||||
|
if (lst[y][x].isdigit()):
|
||||||
|
end += 1;
|
||||||
|
elif (end != 0):
|
||||||
|
if (get_symbol_around(lst, (x - end, y), end)):
|
||||||
|
value += int(lst[y][x - end:x])
|
||||||
|
else:
|
||||||
|
print(f"./input.txt:{y + 1}:{x}")
|
||||||
|
end = 0;
|
||||||
|
|
||||||
|
print(value)
|
51
2023/day03/src/part2.py
Normal file
51
2023/day03/src/part2.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
text: str = open("input.txt", "r").read()
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
|
value: int = 0;
|
||||||
|
|
||||||
|
def get_pos_of_digits_around(lst: list[str], x:int, y:int):
|
||||||
|
start_x = x - (x > 0)
|
||||||
|
start_y = y - (y > 0)
|
||||||
|
stop_x = x + (x < len(lst[y]) - 1)
|
||||||
|
stop_y = y + (y < len(lst) - 1)
|
||||||
|
|
||||||
|
lst_pos = []
|
||||||
|
|
||||||
|
for y, line in enumerate(lst[start_y:stop_y + 1]):
|
||||||
|
line = line[start_x:stop_x + 1]
|
||||||
|
for x, char in enumerate(line):
|
||||||
|
if (char.isdigit() and not (x > 0 and line[x - 1].isdigit())):
|
||||||
|
lst_pos.append((start_x + x, start_y + y))
|
||||||
|
|
||||||
|
return (lst_pos)
|
||||||
|
|
||||||
|
def get_numbers_by_pos(lst: [str], lst_pos: [(int, int)]):
|
||||||
|
|
||||||
|
lst_number = []
|
||||||
|
|
||||||
|
for x, y in lst_pos:
|
||||||
|
start: int = x
|
||||||
|
while (start > 0 and lst[y][start - 1].isdigit()):
|
||||||
|
start -= 1
|
||||||
|
stop: int = x
|
||||||
|
while (stop < len(lst[y]) and lst[y][stop].isdigit()):
|
||||||
|
stop += 1
|
||||||
|
print(lst[y][start:stop])
|
||||||
|
lst_number.append(int(lst[y][start:stop]))
|
||||||
|
print(f"example2.txt:{y + 1}:{x + 1}")
|
||||||
|
return lst_number
|
||||||
|
|
||||||
|
lst_text: [str] = text.split("\n")
|
||||||
|
|
||||||
|
for y, line in enumerate(lst_text):
|
||||||
|
for x, char in enumerate(line):
|
||||||
|
if (char == "*"):
|
||||||
|
lst = get_pos_of_digits_around(lst_text, x, y)
|
||||||
|
if (len(lst) != 2):
|
||||||
|
continue
|
||||||
|
lst = get_numbers_by_pos(lst_text, lst)
|
||||||
|
print(lst)
|
||||||
|
value += math.prod(lst)
|
||||||
|
|
||||||
|
print(value)
|
Loading…
Reference in New Issue
Block a user