Solution to problem 3 part 1 in Python

This commit is contained in:
David Doblas Jiménez 2022-02-22 21:21:38 +01:00
parent 8de9fe3862
commit 8cbd51cb09

View File

@ -31,55 +31,32 @@
# Your puzzle input is 347991.
from typing import Generator, Tuple
# https://stackoverflow.com/questions/36834505/creating-a-spiral-array-in-python
# import numpy as np
from scipy.spatial.distance import cityblock
# def spiral_cw(A):
# A = np.array(A)
# out = []
# while A.size:
# out.append(A[0]) # take first row
# A = A[1:].T[::-1] # cut off first row and rotate counterclockwise
# return np.concatenate(out)
# def base_spiral(nrow, ncol):
# return spiral_cw(np.arange(nrow * ncol).reshape(nrow, ncol))[::-1]
# def to_spiral(A):
# A = np.array(A)
# B = np.empty_like(A)
# B.flat[base_spiral(*A.shape)] = A.flat
# return B
# A = np.arange(1, 348101).reshape(590, 590)
# print(to_spiral(A).shape)
def move_right(x, y):
# https://stackoverflow.com/questions/23706690/how-do-i-make-make-spiral-in-python
def move_right(x: int, y: int) -> Tuple[int, int]:
return x + 1, y
def move_down(x, y):
def move_down(x: int, y: int) -> Tuple[int, int]:
return x, y - 1
def move_left(x, y):
def move_left(x: int, y: int) -> Tuple[int, int]:
return x - 1, y
def move_up(x, y):
def move_up(x: int, y: int) -> Tuple[int, int]:
return x, y + 1
moves = [move_right, move_up, move_left, move_down]
def gen_points(end):
def gen_points(end: int) -> Generator[Tuple[int, Tuple[int, int]], int, None]:
from itertools import cycle
_moves = cycle(moves)
@ -102,24 +79,45 @@ def gen_points(end):
times_to_move += 1
my_lst = list(gen_points(347991))
# print(my_lst)
def part_1() -> None:
my_lst = list(gen_points(347991))
from scipy.spatial.distance import cityblock
from sympy import Segment
init = my_lst[0][1]
target = my_lst[347990][1]
s1 = my_lst[0][1]
print(s1)
s2 = my_lst[347990][1]
print(s2)
print(f"We need {cityblock(init, target)} steps")
print(cityblock(s1, s2))
# print(my_lst[0][1])
# s1 = Segment(my_lst[0][1][0], my_lst[0][1][1])
# s2 = Segment(my_lst[12][1][0], my_lst[12][1][1])
# intersect = s1.intersection(s2)
# if intersect:
# manhattan_distance = abs(intersect[0][0]) + abs(intersect[0][1])
# --- Part Two ---
# print(manhattan_distance)
# As a stress test on the system, the programs here clear the grid and then
# store the value 1 in square 1. Then, in the same allocation order as shown
# above, they store the sum of the values in all adjacent squares, including
# diagonals.
# So, the first few squares' values are chosen as follows:
# Square 1 starts with the value 1.
# Square 2 has only one adjacent filled square (with value 1), so it also
# stores 1.
# Square 3 has both of the above squares as neighbors and stores the sum of
# their values, 2.
# Square 4 has all three of the aforementioned squares as neighbors and
# stores the sum of their values, 4.
# Square 5 only has the first and fourth squares as neighbors, so it gets
# the value 5.
# Once a square is written, its value does not change. Therefore, the first few
# squares would receive the following values:
# 147 142 133 122 59
# 304 5 4 2 57
# 330 10 1 1 54
# 351 11 23 25 26
# 362 747 806---> ...
# What is the first value written that is larger than your puzzle input?
if __name__ == "__main__":
part_1()