Update day03.py
This commit is contained in:
parent
4676f68466
commit
9c48228a3f
1 changed files with 54 additions and 41 deletions
95
day03.py
95
day03.py
|
@ -10,6 +10,7 @@ for line in fileinput.input():
|
||||||
numbers = {}
|
numbers = {}
|
||||||
gear_numbers = {}
|
gear_numbers = {}
|
||||||
|
|
||||||
|
|
||||||
def moore(x, y):
|
def moore(x, y):
|
||||||
for j in (-1, 0, 1):
|
for j in (-1, 0, 1):
|
||||||
for i in (-1, 0, 1):
|
for i in (-1, 0, 1):
|
||||||
|
@ -23,54 +24,66 @@ def moore(x, y):
|
||||||
|
|
||||||
for y in range(len(inp)):
|
for y in range(len(inp)):
|
||||||
for x in range(len(inp[y])):
|
for x in range(len(inp[y])):
|
||||||
if inp[y][x].isdigit():
|
if not inp[y][x].isdigit():
|
||||||
while x >= 0 and inp[y][x].isdigit():
|
continue
|
||||||
x -= 1
|
|
||||||
x += 1
|
|
||||||
|
|
||||||
if (x, y) in numbers:
|
while x >= 0 and inp[y][x].isdigit():
|
||||||
|
x -= 1
|
||||||
|
x += 1
|
||||||
|
|
||||||
|
if (x, y) in numbers:
|
||||||
|
continue
|
||||||
|
|
||||||
|
is_part_number = False
|
||||||
|
gear = None
|
||||||
|
|
||||||
|
x_ = x
|
||||||
|
number = 0
|
||||||
|
|
||||||
|
while x_ < len(inp[y]) and inp[y][x_].isdigit():
|
||||||
|
number = number * 10 + int(inp[y][x_])
|
||||||
|
|
||||||
|
for i, j in moore(x_, y):
|
||||||
|
if not inp[j][i].isdigit() and inp[j][i] != ".":
|
||||||
|
is_part_number = True
|
||||||
|
if inp[j][i] == "*":
|
||||||
|
gear = (i, j)
|
||||||
|
break
|
||||||
|
|
||||||
|
x_ += 1
|
||||||
|
|
||||||
|
if is_part_number:
|
||||||
|
numbers[(x, y)] = number
|
||||||
|
|
||||||
|
if not gear or (x, y) in gear_numbers:
|
||||||
|
continue
|
||||||
|
|
||||||
|
gear_number = 0
|
||||||
|
|
||||||
|
for i, j in moore(*gear):
|
||||||
|
if not inp[j][i].isdigit():
|
||||||
continue
|
continue
|
||||||
|
|
||||||
is_part_number = False
|
while j >= 0 and inp[j][i].isdigit():
|
||||||
gear = None
|
i -= 1
|
||||||
|
i += 1
|
||||||
x_ = x
|
|
||||||
number = 0
|
|
||||||
|
|
||||||
while x_ < len(inp[y]) and inp[y][x_].isdigit():
|
if (i, j) in gear_numbers:
|
||||||
number = number * 10 + int(inp[y][x_])
|
break
|
||||||
|
|
||||||
for i, j in moore(x_, y):
|
if (x, y) == (i, j):
|
||||||
if not inp[j][i].isdigit() and inp[j][i] != ".":
|
continue
|
||||||
is_part_number = True
|
|
||||||
if inp[j][i] == "*": gear = (i, j)
|
|
||||||
break
|
|
||||||
|
|
||||||
x_ += 1
|
i_ = i
|
||||||
|
while i_ < len(inp[j]) and inp[j][i_].isdigit():
|
||||||
|
gear_number = gear_number * 10 + int(inp[j][i_])
|
||||||
|
i_ += 1
|
||||||
|
|
||||||
if gear and (x, y) not in gear_numbers:
|
gear_number *= number
|
||||||
gear_number = 0
|
gear_numbers[(x, y)] = gear_number
|
||||||
|
break
|
||||||
for i, j in moore(*gear):
|
|
||||||
if inp[j][i].isdigit():
|
|
||||||
while j >= 0 and inp[j][i].isdigit():
|
|
||||||
i -= 1
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
if (i, j) in gear_numbers:
|
|
||||||
break
|
|
||||||
|
|
||||||
if (x, y) != (i, j):
|
part1 = sum(numbers.values())
|
||||||
i_ = i
|
|
||||||
while i_ < len(inp[j]) and inp[j][i_].isdigit():
|
|
||||||
gear_number = gear_number * 10 + int(inp[j][i_])
|
|
||||||
i_ += 1
|
|
||||||
gear_number *= number
|
|
||||||
gear_numbers[(x, y)] = gear_number
|
|
||||||
break
|
|
||||||
|
|
||||||
numbers[(x, y)] = (number, is_part_number, gear)
|
|
||||||
|
|
||||||
part1 = sum(num for (num, ispart, _) in numbers.values() if ispart)
|
|
||||||
part2 = sum(gear_numbers.values())
|
part2 = sum(gear_numbers.values())
|
||||||
print(part1, part2)
|
print(part1, part2)
|
||||||
|
|
Loading…
Reference in a new issue