Added 2017/19
This commit is contained in:
parent
c970981aa9
commit
81990b5833
@ -2,129 +2,62 @@
|
|||||||
import sys,re
|
import sys,re
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
sys.path.insert(0, '../../')
|
sys.path.insert(0, '../../')
|
||||||
from fred import list2int
|
from fred import list2int,get_value_in_direction
|
||||||
|
|
||||||
input_f = 'test'
|
input_f = 'input'
|
||||||
|
|
||||||
part = 1
|
|
||||||
#########################################
|
#########################################
|
||||||
# #
|
# #
|
||||||
# Part 1 #
|
# Part 1+2 #
|
||||||
# #
|
# #
|
||||||
#########################################
|
#########################################
|
||||||
|
|
||||||
grid = []
|
grid = []
|
||||||
|
|
||||||
start = ()
|
with open(input_f) as file:
|
||||||
prev = ()
|
for line in file:
|
||||||
|
grid.append(line.rstrip())
|
||||||
|
|
||||||
def valid_n(grid,cur):
|
start = (0,grid[0].index('|'))
|
||||||
r,c = cur
|
|
||||||
count = 0
|
|
||||||
if grid[r-1][c] != ' ':
|
dir = (1,0)
|
||||||
|
visited = []
|
||||||
|
cur = start
|
||||||
|
count = 1
|
||||||
|
|
||||||
|
while get_value_in_direction(grid,cur) != 'S': # last letter in my input
|
||||||
|
|
||||||
|
new = (cur[0]+dir[0],cur[1]+dir[1])
|
||||||
|
|
||||||
|
if get_value_in_direction(grid,new) != ' ':
|
||||||
|
cur = new
|
||||||
count += 1
|
count += 1
|
||||||
if grid[r+1][c] != ' ':
|
|
||||||
count += 1
|
if get_value_in_direction(grid,cur).isalpha():
|
||||||
if grid[r][c+1] != ' ':
|
visited.append(get_value_in_direction(grid,cur))
|
||||||
count += 1
|
|
||||||
if grid[r][c-1] != ' ':
|
if get_value_in_direction(grid,cur) == '+':
|
||||||
count += 1
|
|
||||||
return count
|
if get_value_in_direction(grid,cur,'right') == '-' and dir[1] != -1:
|
||||||
|
dir = (0,1)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if get_value_in_direction(grid,cur,'left') == '-' and dir[1] != 1:
|
||||||
|
dir = (0,-1)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if get_value_in_direction(grid,cur,'up') == '|' and dir[0] != 1:
|
||||||
|
dir = (-1,0)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if get_value_in_direction(grid,cur,'down') == '|' and dir[0] != -1:
|
||||||
|
dir = (1,0)
|
||||||
|
continue
|
||||||
|
|
||||||
|
|
||||||
if part == 1:
|
for i in visited:
|
||||||
with open(input_f) as file:
|
print(i,end='')
|
||||||
for line in file:
|
print()
|
||||||
grid.append(list(line.rstrip()))
|
print(count)
|
||||||
|
|
||||||
for r, row in enumerate(grid):
|
|
||||||
for c, col in enumerate(row):
|
|
||||||
print(grid[r][c],end='')
|
|
||||||
print()
|
|
||||||
|
|
||||||
for r, row in enumerate(grid):
|
|
||||||
for c, col in enumerate(row):
|
|
||||||
if r == 0:
|
|
||||||
if grid[r][c] == '|':
|
|
||||||
start = (r,c)
|
|
||||||
|
|
||||||
end = False
|
|
||||||
cur = start
|
|
||||||
prev = (-1,-1)
|
|
||||||
prev_dir = {
|
|
||||||
'up': False,
|
|
||||||
'down': False,
|
|
||||||
'left': False,
|
|
||||||
'right': False
|
|
||||||
}
|
|
||||||
|
|
||||||
while not end:
|
|
||||||
r,c = cur
|
|
||||||
try:
|
|
||||||
if grid[r-1][c] != ' ' and prev != (r-1,c):
|
|
||||||
print('up',grid[r-1][c])
|
|
||||||
|
|
||||||
prev = cur
|
|
||||||
prev_dir = {
|
|
||||||
'up': True,
|
|
||||||
'down': False,
|
|
||||||
'left': False,
|
|
||||||
'right': False
|
|
||||||
}
|
|
||||||
cur = (r-1,c)
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
|
|
||||||
try:
|
|
||||||
if grid[r+1][c] != ' ' and prev != (r+1,c):
|
|
||||||
print('down',grid[r+1][c])
|
|
||||||
if valid_n(grid,cur) >= 1 and prev_dir['down']:
|
|
||||||
prev = cur
|
|
||||||
prev_dir = {
|
|
||||||
'up': False,
|
|
||||||
'down': True,
|
|
||||||
'left': False,
|
|
||||||
'right': False
|
|
||||||
}
|
|
||||||
cur = (r+1,c)
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
|
|
||||||
try:
|
|
||||||
if grid[r][c-1] != ' ' and prev != (r,c-1) and not prev_dir['left']:
|
|
||||||
print('left',grid[r][c-1])
|
|
||||||
prev = cur
|
|
||||||
prev_dir = {
|
|
||||||
'up': False,
|
|
||||||
'down': False,
|
|
||||||
'left': True,
|
|
||||||
'right': False
|
|
||||||
}
|
|
||||||
cur = (r,c-1)
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
|
|
||||||
try:
|
|
||||||
if grid[r][c+1] != ' ' and prev != (r,c+1) and not prev_dir['right']:
|
|
||||||
print('right',grid[r][c+1])
|
|
||||||
prev = cur
|
|
||||||
prev_dir = {
|
|
||||||
'up': False,
|
|
||||||
'down': False,
|
|
||||||
'left': False,
|
|
||||||
'right': True
|
|
||||||
}
|
|
||||||
cur = (r,c+1)
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
input()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#########################################
|
|
||||||
# #
|
|
||||||
# Part 2 #
|
|
||||||
# #
|
|
||||||
#########################################
|
|
||||||
if part == 2:
|
|
||||||
exit()
|
|
||||||
|
59
2017/20/20.md
Normal file
59
2017/20/20.md
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
## \-\-- Day 20: Particle Swarm \-\--
|
||||||
|
|
||||||
|
Suddenly, the GPU contacts you, asking for
|
||||||
|
help.
|
||||||
|
Someone has asked it to simulate *too many particles*, and it won\'t be
|
||||||
|
able to finish them all in time to render the next frame at this rate.
|
||||||
|
|
||||||
|
It transmits to you a buffer (your puzzle input) listing each particle
|
||||||
|
in order (starting with particle `0`, then particle `1`, particle `2`,
|
||||||
|
and so on). For each particle, it provides the `X`, `Y`, and `Z`
|
||||||
|
coordinates for the particle\'s position (`p`), velocity (`v`), and
|
||||||
|
acceleration (`a`), each in the format `<X,Y,Z>`.
|
||||||
|
|
||||||
|
Each tick, all particles are updated simultaneously. A particle\'s
|
||||||
|
properties are updated in the following order:
|
||||||
|
|
||||||
|
- Increase the `X` velocity by the `X` acceleration.
|
||||||
|
- Increase the `Y` velocity by the `Y` acceleration.
|
||||||
|
- Increase the `Z` velocity by the `Z` acceleration.
|
||||||
|
- Increase the `X` position by the `X` velocity.
|
||||||
|
- Increase the `Y` position by the `Y` velocity.
|
||||||
|
- Increase the `Z` position by the `Z` velocity.
|
||||||
|
|
||||||
|
Because of seemingly tenuous rationale involving
|
||||||
|
[z-buffering](https://en.wikipedia.org/wiki/Z-buffering), the GPU would
|
||||||
|
like to know which particle will stay closest to position `<0,0,0>` in
|
||||||
|
the long term. Measure this using the [Manhattan
|
||||||
|
distance](https://en.wikipedia.org/wiki/Taxicab_geometry), which in this
|
||||||
|
situation is simply the sum of the absolute values of a particle\'s `X`,
|
||||||
|
`Y`, and `Z` position.
|
||||||
|
|
||||||
|
For example, suppose you are only given two particles, both of which
|
||||||
|
stay entirely on the X-axis (for simplicity). Drawing the current states
|
||||||
|
of particles `0` and `1` (in that order) with an adjacent a number line
|
||||||
|
and diagram of current `X` positions (marked in parentheses), the
|
||||||
|
following would take place:
|
||||||
|
|
||||||
|
p=< 3,0,0>, v=< 2,0,0>, a=<-1,0,0> -4 -3 -2 -1 0 1 2 3 4
|
||||||
|
p=< 4,0,0>, v=< 0,0,0>, a=<-2,0,0> (0)(1)
|
||||||
|
|
||||||
|
p=< 4,0,0>, v=< 1,0,0>, a=<-1,0,0> -4 -3 -2 -1 0 1 2 3 4
|
||||||
|
p=< 2,0,0>, v=<-2,0,0>, a=<-2,0,0> (1) (0)
|
||||||
|
|
||||||
|
p=< 4,0,0>, v=< 0,0,0>, a=<-1,0,0> -4 -3 -2 -1 0 1 2 3 4
|
||||||
|
p=<-2,0,0>, v=<-4,0,0>, a=<-2,0,0> (1) (0)
|
||||||
|
|
||||||
|
p=< 3,0,0>, v=<-1,0,0>, a=<-1,0,0> -4 -3 -2 -1 0 1 2 3 4
|
||||||
|
p=<-8,0,0>, v=<-6,0,0>, a=<-2,0,0> (0)
|
||||||
|
|
||||||
|
At this point, particle `1` will never be closer to `<0,0,0>` than
|
||||||
|
particle `0`, and so, in the long run, particle `0` will stay closest.
|
||||||
|
|
||||||
|
*Which particle will stay closest to position `<0,0,0>`* in the long
|
||||||
|
term?
|
||||||
|
|
||||||
|
To begin, [get your puzzle input](20/input).
|
||||||
|
|
||||||
|
Answer:
|
||||||
|
|
Binary file not shown.
51
fred.py
51
fred.py
@ -6,3 +6,54 @@ def ppprint(x):
|
|||||||
for jdx,j in enumerate(i):
|
for jdx,j in enumerate(i):
|
||||||
print(x[idx][jdx],end='')
|
print(x[idx][jdx],end='')
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
def get_value_in_direction(grid, position, direction=None):
|
||||||
|
"""
|
||||||
|
Get the value in a specified direction from a given position in a grid.
|
||||||
|
If no direction is provided, returns the value at the current position.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
grid (list of list of int/float): The 2D grid.
|
||||||
|
position (set): A set containing x (row index) and y (column index) as integers.
|
||||||
|
direction (str, optional): The direction to check.
|
||||||
|
Options: 'up', 'down', 'left', 'right',
|
||||||
|
'up-left', 'up-right', 'down-left', 'down-right'.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The value in the grid in the specified direction, or None if out of bounds.
|
||||||
|
"""
|
||||||
|
# Ensure the position is a set of two integers
|
||||||
|
if len(position) != 2 or not all(isinstance(coord, int) for coord in position):
|
||||||
|
raise ValueError("Position must be a set containing two integers (x, y).")
|
||||||
|
|
||||||
|
x, y = position
|
||||||
|
offsets = {
|
||||||
|
'up': (-1, 0),
|
||||||
|
'down': (1, 0),
|
||||||
|
'left': (0, -1),
|
||||||
|
'right': (0, 1),
|
||||||
|
'up-left': (-1, -1),
|
||||||
|
'up-right': (-1, 1),
|
||||||
|
'down-left': (1, -1),
|
||||||
|
'down-right': (1, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
# If no direction is given, return the value at the current position
|
||||||
|
if direction is None:
|
||||||
|
if 0 <= x < len(grid) and 0 <= y < len(grid[x]):
|
||||||
|
return grid[x][y]
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Validate direction
|
||||||
|
if direction not in offsets:
|
||||||
|
raise ValueError(f"Invalid direction: {direction}. Choose from {list(offsets.keys())}")
|
||||||
|
|
||||||
|
dx, dy = offsets[direction]
|
||||||
|
new_x, new_y = x + dx, y + dy
|
||||||
|
|
||||||
|
# Check for out-of-bounds, considering varying row lengths
|
||||||
|
if 0 <= new_x < len(grid) and 0 <= new_y < len(grid[new_x]):
|
||||||
|
return grid[new_x][new_y]
|
||||||
|
else:
|
||||||
|
return None
|
Loading…
x
Reference in New Issue
Block a user