add day3, part 2

This commit is contained in:
Dmitry Fedotov
2023-12-10 20:13:01 +03:00
parent 916616784f
commit f01e786713
2 changed files with 56 additions and 14 deletions

View File

@@ -10,7 +10,8 @@ class Scheme(object):
for line in inp: for line in inp:
self.grid.append(['.'] + list(line) + ['.']) self.grid.append(['.'] + list(line) + ['.'])
self.grid.append(['.' for _ in range(len(inp[0])+2)]) self.grid.append(['.' for _ in range(len(inp[0])+2)])
self.part_numbers =[] self.part_numbers = []
self.gears = []
def __str__(self): def __str__(self):
out = '' out = ''
@@ -71,24 +72,32 @@ class Scheme(object):
x_array.sort() x_array.sort()
return int(found_digit), (y,) + tuple(x_array) return int(found_digit), (y,) + tuple(x_array)
def _find_part_numbers(self): def _find_part_numbers(self):
symbols = self._find_all_symbols() symbols = self._find_all_symbols()
for symbol in symbols: for symbol in symbols:
found_numbers = self._find_adjacent_numerals(symbol[0], symbol[1]) found_numbers = self._find_adjacent_numerals(symbol[0], symbol[1])
self.part_numbers.extend(found_numbers) self.part_numbers.extend(found_numbers)
def _find_all_gears(self):
symbols = self._find_all_symbols()
for symbol in symbols:
if self.grid[symbol[0]][symbol[1]] == '*':
numerals = self._find_adjacent_numerals(symbol[0], symbol[1])
if len(numerals) == 2:
# there are exactly to numerals adjacent
# to a '*' symbol, so this is a gear
self.gears.append(tuple(numerals))
def find_sum_part_numbers(self): def find_sum_part_numbers(self):
self._find_part_numbers() self._find_part_numbers()
return sum(self.part_numbers) return sum(self.part_numbers)
def find_sum_gears(self):
def solve1(lines): self._find_all_gears()
scheme = Scheme(lines)
return scheme.find_sum_part_numbers()
def solve2(lines):
total = 0 total = 0
for g in self.gears:
ratio = g[0] * g[1]
total += ratio
return total return total
@@ -96,10 +105,12 @@ if __name__ == '__main__':
r = tools.Reader('input.txt') r = tools.Reader('input.txt')
lines = r.read() lines = r.read()
x = solve1(lines) scheme = Scheme(lines)
x = scheme.find_sum_part_numbers()
print(x) print(x)
y = solve2(lines) y = scheme.find_sum_gears()
print(y) print(y)

View File

@@ -30,3 +30,34 @@ Of course, the actual engine schematic is much larger. What is the sum of all of
Your puzzle answer was 531561. Your puzzle answer was 531561.
The first half of this puzzle is complete! It provides one gold star: * The first half of this puzzle is complete! It provides one gold star: *
--- 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?
Your puzzle answer was 83279367.
Both parts of this puzzle are complete! They provide two gold stars: **