Solution to problem 3 in Python
This commit is contained in:
@@ -119,5 +119,52 @@ def part_1() -> None:
|
|||||||
# What is the first value written that is larger than your puzzle input?
|
# 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__":
|
if __name__ == "__main__":
|
||||||
part_1()
|
part_1()
|
||||||
|
part_2()
|
||||||
|
|||||||
Reference in New Issue
Block a user