diff --git a/2023/day03/example.txt b/2023/day03/example.txt new file mode 100644 index 0000000..624ea4f --- /dev/null +++ b/2023/day03/example.txt @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. \ No newline at end of file diff --git a/2023/day03/solutions/python2.py b/2023/day03/solutions/python2.py new file mode 100644 index 0000000..8d6f1a2 --- /dev/null +++ b/2023/day03/solutions/python2.py @@ -0,0 +1,84 @@ +with open("../input.txt", "r") as fp: + inputs = [line.rstrip() for line in fp] + +def getReplaceNumber(y,x): + if not inputs[y][x].isdigit() or inputs[y][x] == ".": + return (0,1) + num = [inputs[y][x]] + xSave = x + # Left + x-=1 + while x >= 0 and inputs[y][x].isdigit(): + num.insert(0,inputs[y][x]) + tmp = list(inputs[y]) + tmp[x] = '.' + inputs[y] = ''.join(tmp) + x-=1 + # Right + x = xSave + 1 + while x < len(inputs[y]) and inputs[y][x].isdigit(): + num += inputs[y][x] + tmp = list(inputs[y]) + tmp[x] = '.' + inputs[y] = ''.join(tmp) + x+=1 + return (1,int(''.join(num))) + +def findNumbers(y, x): + if inputs[y][x] != "*": + return 0 + + product = 1 + cnt = 0 + + # N + if y-1>=0: + tmp = getReplaceNumber(y-1, x) + cnt += tmp[0] + product *= tmp[-1] + # NW + if x-1>=0: + tmp = getReplaceNumber(y-1, x-1) + cnt += tmp[0] + product *= tmp[-1] + # NE + if x+1=0: + tmp = getReplaceNumber(y+1, x-1) + cnt += tmp[0] + product *= tmp[-1] + # SE + if x+1=0: + tmp = getReplaceNumber(y, x-1) + cnt += tmp[0] + product *= tmp[-1] + # E + if x+1 1) else 0 + +finalSum = 0 +for y in range(0,len(inputs)): + for x in range(0,len(inputs[y])): + finalSum += findNumbers(y,x) + +print(finalSum) \ No newline at end of file