Quick and dirty solution to part 1

This commit is contained in:
David Doblas Jiménez 2022-02-21 21:36:19 +01:00
parent 81766ea23e
commit d58fff5e25

125
src/Year_2017/P3.py Normal file
View File

@ -0,0 +1,125 @@
# --- Day 3: Spiral Memory ---
# You come across an experimental new kind of memory stored on an infinite
# two-dimensional grid.
# Each square on the grid is allocated in a spiral pattern starting at a
# location marked 1 and then counting up while spiraling outward. For example,
# the first few squares are allocated like this:
# 17 16 15 14 13
# 18 5 4 3 12
# 19 6 1 2 11
# 20 7 8 9 10
# 21 22 23---> ...
# While this is very space-efficient (no squares are skipped), requested data
# must be carried back to square 1 (the location of the only access port for
# this memory system) by programs that can only move up, down, left, or right.
# They always take the shortest path: the Manhattan Distance between the
# location of the data and square 1.
# For example:
# Data from square 1 is carried 0 steps, since it's at the access port.
# Data from square 12 is carried 3 steps, such as: down, left, left.
# Data from square 23 is carried only 2 steps: up twice.
# Data from square 1024 must be carried 31 steps.
# How many steps are required to carry the data from the square identified in
# your puzzle input all the way to the access port?
# Your puzzle input is 347991.
# https://stackoverflow.com/questions/36834505/creating-a-spiral-array-in-python
# import numpy as np
# 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):
return x + 1, y
def move_down(x, y):
return x, y - 1
def move_left(x, y):
return x - 1, y
def move_up(x, y):
return x, y + 1
moves = [move_right, move_up, move_left, move_down]
def gen_points(end):
from itertools import cycle
_moves = cycle(moves)
n = 1
pos = 0, 0
times_to_move = 1
yield n, pos
while True:
for _ in range(2):
move = next(_moves)
for _ in range(times_to_move):
if n >= end:
return
pos = move(*pos)
n += 1
yield n, pos
times_to_move += 1
my_lst = list(gen_points(347991))
# print(my_lst)
from scipy.spatial.distance import cityblock
from sympy import Segment
s1 = my_lst[0][1]
print(s1)
s2 = my_lst[347990][1]
print(s2)
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])
# print(manhattan_distance)