diff --git a/2023/day03/README.md b/2023/day03/README.md index 77c9503..5374cef 100644 --- a/2023/day03/README.md +++ b/2023/day03/README.md @@ -1,3 +1,7 @@ +Part 1: `543867` + +Part 2: `` + [--- Day 3: Gear Ratios --- -------------------------- @@ -43,4 +47,46 @@ In this schematic, two numbers are *not* part numbers because they are not adjac Of course, the actual engine schematic is much larger. *What is the sum of all of the part numbers in the engine schematic?* +, --- Part Two --- +---------------- + +The engineer finds the missing part and installs it in the engine! As the engine springs to life, you jump in the closest gondola, finally ready to ascend to the water source. + + +You don't seem to be going very fast, though. Maybe something is still wrong? Fortunately, the gondola has a phone labeled "help", so you pick it up and the engineer answers. + + +Before you can explain the situation, she suggests that you look out the window. There stands the engineer, holding a phone in one hand and waving with the other. You're going so slowly that you haven't even left the station. You exit the gondola. + + +The missing part wasn't the only issue - one of the gears in the engine is wrong. A *gear* is any `*` symbol that is adjacent to *exactly two part numbers*. Its *gear ratio* is the result of multiplying those two numbers together. + + +This time, you need to find the gear ratio of every gear and add them all up so that the engineer can figure out which gear needs to be replaced. + + +Consider the same engine schematic again: + + + +``` +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. + +``` + +In this schematic, there are *two* gears. The first is in the top left; it has part numbers `467` and `35`, so its gear ratio is `16345`. The second gear is in the lower right; its gear ratio is `451490`. (The `*` adjacent to `617` is *not* a gear because it is only adjacent to one part number.) Adding up all of the gear ratios produces `*467835*`. + + +*What is the sum of all of the gear ratios in your engine schematic?* + + ] \ No newline at end of file diff --git a/2023/day03/solutions/python1.py b/2023/day03/solutions/python1.py new file mode 100644 index 0000000..791bbd2 --- /dev/null +++ b/2023/day03/solutions/python1.py @@ -0,0 +1,67 @@ +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 + 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 int(''.join(num)) + +def findNumbers(y, x): + if inputs[y][x].isdigit() or inputs[y][x] == ".": + return 0 + + sum = 0 + + # N + if y-1>=0: + sum += getReplaceNumber(y-1, x) + # NW + if x-1>=0: + sum += getReplaceNumber(y-1, x-1) + # NE + if x+1=0: + sum += getReplaceNumber(y+1, x-1) + # SE + if x+1=0: + sum += getReplaceNumber(y, x-1) + # E + if x+1