Files
Dmitry Fedotov 9c9e16fbe4 tidy up day 3
2023-12-10 20:29:23 +03:00

127 lines
3.8 KiB
Python

import sys
sys.path.append('../lib')
import tools, re
class Scheme(object):
def __init__(self, inp):
self.grid = []
self.grid.append(['.' for _ in range(len(inp[0])+2)])
for line in inp:
self.grid.append(['.'] + list(line) + ['.'])
self.grid.append(['.' for _ in range(len(inp[0])+2)])
self.part_numbers = []
self.gears = []
def __str__(self):
out = ''
for row in self.grid:
out += ''.join(row) + '\n'
return out
def _isdigit(self, y, x) -> bool:
return self.grid[y][x].isdigit()
def _issymbol(self, y, x) -> bool:
return (not self._isdigit(y, x)) and (self.grid[y][x] != '.')
def _find_all_symbols(self) -> list:
all_symbols = []
for y in range(len(self.grid)):
for x in range(len(self.grid[y])):
if self._issymbol(y, x):
all_symbols.append((y, x))
return all_symbols
def _find_adjacent_numerals(self, y, x) -> list:
tmp = dict()
for yadd in [-1, 0, 1]:
for xadd in [-1, 0, 1]:
x_, y_ = y + yadd, x + xadd
if self._isdigit(x_, y_):
# this is suboptimal because we will process the
# numeral adjacent to a symbol three times at max
# like in the following case
# ...*...
# ..123..
# however this saves some lines of code.
# we'll just use memoization with a dict to make sure
# we don't add the same numeral more than once
num, coords = self._extend_digit_to_numeral(x_, y_)
tmp[coords] = num
return list(tmp.values())
def _extend_digit_to_numeral(self, y, x) -> (int, tuple):
'''
this returns the numeral itself plus coordinates where the numeral
has been found (y, x1..xn)
'''
found_digit = self.grid[y][x]
x_array = [x]
x_ = x-1
proceed = True
while proceed:
if self._isdigit(y, x_):
x_array.append(x_)
found_digit = self.grid[y][x_] + found_digit
x_ -= 1
continue
proceed = False
x_ = x+1
proceed = True
while proceed:
if self._isdigit(y, x_):
x_array.append(x_)
found_digit += self.grid[y][x_]
x_ += 1
continue
proceed = False
x_array.sort()
return int(found_digit), (y,) + tuple(x_array)
def _find_part_numbers(self):
symbols = self._find_all_symbols()
for symbol in symbols:
found_numbers = self._find_adjacent_numerals(symbol[0], symbol[1])
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):
self._find_part_numbers()
return sum(self.part_numbers)
def find_sum_gears(self):
self._find_all_gears()
total = 0
for g in self.gears:
ratio = g[0] * g[1]
total += ratio
return total
if __name__ == '__main__':
r = tools.Reader('input.txt')
lines = r.read()
scheme = Scheme(lines)
x = scheme.find_sum_part_numbers()
print(x)
y = scheme.find_sum_gears()
print(y)