Solution to problem 3 in Python

This commit is contained in:
David Doblas Jiménez 2022-02-23 20:39:08 +01:00
parent 8cbd51cb09
commit 85ec72b67c

View File

@ -119,5 +119,52 @@ def part_1() -> None:
# What is the first value written that is larger than your puzzle input?
def is_adjacent(p1: Tuple[int, int], p2: Tuple[int, int]) -> bool:
if (abs(p1[0] - p2[0]) == 0) and (abs(p1[1] - p2[1]) == 1):
return True
elif (abs(p1[0] - p2[0]) == 1) and (abs(p1[1] - p2[1]) == 0):
return True
elif (abs(p1[0] - p2[0]) == 1) and (abs(p1[1] - p2[1]) == 1):
return True
return False
def gen_points_adjacents(
end: int,
) -> Generator[Tuple[int, Tuple[int, int]], int, None]:
from itertools import cycle
_moves = cycle(moves)
n = 1
pos = 0, 0
times_to_move = 1
yield n, pos
saved_items = [(n, pos)]
while True:
for _ in range(2):
move = next(_moves)
for _ in range(times_to_move):
if n >= end:
return
pos = move(*pos)
adjacent_elements = []
for el in saved_items:
if is_adjacent(el[1], pos):
adjacent_elements.append(el[0])
n = sum(adjacent_elements)
saved_items.append((n, pos))
yield n, pos
times_to_move += 1
def part_2() -> None:
my_lst = list(gen_points_adjacents(347991))
print(f"The value we are looking for is {my_lst[-1:][0][0]}")
if __name__ == "__main__":
part_1()
part_2()